日期:2014-05-17 浏览次数:20868 次
--建立两个表 create table publishinfo ( publishid int,--出版社ID publish varchar2(20),--出版社 constraint un_publish unique (publish) ); create table bookinfo ( bookid int,--图书ID loan char(1),--借出状态 publish varchar2(20),--出版社 constraint fk_publish foreign key (publish) references publishinfo (publish) on delete cascade ); --开始添加数据 insert into publishinfo (publishid ,publish) values (10000,111); insert into publishinfo (publishid ,publish) values (10000,null); insert into bookinfo (publishid ,publish) values (10000,222);--依旧会进行验证
--首先你的不是组合fk,组合是两个或两个以上联合作为外键,如下id,sid在一起叫组合fk create table t( id int, sid int, name varchar2(20), constraint pk_t primary key(id,sid) ); create table ct( cid int, id int, sid int, name varchar(20), constraint pk_ct primary key(cid), constraint fk_ct_t_id_sid foreign key(id,sid) references t(id,sid) ); insert into t values(1,1, 'name1'); insert into ct values(1,1,1, 'n1 '); --此处可以成功,就像你说的:则将跳过组成该约束的所有值的验证。 insert into ct values(2,1,null, 'n2'); --此处失败 insert into ct values(2,1,2, 'n2');
------解决方案--------------------
FOREIGN KEY约束可以包含空值
任何组合FOREIGN KEY约束的列包含空值