创建表错误:引用表中没有与外键

时间:2017-04-01 02:11:46

标签: sql sql-server tsql

CREATE TABLE [Role]
(
     Role_Name nvarchar(20) NOT NULL,
     Bssn nvarchar(20) NOT NULL,

     CONSTRAINT Pk_Role_Role_Name 
         PRIMARY KEY(Bssn, Role_Name),
     CONSTRAINT Fk_Role_Bssn 
         FOREIGN KEY(Bssn) REFERENCES Bandmembers(Ssn) 
             ON DELETE CASCADE ON UPDATE CASCADE
)

CREATE TABLE Has
( 
    Role_Name nvarchar(20)NOT NULL,
    Bssn nvarchar(20) NOT NULL,

    CONSTRAINT Pk_Role_Id_Ssn_Id 
        PRIMARY KEY(Bssn, Role_Name),
    CONSTRAINT Pk_Works_0N_Role_Name 
        FOREIGN KEY(Bssn) REFERENCES Bandmembers(Ssn) 
            ON DELETE CASCADE ON UPDATE CASCADE, 
    CONSTRAINT Pk_Works_On_Bssn 
        FOREIGN KEY (Role_Name) REFERENCES role(Role_Name) 
            ON DELETE CASCADE ON UPDATE CASCADE
)

错误:

  

Msg 1776,Level 16,State 0,Line 1
  引用的表“Role”中没有与外键“Pk_Works_On_Role”中的引用列列表匹配的主键或候选键。

     

Msg 1750,Level 16,State 0,Line 1
  无法创建约束。查看以前的错误。

1 个答案:

答案 0 :(得分:1)

表Has的外键与引用表的主键相同。

CONSTRAINT Pk_Works_On_Bssn FOREIGN KEY (Bssn, Role_Name) REFERENCES [Role](Bssn, Role_Name)ON DELETE CASCADE ON UPDATE CASCADE