SQL

一.Transact-SQL的GO,详解

(1) SQL Server 实用工具将 GO 解释为应将当前的 Transact-SQL 批处理语句发送给 SQL Server 的信号。当前批处理语句是自上一 GO 命令后输入的所有语句,若是第一条 GO 命令,则是从特殊会话或脚本的开始          处到这条 GO 命令之间的所有语句。

 

(2)GO 命令和Transact-SQL 语句不可在同一行上。但在 GO 命令行中可包含注释。 用户必须遵照使用批处理的规则。例如,在批处理中的第一条语句后执行任何存储过程必须包含 EXECUTE 关键字。局部(用户定义)变量的作用域限制在一个批处理中,不可在 GO 命令后引用 二.关于SET ANSI_PADDING的用法 当设置为 ON 时,不剪裁字符值中插入到 varchar 列的尾随空格和二进制值中插入到 varbinary 列的尾随零。不将值按列的长度进行填充。 当设置为 OFF 时,剪裁 varchar 列的尾随空格和 varbinary 列的尾随零。该设置只影响新列的定义。

(1)SET ANSI_PADDING 为 ON 时,将允许空值的 Char(n) 和 binary(n) 列填充到列长

(2)SET ANSI_PADDING 为 OFF 时,将剪裁尾随空格和零。始终将不允许空值的 Char(n) 和 binary(n) 列填充到列长。

三.一些建表常用到的命令--自己做的一个DemoDB模板

use master
go
if exists(select * from sysdatabases where name='DemoDB' )
drop database DemoDB
go

create database DemoDB
on primary
(
name='DemoDB_data',
filename='C:DBDemoDB_data.mdf',
size=10MB,
filegrowth=1MB

)
log on
(
name='DemoDB_log',
filename='C:DBDemoDB_log.ldf',
size=3MB,
filegrowth=1MB
)
go

use DemoDB
go
if exists(select * from sysobjects where name='Students')
drop table Students
go
create table Students
(
StudentId int identity(100000,1),
StudentName varchar(20) not null,
Gender char(2) not null,
Birthday datetime not null,
StudentIdNo numeric(18,0) not null,
Age int not null,
PhoneNumber varchar(50),
StudentAddress varchar(500),
ClassId int not null --班级编号,外键
)
go

--创建班级表
if exists(select * from sysobjects where name='StudentClass')
drop table StudentClass
go
create table StudentClass
(
ClassId int primary key,
ClassName varchar(20) not null
)
go

--创建成绩表
if exists(select * from sysobjects where name='ScoreList')
drop table ScoreList
go
create table ScoreList
(
Id int identity(1,1) primary key,
StudentId int not null,
CSharp int null,
SQLServerDB int null,
UOdateTime datetime not null
)
go
--创建管理员表
if exists(select * from sysobjects where name='Admins')
drop table Admins
go
create table Admins
(
LoginId int identity(1000,1) primary key,
LoginPwd varchar(20) not null,
AdminName varchar(20) not null
)
go

--创建主键约束
use DemoDB
go
if exists(select * from sysobjects where name='pk_StudentId')
alter table Students drop constraint pk_StudentId

alter table Students
add constraint pk_StudentId
primary key(StudentId) --修改这个数据表


--创建唯一约束
alter table Students
add constraint uq_StudentIdNo
unique(StudentIdNo)

--创建检查约束
alter table Students
add constraint ck_Age
check(Age between 18 and 25)


--创建默认约束
alter table Students
add constraint df_StudentAddress
default('地址不详')
for StudentAddress

--创建外键约束 --forreign key创建一个外键 references引用

alter table Students
add constraint fk_ClassId
foreign key(ClassId)
references StudentClass(ClassId)

--测试
--插入数据
--添加测试数据的时候,在实际项目中必须首先完成所有主键表数据的添加
insert into StudentClass(ClassId,ClassName)
values(1,'软件一班')
insert into StudentClass(ClassId,ClassName)
values(2,'软件二班')
insert into StudentClass(ClassId,ClassName)
values(3,'软件三班')
insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,
StudentAddress,ClassId)
values('马向前','男','1990-05-24',130628199005242234,24,'15911442348','保定市',1)

insert into Students(StudentName,Gender,Birthday,StudentIdNo,Age,PhoneNumber,
StudentAddress,ClassId)
values('向前','男','1990-05-24',130628199005242233,24,'15221442348','保定市',1)

insert into Admins(AdminName,LoginPwd,LoginId)
values('Max','123456')

--更改表中数据
update StudentClass
set ClassName='软件二班'
where ClassName='软件2二班'--表中多了一个数字2 ,可以直接去更改数据

--删除表中数据
delete StudentClass
where ClassName='软件二班' --把表中名字为软件二班的那条数据删除

--查询,简单的查表
select * from Students
select * from StudentClass
select * from Admins

 

原文地址:https://www.cnblogs.com/Maxq/p/5216389.html