bbsTopic(TID int IDENTITY(1,1) NOT NULL,
TSID int NOT NULL,
TUID int NOT NULL,
TTOPIC varchar(255) NOT NULL,
TCONTENTS text NOT NULL,
TTIME datetime NULL,
treplycount int not null,)alter table bbsTopic add constraint PK_TID primary key (TID)
alter table bbsTopic add constraint FK_TSID foreign key(TSID) references bbsSection(SID)
alter table bbsTopic add constraint FK_TUID foreign key(TUID) references bbsUsers(UID)执行下列语句
insert bbstopic values(1,0,'112','121','2011-01-12 17:55:17',0)
就会出现如下错误
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TUID". The conflict occurred in database "bbsDB", table "dbo.bbsUsers", column 'UID'.我想问,插入的时候如果是全插入的话,除了那个identity列的用的着写其他列的列名吗?
在学校机房的机器上可以,在我自己的机器上就出现上述错误,不知道什么原因,烦死我了,请大哥们指教.
TSID int NOT NULL,
TUID int NOT NULL,
TTOPIC varchar(255) NOT NULL,
TCONTENTS text NOT NULL,
TTIME datetime NULL,
treplycount int not null,)alter table bbsTopic add constraint PK_TID primary key (TID)
alter table bbsTopic add constraint FK_TSID foreign key(TSID) references bbsSection(SID)
alter table bbsTopic add constraint FK_TUID foreign key(TUID) references bbsUsers(UID)执行下列语句
insert bbstopic values(1,0,'112','121','2011-01-12 17:55:17',0)
就会出现如下错误
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TUID". The conflict occurred in database "bbsDB", table "dbo.bbsUsers", column 'UID'.我想问,插入的时候如果是全插入的话,除了那个identity列的用的着写其他列的列名吗?
在学校机房的机器上可以,在我自己的机器上就出现上述错误,不知道什么原因,烦死我了,请大哥们指教.
set IDENTITY_INSERT TABLE1 ON
insert into TABLE1(cs,UID)
select '2008-12-23',4
union all
select '2009-01-24',5
create table bbsTopic(TID int IDENTITY(1,1) NOT NULL,
TSID int NOT NULL,
TUID int NOT NULL,
TTOPIC varchar(25) NOT NULL,
TCONTENTS text NOT NULL,
TTIME datetime NULL,
treplycount int not null)--插入数据
insert bbstopic values(1,0,'112','121','2011-01-12 17:55:17',0)
go 10
select * from bbstopic--有结果
/*
是不是你运行了我的语句。。这句是开启你的identity 插入
*/
insert into tb select 'abcd','efg' union all select 'vad','vwed'
select * from tb
go
drop table tb
/*
id col1 col2
----------- ---------- ----------
1 abcd efg
2 vad vwed(2 行受影响)*/
create table bbsTopic(TID int IDENTITY(1,1) NOT NULL,
TSID int NOT NULL,
TUID int NOT NULL,
TTOPIC varchar(255) NOT NULL,
TCONTENTS text NOT NULL,
TTIME datetime NULL,
treplycount int not null,)
create table bbsSection(sid int PRIMARY KEY)
create table bbsUsers(uid int PRIMARY KEY)
insert into bbsSection select 1
insert into bbsUsers select 0
go
alter table bbsTopic add constraint PK_TID primary key (TID)
alter table bbsTopic add constraint FK_TSID foreign key(TSID) references bbsSection(SID)
alter table bbsTopic add constraint FK_TUID foreign key(TUID) references bbsUsers(UID)
go
insert bbstopic values(1,0,'112','121','2011-01-12 17:55:17',0)
go
select * from bbstopic
drop table bbstopic,bbsSection,bbsUsers
/*
(1 行受影响)
TID TSID TUID TTOPIC TCONTENTS TTIME treplycount
----------- ----------- ----------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------- -----------
1 1 0 112 121 2011-01-12 17:55:17.000 0(1 行受影响)*/
就会出现如下错误
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TUID". The conflict occurred in database "bbsDB", table "dbo.bbsUsers", column 'UID'.===============
表bbsUsers中没有uid=0的记录 所以提示此错误信息
既然建立了约束关系那么
tuid 就必须是在表bbsUsers的uid的某一个值,当找不到就报错。
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TUID". The conflict occurred in database "bbsDB", table "dbo.bbsUsers", column 'UID'.
tuid列与bbsusers中uid是外键关系!如果在bbsusers不存在此uid,这里为112,则外键验证失败,所以不能插入!
所以,应先插入bbsuser,或者检查是否存在!