问个SQL外键很简单的问题,给分再加分

来源:百度知道 编辑:UC知道 时间:2024/06/22 09:11:32
请看SQL代码↓
if exists(select *from sysobjects where name='bb')
drop table bb
create table bb
(
bid int identity(1000,1) primary key,
bexname varchar(20) not null,
bexsorce varchar(20) not null
)
go
if exists(select *from sysobjects where name='cc')
drop table cc
go
create table cc
(
cid int identity primary key,
bid int foreign key(bid) references bb(bid),
clive varchar(20) not null
)
go
insert into bb values('ggg','100')
insert into bb values('kkkk','100')
insert into bb values('ggghj','100')
insert into bb values('ggghyj','100')
insert into bb values('ggghfdj','100')
insert into cc values(1,'100')
select *from cc

问题↓
为什么一定要先在bb表中插入5条以上的数据,才可以再在cc表中插入数据,这样就不会有(—INSERT 语句与 COLUMN FOREIGN KEY 约束 'FK__cc__bid__7F6CA5A8' 冲突。该冲突发生于数据

不是一定要5条以上吧,只要有数据应该就可以 比如bb表中的bid 没有 “6” 但是你在cc表中插入bid的值为“6”的记录是就会有错 和记录数没什么关系 所以你需要检查bb表中bid的值