2013-06-04 95 views
-1

我在嘗試在數據庫中插入一些數據時出現此錯誤。INSERT語句與FOREIGN KEY約束衝突。錯誤消息與我的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','[email protected]','[email protected]','Philippines','12345','0277824893'), 
    ('Dev123456B','Actor One','@ctor1','[email protected]','New Zealand','11234','0293847567'), 
    ('Dev123456C','Actor Two','@ctor2','[email protected]','USA','42562','028394576'), 
    ('Dev123456D','Actor Three','@ctor3','[email protected]','USA','93847','0294857689'), 
    ('Dev123456E','Actor Four','@ctor4','[email protected]','Australia','02947','0293847563'), 
    ('Dev123456F','Actor Five','@ctor5','[email protected]','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

檢查,有沒有衝突目前,然後刪除該FK添加數據並添加FK回來。 –

+1

這是什麼DBMS?我猜可能SQL Server給予使用dbo。可以幫助將其添加到問題中。 –

+0

我在小提琴中嘗試過你的唱片,它的工作完美。 http://www.sqlfiddle.com/#!3/4c27e/1 –

回答

4

您的數據插入到表開發失敗,所以不必在開發表中的任何記錄。而當你將數據插入節目表,Programme.DevID不能引用Developer.DevID

我想你的腳本插入數據,並出現錯誤開發商表:

字符串或二進制數據將被截斷。 該聲明已被終止。

'[email protected]' 萊恩:21字符,但電子郵件字段:VARCHAR(20)=>錯誤

您可以再次檢查

+0

謝謝我最終能夠使它工作..謝謝 – user2449983

相關問題