add constraint no_unique unique nonclustered(no)
no_unique是约束的名字,no是约束列名 ,将此语句在控制台执行就可实现添加约束。
例:添加外键约束
alter table book_info
add constraint FK_bookinfo_room
foreign key(room_id) references tb_room(room_id)
其中 book_info为要添加外键的表, FK_bookinfo_room为外键约束名 ,room_id为 book_info表中的字段, tb_room(room_id)为要参考的外键约束。
----------------------------------------------------------------------------------------------------------
创建表以及添加外键约束、check约束
create table 借阅
(
书号 nchar(10) NOT NULL foreign key references 图书(书号),
借书证号 nchar(6) NOT NULL check (借书证号 like'[0-9][0-9][0-9][0-9][0-9][0-9]')foreign key references 读者(借书证号),
借书日期 datetime NOT NULL,
还书日期 datetime NOT NULL,
primary key(书号,借书证号)
)
创建表“借书” 。书号添加外键约束图书表中的书号,借阅证号参考读者表中的借阅证号,并且约束借阅证号为六位0到9之间的数字。主键为书号和借阅证号。