【问题标题】:SQL ERROR : "There are no primary or candidate keys in the referenced table..."SQL 错误:“引用的表中没有主键或候选键...”
【发布时间】:2012-05-25 00:58:03
【问题描述】:

我使用的是 SQL Server 2008。当我尝试创建表“Dossier_Financement”时出现错误:

create table Dossier_Financement 
(
    ID_Reunion integer foreign key references Reunion(ID_Reunion),
    ID_Dossier integer foreign key references Dossier(ID_Dossier),
    Decision varchar(20),
    Motif text,
    Montant_Retenu decimal(6,2),/* montant accorder */
    Duree_Retenu smallint,/*nb jours accorder */
    Nom_Giac varchar(50) foreign key references GIAC(Nom_Giac),
    primary key(ID_Dossier,Nom_Giac,ID_Reunion)
)
GO

这是两张表:

create table Reunion 
(
    ID_Reunion integer ,
    Date_Reunion datetime,
    ID_Membre integer,/*jquery*/
    Type_Reunion varchar(20),
    Nom_Giac varchar(50),
    foreign key(ID_Membre,Nom_Giac) references Membre(ID_Membre,Nom_Giac),
    primary key(ID_Reunion,Nom_Giac)
)
GO


create table Dossier_Financement 
(
    ID_Reunion integer foreign key references Reunion(ID_Reunion),
    ID_Dossier integer foreign key references Dossier(ID_Dossier),
    Decision varchar(20),
    Motif text,
    Montant_Retenu decimal(6,2),/* montant accorder */
    Duree_Retenu smallint,/*nb jours accorder */
    Nom_Giac varchar(50) foreign key references GIAC(Nom_Giac),
    primary key(ID_Dossier,Nom_Giac,ID_Reunion)
)
GO

“Reunion”正常执行,没有任何问题,但在尝试创建第二个表时出现此错误:

Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 'Reunion' that match the referencing column list in the foreign key 'FK__Dossier_F__ID_Re__5629CD9C'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

【问题讨论】:

  • 就是它所说的。 “主表”中的列必须是键。如果 Reunion.ID_Reunion 未与 Now_Giac 配对,则不符合此要求。
  • Reunion 表中的 ID_Reunion 必须为主键。
  • 但它确实有一个主键 Reunion(ID_Reunion)
  • @Yassineedouiri 它有一个复合 PK (ID_Reunion, Now_Giac)。这并不意味着 ID_Reunion(没有 Now_Giac)是一个 Key。
  • 创建表 Reunion ( ID_Reunion integer , primary key(ID_Reunion,Nom_Giac) ) GO

标签: sql sql-server sql-server-2008 foreign-keys


【解决方案1】:

根据实际模型需要/要求,一种解决方案是引用Key,这需要所有部分(注意Nom_Giac被添加到FK定义中):

create table Dossier_Financement 
(
    ...
    foreign key(ID_Reunion,Nom_Giac) references Reunion(ID_Reunion,Nom_Giac),
)

另一种解决方案,根据 Mark M 的回答是使 ID_Reunion 列成为 Key(注意 Nom_Giac 已从 PK 定义中删除):

create table Reunion 
(
    ...
    primary key(ID_Reunion)
)

这将使 ID_Reunion 成为一个键(实际上是主键),然后可以用 foreign key references Reunion(ID_Reunion) 引用。

编码愉快!

【讨论】:

  • 另外,如果尝试第一个选项,则需要按照它们在键中出现的顺序列出列。 (例子是正确的,但值得一提)
【解决方案2】:

您只能使用所引用表的完整主键来创建外键。您的第二个表尝试仅使用 Reunion 表的一半主键来创建外键。

【讨论】:

  • 不一定是主键;一个唯一索引(这意味着它是一个候选键)也足够了。
  • 但是,我认为它可能应该是PK,所以+1。
  • tnks dudde 为您提供帮助...您能更具体一点
  • 您在 Reunion 上的主键声明为 ID_Reunion,Nom_Giac,但您在 Dossier_Financement 中的外键引用仅使用 ID_Reunion。它需要使用您声明为主键的两列,或者您需要将 Reunion 主键更改为仅包含 ID_Reunion
  • @pst:理论上你是对的,但实际上许多 RDBMS 系统要求外键引用主键。
【解决方案3】:

在要创建索引的列上创建唯一索引:

CREATE UNIQUE NONCLUSTERED INDEX [UX_Index] ON dbo.[Table]([Column1],[Column2])

【讨论】:

    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多