四、完整性和约束

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'))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值