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列的用的着写其他列的列名吗?
在学校机房的机器上可以,在我自己的机器上就出现上述错误,不知道什么原因,烦死我了,请大哥们指教.

解决方案 »

  1.   

    ALTER TABLE TABLE1 ADD UID INT IDENTITY(1,1)
    set       IDENTITY_INSERT     TABLE1  ON
    insert into TABLE1(cs,UID)
    select '2008-12-23',4
    union all
    select '2009-01-24',5
      

  2.   


    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--有结果
      

  3.   

    set       IDENTITY_INSERT     TABLE1  ON
    /*
    是不是你运行了我的语句。。这句是开启你的identity 插入
    */
      

  4.   

    没有开启identity_insert,可能是应用获取用户ID没有成功造成的
      

  5.   

    +1create table tb(id int identity(1,1),col1 nvarchar(10),col2 nvarchar(10))
    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 行受影响)*/
      

  6.   

    你出错的原因在于你现在数据库中另两个表主键没有对应的值:
    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 行受影响)*/
      

  7.   

    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'.===============
    表bbsUsers中没有uid=0的记录 所以提示此错误信息
    既然建立了约束关系那么
    tuid 就必须是在表bbsUsers的uid的某一个值,当找不到就报错。
      

  8.   

    外键约束 了!并不是identity的问题,自增长列不用写列名,但其它人一列名要写写
    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,或者检查是否存在!