0,自定义数据类型
exec sp_addtype birthday,datetime,'null'
1,建表(带有check约束,主键约束和非空约束)
use review
create table test
(
[name] varchar(10)not null primary key,
age int not null,
sex varchar check(sex in('m','f'))
)
2,建主键
alter table test
add constraint prim_key primary key([name])
3,删除主键
alter table test
drop constraint prim_key
4,向表中填加重复值
declare @a int
select @a=0
while @a<10
begin
insert test values('cheng',@a,'f')
select @a=@a+1
end
5,显示前几条
select distinct top 5 * from test
6,两列合并
select name+' '+convert(varchar(2),age) as 'name&age' from test
7,identity约束
alter table test
add num int identity(101,2) //不能将已经存在的列加IDENTITY约束。
8,IDENTITYCOL 代表对具有IDENTITY约束列的引用
select name,identitycol from test
9,删除列
alter table test
drop column age
10,建外键
alter table test_1
add constraint for_key foreign key(num) references test
11, 唯一约束//可以有一个空值
alter table test_1
add constraint unq_add unique (name)
12,建组合键//组合的结果不为空即可
alter table test_1
add constraint prm_key primary key([name],num)
13,default约束
alter table test
add constraint def_key default(100) for age
14 uniqueidentifier约束//只允许在创建时用,只允许用newid()函数来加记录
create table test1
(
[name] varchar(10),
num1 uniqueidentifier not null
)
15,newid()函数
insert test1 values('hello',newid())
16,check约束
alter table test1
add sex varchar(2) check(sex in('m','f'))