简体   繁体   English

SQL Server:引用表中没有与外键'FK'中的引用列列表匹配的主键或候选键

[英]SQL Server : there are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key 'FK'

I've been trying to solve this error for several minutes but I don't know what am I missing from table definition. 我一直试图解决这个错误几分钟,但我不知道我在表定义中缺少什么。

The code for the tables are below: 表格的代码如下:

Table Autocare : Autocare

CREATE TABLE [dbo].[Autocare] 
(
     [IDAutocar]    NUMERIC (18, 0) NOT NULL,
     [IDTipAutocar] NUMERIC (18, 0) NOT NULL,

     PRIMARY KEY CLUSTERED ([IDAutocar] ASC, [IDTipAutocar] ASC),

     CONSTRAINT [FK_Autogari_TipAutocar] 
        FOREIGN KEY ([IDTipAutocar]) 
        REFERENCES [dbo].[TipAutocar] ([IDTipAutocar])
);

Table Curse : Curse

CREATE TABLE [dbo].[Curse]
(
    [IDCursa] NUMERIC (18, 0) NOT NULL PRIMARY KEY,
    [IDTraseu] NUMERIC (18, 0) NOT NULL, 
    [Data] TIMESTAMP NOT NULL, 
    [IDCompanie] NUMERIC (18, 0) NOT NULL, 
    [NrLocuri] NUMERIC (18, 0) NOT NULL, 
    [IDAutocar] NUMERIC (18, 0) NOT NULL, 

    CONSTRAINT [FK_Curse_Trasee] 
       FOREIGN KEY ([IDTraseu]) 
       REFERENCES [Trasee]([IDTraseu]), 
    CONSTRAINT [FK_Curse_Companii] 
       FOREIGN KEY ([IDCompanie]) 
       REFERENCES [Companii]([IDCompanie]), 
    CONSTRAINT [FK_Curse_Autocare] 
       FOREIGN KEY ([IDAutocar]) 
       REFERENCES [Autocare]([IDAutocar])
)

When I try to execute the second script I get the following error (and I know is something related to the relationship between tables) and I don't figure out where the problem might be. 当我尝试执行第二个脚本时,我得到以下错误(我知道是与表之间的关系有关的东西),我不知道问题可能在哪里。 I'm a newbie into C# and SQL Server, so please forgive if I asked a stupid question. 我是C#和SQL Server的新手,所以如果我问了一个愚蠢的问题,请原谅。

Msg 1776, Level 16, State 0, Line 1 Msg 1776,Level 16,State 0,Line 1
There are no primary or candidate keys in the referenced table 'Autocare' that match the referencing column list in the foreign key 'FK_Curse_Autocare'. 引用的表'Autocare'中没有主键或候选键与外键'FK_Curse_Autocare'中的引用列列表匹配。

Msg 1750, Level 16, State 0, Line 1 Msg 1750,Level 16,State 0,Line 1
Could not create constraint. 无法创建约束。 See previous errors. 查看以前的错误。

Your table Autocare has a compound primary key made up from two columns : 您的表Autocare具有由两列组成的复合主键:

PRIMARY KEY CLUSTERED ([IDAutocar] ASC, [IDTipAutocar] ASC),

Therefore, any table that wishes to reference Autocare must also provide both columns in their foreign key! 因此,任何希望引用Autocare 必须Autocare键中提供两列

So this will obviously not work: 因此,这显然不会工作:

CONSTRAINT [FK_Curse_Autocare] 
   FOREIGN KEY ([IDAutocar]) 
   REFERENCES [Autocare]([IDAutocar])

since it references only one of the two columns of the primary key of Autocare . 因为它只引用的主键的两列的一个 Autocare

You need to add that second column IDTipAutocar to your Curse table and include it in your foreign key: 您需要将第二列IDTipAutocar添加到您的Curse表并将其包含在您的外键中:

CONSTRAINT [FK_Curse_Autocare] 
   FOREIGN KEY ([IDAutocar], [IDTipAutocar]) 
   REFERENCES [Autocare]([IDAutocar], [IDTipAutocar])

