SQL 约束

来源:百度知道 编辑:UC知道 时间:2024/05/08 00:55:45
请问表的约束怎么写?分别有4个表:读者信息表,书籍信息表,借书表,还书表.还书表与借书表分别包含读者信息表的读者编号,书信息信息表的索书号,请问约束怎么写呢?谢谢~
= =他说是表与表之间的约束

你说的不是很清楚,我给你一个例子吧,但愿你能在其中找到你想要的:
if exists(select * from sysobjects where name = 'caption')
drop table caption

---------------------------------建立标题表(caption)---------------------------------
create table caption
(
Cid int not null, ----三级标题编号
Ctitle varchar(30) not null, ----标题名
Crid int not null,-----与(类型分枝)表相对应的ID
Caid int not null,------审核编号(可以从authorAndCensor表中得到作者和审核状态等信息)
Cdatatime datetime not null, ------创作日期
)
go

---------------建立表约束------------------
--编号(主键)约束
alter table caption
add constraint pk_Sid primary key (Cid)
--类别(主外键)约束
alter table caption
add constraint fk_Crid foreign key(Crid) references genreRamify(Rid)
--审核编号(主外键)约束
alter table caption
add constraint fk_Caid foreign key(Caid) references authorAndCensor(Aid)
--时间(默认)约束
alter table caption
add constraint df_Cdatetime default (Getdate()) for Cdatatime
go