根據不同的需要,sql server數據庫中不同的表之間有的將有所關聯,下面將爲您介紹sql server中如何增加約束,並刪除的方法,供您參考。
sql server中增加約束的語句是:
---添加主鍵約束
alter table 表名
add constraint 約束名 primary key (主鍵)
---添加唯一約束
alter table 表名
add constraint 約束名 unique (字段)
---添加默認約束
alter table 表名
add constraint 約束名 default ('默認內容') for 字段
--添加檢查check約束,要求字段只能在1到100之間
alter table 表名
add constraint 約束名 check (字段 between 1 and 100 )
---添加外鍵約束(主表stuInfo和從表stuMarks建立關係,關聯字段爲stuNo)
alter table 從表
add constraint 約束名
foreign key(關聯字段) references 主表(關聯字段)
GO
sql server中刪除約束的語句是:
alter table 表名 drop constraint 約束名
sp_helpconstraint 表名 找到數據表中的所有列的約束