sql 约束

为方便大家理解约束的含义及约束的使用,以下示例均以第三种方法为各个字段添加约束]

 

--SQL Server2000提供了以下5中约束:

 

--添加主键约束(即primary key约束)

 

alter table goods add constraint pk_gid primary key(gid)

 

--№1.删除主键约束

 

alter table goods drop pk_gid

 

--添加唯一约束(即unique约束)

 

alter table goods add constraint uq_gname unique(gname)

 

--删除唯一约束:仿№1.

 

--添加缺省约束(即default约束)

 

alter table goods add constraint def_gtel default 0000-00000000 for gtel

 

--删除缺省约束:仿№1.

 

--添加检查约束(即check约束)

 

alter table goods add constraint ck_gprice check(gprice>500)

 

--删除check约束:仿№1.

 

--创建表2

 

create table g_p

 

(

 

wno int identity(1,1) primary key,

 

gno int

 

)

 

--添加外键约束(即foreign key约束)

 

alter table g_p

 

add constraint fk_gno

 

foreign key(gno) references goods(gid)

 

--删除外键约束:仿№1.

 


--使用newid()

select newid()--生成全球唯一的ID号

 

create table customer

 

(

 

custID uniqueidentifier not null default newid(),

 

customer char(30) not null

 

)

转载于:https://www.cnblogs.com/lean/articles/1081382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值