Your foreign keys must always reference the WHOLE primary key - not just parts of it. 您的外键必须始终引用WHOLE主键 - 而不仅仅是它的一部分。

A foreign key must reference a unique key, be it primary or not. 外键必须引用唯一键,无论是否为主键。 In your current supplied DDL, the unique identifier (primary key) in Autocare is the combination of IDAutocar and IDTipAutocar . 在当前提供的DDL中, Autocare的唯一标识符(主键)是IDAutocarIDTipAutocar的组合。 On the other hand, you have Curse referencing Autocare.IDAutocar , which is not unique. 另一方面,你有Curse引用Autocare.IDAutocar ,它不是唯一的。

You can either add IDTipAutocar to Curse (and its foreign key definition): 您可以将IDTipAutocar添加到Curse (及其外键定义):

CREATE TABLE [dbo].[Curse] (
    [IDCursa] NUMERIC (18, 0) NOT NULL PRIMARY KEY,
    [IDTraseu] NUMERIC (18, 0) NOT NULL, 
    [Data] TIMESTAMP NOT NULL, 
    [IDCompanie] NUMERIC (18, 0) NOT NULL, 
    [NrLocuri] NUMERIC (18, 0) NOT NULL, 
    [IDAutocar] NUMERIC (18, 0) NOT NULL, 
    [IDTipAutocar] NUMERIC (18, 0) NOT NULL,

    CONSTRAINT [FK_Curse_Trasee] 
        FOREIGN KEY ([IDTraseu]) 
        REFERENCES [Trasee]([IDTraseu]), 
    CONSTRAINT [FK_Curse_Companii]
        FOREIGN KEY ([IDCompanie])
        REFERENCES [Companii]([IDCompanie]), 
    CONSTRAINT [FK_Curse_Autocare]
        FOREIGN KEY ([IDAutocar], [IDTipAutocar]) 
        REFERENCES [Autocare]([IDAutocar], [IDTipAutocar])
)

Alternatively, you could remove IDTipAutocar from Autocare 's primary key definition: 或者,您可以从Autocare的主键定义中删除IDTipAutocar

CREATE TABLE [dbo].[Autocare] 
(
     [IDAutocar]    NUMERIC (18, 0) NOT NULL,
     [IDTipAutocar] NUMERIC (18, 0) NOT NULL,

     PRIMARY KEY CLUSTERED ([IDAutocar] ASC),

     CONSTRAINT [FK_Autogari_TipAutocar] 
        FOREIGN KEY ([IDTipAutocar]) 
        REFERENCES [dbo].[TipAutocar] ([IDTipAutocar])
);

Which ever makes more sense from your business logic's perspective. 从业务逻辑的角度来看,这更有意义。

暂无
暂无

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

相关问题 引用表中没有主键或候选键…与外键中的引用列列表匹配 - There are no primary or candidate keys in the referenced table … that match the referencing column list in the foreign key 被引用表“ ”中没有与外键中的引用列列表匹配的主键或候选键 - There are no primary or candidate keys in the referenced table ' ' that match the referencing column list in the foreign key 被引用表中没有与外键中的引用列列表匹配的主键或候选键 - No primary or candidate keys in the referenced table that match the referencing column list in the foreign key 被引用表中没有与外键中的引用列列表匹配的主键或候选键 - There are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key 引用表中没有与外键中的列列表匹配的主键或候选键 - No Primary or candidate keys in the referenced table that match column list in foreign key 我无法添加外键:被引用表中没有与外键中的引用列列表匹配的主键或候选键 - I can't add foreign key:There are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key 在参考表''中没有与参考列匹配的主键或候选键 - There are no primary or candidate keys in the referenced table '' that match the referencing column 引用表“factIngresosCurso”中没有与引用列匹配的主键或候选键 - There are no primary or candidate keys in the referenced table 'factIngresosCurso' that match the referencing column SQL Server:引用的表中没有主键或候选键 - SQL Server : there are no primary or candidate keys in the referenced table 无法创建外键“引用表中没有主键或候选键” - Not able to create foreign key “There are no primary or candidate keys in the referenced table”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM