简体   繁体   English

SQL错误:“引用的表中没有主键或候选键...”

[英]SQL ERROR : “There are no primary or candidate keys in the referenced table…”

I'm using SQL Server 2008. I'm getting an error when I try to create the table 'Dossier_Financement': 我正在使用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

These are the two tables: 这是两个表:

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

The 'Reunion' execute normally without any problem but I get this error when trying to create the second table: '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.

You can only make a foreign key using the complete primary key of the table you are referencing. 您只能使用要引用的表的完整主键创建外键。 Your 2nd table tries to create a foreign key using only half of the Reunion table's primary key. 您的第二个表尝试仅使用Reunion表的主键的一半来创建外键。

Depending upon the actual model needs/requirements, one solution is to reference the Key, which requires all parts (note that Nom_Giac is added to the FK definition): 根据实际的模型需求/要求,一个解决方案是引用Key,它需要所有部分(注意Nom_Giac被添加到FK定义中):

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

Another solution, as per Mark M's answer is to make the ID_Reunion column a Key (note that Nom_Giac is removed from the PK definition): 根据Mark M的回答,另一个解决方案是将ID_Reunion列设为Key(请注意Nom_Giac已从PK定义中删除):

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

This will make ID_Reunion a Key (the Primary Key, in fact) which can then be referenced with foreign key references Reunion(ID_Reunion) . 这将使ID_Reunion成为Key(实际上是主键),然后可以使用foreign key references Reunion(ID_Reunion)

Happy coding! 快乐的编码!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM