简体   繁体   English

创建外键来复合主键

[英]Creating a foreign key to compound primary key

I have two tables - Educators and Faculties : 我有两个表格- 教育人员学院

CREATE TABLE [dbo].[Educators]
(
    [UserId] [nvarchar](128) NOT NULL,
    [FacultyId] [smallint] NOT NULL,
    [InstitutionUserId] [nvarchar](128) NOT NULL,

    CONSTRAINT [PK_Educators] 
        PRIMARY KEY CLUSTERED ([UserId] ASC)
)

CREATE TABLE [dbo].[Faculties]
(
    [InstitutionUserId] [nvarchar](128) NOT NULL,
    [FacultyId] [smallint] NOT NULL,

    CONSTRAINT [PK_UserFaculties] 
        PRIMARY KEY CLUSTERED ([InstitutionUserId] ASC, [FacultyId] ASC)
)

The table Faculties has a compound primary key made up from two columns ( InstitutionUserId and FacultyId ). Faculties具有由两列( InstitutionUserIdFacultyId )组成的复合主键。 I also have the same column in the Educators table. 我在Educators表中也有相同的列。 I want to link those two tables together with a foreign key. 我想将这两个表与一个外键链接在一起。

So, this is my query: 因此,这是我的查询:

ALTER TABLE [dbo].[Educators] WITH CHECK 
    ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId] 
    FOREIGN KEY ([FacultyId], [InstitutionUserId])
    REFERENCES [dbo].[Faculties] ([FacultyId], [InstitutionUserId])

But I getting this error message: 但是我收到此错误消息:

Msg 1776, Level 16, State 0, Line 7 消息1776,级别16,状态0,第7行
There are no primary or candidate keys in the referenced table 'dbo.Faculties' that match the referencing column list in the foreign key 'FK_Educators_FacultyId_Faculties_FacultyId'. 引用表'dbo.Faculties'中没有与外键'FK_Educators_FacultyId_Faculties_FacultyId'中的引用列列表匹配的主键或候选键。

Msg 1750, Level 16, State 1, Line 7 消息1750,第16级,状态1,第7行
Could not create constraint or index. 无法创建约束或索引。 See previous errors. 请参阅先前的错误。

How to solve this problem? 如何解决这个问题呢?

Your tables and their constraints look fine. 您的表及其约束看起来很好。 The only issue I can see is that the order of the columns in your primary key 我唯一看到的问题是主键中列的顺序

CONSTRAINT [PK_UserFaculties] PRIMARY KEY CLUSTERED 
(
    [InstitutionUserId] ASC,
    [FacultyId] ASC
)

is different from the order that you've declared in your foreign key constraint 与您在外键约束中声明的顺序不同

ALTER TABLE [dbo].[Educators]  WITH CHECK ADD  CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId] FOREIGN KEY([FacultyId], [InstitutionUserId])
REFERENCES [dbo].[Faculties] ([FacultyId], [InstitutionUserId])

Try changing the order of the columns in the declaration of your foreign key like this: 尝试像这样更改外键声明中的列顺序:

ALTER TABLE [dbo].[Educators]  WITH CHECK ADD  CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId] FOREIGN KEY([InstitutionUserId], [FacultyId])
REFERENCES [dbo].[Faculties] ([InstitutionUserId], [FacultyId])

The keys should be in the same order that they are defined: 密钥应与定义的顺序相同:

ALTER TABLE dbo.Educators
    ADD CONSTRAINT FK_Educators_FacultyId_Faculties_FacultyId
        FOREIGN KEY(InstitutionUserId, FacultyId)
            REFERENCES dbo.Faculties(InstitutionUserId, FacultyId);

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

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