为复合主键创建外键
Creating a foreign key to compound primary key
我有两个 tables - 教育工作者 和 教师:
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)
)
table Faculties
有一个由两列(InstitutionUserId
和 FacultyId
)组成的复合主键。我在 Educators
table 中也有相同的专栏。我想 link 这两个 table 和一个外键。
所以,这是我的查询:
ALTER TABLE [dbo].[Educators] WITH CHECK
ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId]
FOREIGN KEY ([FacultyId], [InstitutionUserId])
REFERENCES [dbo].[Faculties] ([FacultyId], [InstitutionUserId])
但我收到此错误消息:
Msg 1776, Level 16, State 0, Line 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'.
Msg 1750, Level 16, State 1, Line 7
Could not create constraint or index. See previous errors.
如何解决这个问题?
键的顺序应与定义的顺序相同:
ALTER TABLE dbo.Educators
ADD CONSTRAINT FK_Educators_FacultyId_Faculties_FacultyId
FOREIGN KEY(InstitutionUserId, FacultyId)
REFERENCES dbo.Faculties(InstitutionUserId, FacultyId);
您的表及其约束看起来不错。我能看到的唯一问题是主键中列的顺序
CONSTRAINT [PK_UserFaculties] PRIMARY KEY CLUSTERED
(
[InstitutionUserId] ASC,
[FacultyId] ASC
)
与您在外键约束中声明的顺序不同
ALTER TABLE [dbo].[Educators] WITH CHECK ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId] FOREIGN KEY([FacultyId], [InstitutionUserId])
REFERENCES [dbo].[Faculties] ([FacultyId], [InstitutionUserId])
尝试像这样更改外键声明中列的顺序:
ALTER TABLE [dbo].[Educators] WITH CHECK ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId] FOREIGN KEY([InstitutionUserId], [FacultyId])
REFERENCES [dbo].[Faculties] ([InstitutionUserId], [FacultyId])
我有两个 tables - 教育工作者 和 教师:
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)
)
table Faculties
有一个由两列(InstitutionUserId
和 FacultyId
)组成的复合主键。我在 Educators
table 中也有相同的专栏。我想 link 这两个 table 和一个外键。
所以,这是我的查询:
ALTER TABLE [dbo].[Educators] WITH CHECK
ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId]
FOREIGN KEY ([FacultyId], [InstitutionUserId])
REFERENCES [dbo].[Faculties] ([FacultyId], [InstitutionUserId])
但我收到此错误消息:
Msg 1776, Level 16, State 0, Line 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'.Msg 1750, Level 16, State 1, Line 7
Could not create constraint or index. See previous errors.
如何解决这个问题?
键的顺序应与定义的顺序相同:
ALTER TABLE dbo.Educators
ADD CONSTRAINT FK_Educators_FacultyId_Faculties_FacultyId
FOREIGN KEY(InstitutionUserId, FacultyId)
REFERENCES dbo.Faculties(InstitutionUserId, FacultyId);
您的表及其约束看起来不错。我能看到的唯一问题是主键中列的顺序
CONSTRAINT [PK_UserFaculties] PRIMARY KEY CLUSTERED
(
[InstitutionUserId] ASC,
[FacultyId] ASC
)
与您在外键约束中声明的顺序不同
ALTER TABLE [dbo].[Educators] WITH CHECK ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId] FOREIGN KEY([FacultyId], [InstitutionUserId])
REFERENCES [dbo].[Faculties] ([FacultyId], [InstitutionUserId])
尝试像这样更改外键声明中列的顺序:
ALTER TABLE [dbo].[Educators] WITH CHECK ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId] FOREIGN KEY([InstitutionUserId], [FacultyId])
REFERENCES [dbo].[Faculties] ([InstitutionUserId], [FacultyId])