sql 约束语法

来源:百度知道 编辑:UC知道 时间:2024/05/26 17:15:45
现在有2张表
A、Person 人员表 字段 personcode 人员编码
B、kkk 业余表
现在在KKK 中字段Personcode存储时候检测是否在Person存在,不存在不能保存
这个用约束能做到吗

我把添加约束的语法告诉你,请你根据自己情况灵活使用。

--------添加主键约束(bookid作为主键)
alter table bookmessage
add constraint pk_bookid primary key(bookid)

--------添加唯一约束
alter table bookmessage
add constraint uq_bookid UNIQUE(bookid)

---------添加默认约束
alter table bookmessage
add constraint df_address DEFAUIT('地址不详') for Address

--------添加检出约束,要求年龄只能在15---40之间
alter table readermessage
add constraint CK_age CHECK(age BETWEEN 15 AND 40)

-------添加外键约束
alter table bookmessage
add constraint fk_bookid
foreign key(bookid)<外键> references readermessage<表>(readerid)<表中的主键>

-------删除约束
alter table 表名
drop constraint 约束名

--
以上,希望对你有有所帮助。

用外键啊