INSERT语句与FOREIGN KEY约束冲突。我的sql语句的错误消息

时间:2013-06-04 02:22:49

标签: sql

尝试在数据库中插入一些数据时出现此错误。

  

INSERT语句与FOREIGN KEY约束FK_DevID_Prog冲突。冲突发生在数据库ElectricImp,表dbo.Developer,列DevID

代码:

Create Table Developer
(
    DevID       Char(10)    Not Null,
    UserName    Char(20)    Not Null,
    DevPassword Char(20)    Not Null,
    Email       VarChar(20) Not Null,
    Country     VarChar(25) Not Null,
    ZipCode     Integer,
    CellPhone   Integer     Not Null,
    Constraint PK_DevID Primary Key(DevID)
);

--Drop Table Programme;
Create Table Programme
(
    ProgrammeID     Char(10)    Not Null,
    DevID           Char(10)    Not Null,
    ProgrammeName   VarChar(20) Not Null,
    ProgrammeCode   VarChar(20) Not Null,
    Constraint PK_ProgID Primary Key(ProgrammeID),
    Constraint FK_DevID_Prog Foreign Key(DevID) References Developer(DevID)

Insert Into 
Developer(DevID,UserName,DevPassword,Email,Country,ZipCode,CellPhone)
Values
    ('Dev123456A','Paul Wilson Diu','P@ulWilson','Paul_Wilson@yahoo.com','Philippines','12345','0277824893'),
    ('Dev123456B','Actor One','@ctor1','Actor_One@yahoo.com','New Zealand','11234','0293847567'),
    ('Dev123456C','Actor Two','@ctor2','Actor_Two@yahoo.com','USA','42562','028394576'),
    ('Dev123456D','Actor Three','@ctor3','Actor_Three@yahoo.com','USA','93847','0294857689'),
    ('Dev123456E','Actor Four','@ctor4','Actor_Four@yahoo.com','Australia','02947','0293847563'),
    ('Dev123456F','Actor Five','@ctor5','Actor_Five@gmail.com','Germany','92846','0293748123');

Insert Into 
Programme(ProgrammeID,DevID,ProgrammeName,ProgrammeCode)
Values
    ('Prog12345A','Dev123456A','Programme A','ProgCode123A'),
    ('Prog12345B','Dev123456B','Programme B','ProgCode123B'),
    ('Prog12345C','Dev123456A','Programme C','ProgCode123C'),
    ('Prog12345D','Dev123456C','Programme D','ProgCode123D'),
    ('Prog12345E','Dev123456B','Programme E','ProgCode123E'),
    ('Prog12345F','Dev123456D','Programme F','ProgCode123F'),
    ('Prog12345G','Dev123456C','Programme G','ProgCode123G'),
    ('Prog12345H','Dev123456E','Programme H','ProgCode123H');

1 个答案:

答案 0 :(得分:4)

您的数据插入Developer表失败,因此在Developer表中没有任何记录。当您将数据插入到Program表中时,Programme.DevID不能引用Developer.DevID

我尝试了你的脚本插入数据,并且发生了错误:Developer table:

字符串或二进制数据将被截断。 声明已经终止。

'Paul_Wilson@yahoo.com'Len:21个字符,但电子邮件字段:VARCHAR(20)=>错误

您可以再次查看