Ich habe zwei Tabellen - Educators und Fakultäten:einen Fremdschlüssel erstellen Verbindung Primärschlüssel
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)
)
Die Tabelle Faculties
hat eine Verbindung Primärschlüssel aus zwei Spalten aus (InstitutionUserId
und FacultyId
). Ich habe auch die gleiche Spalte in der Educators
Tabelle. Ich möchte diese beiden Tabellen mit einem Fremdschlüssel verknüpfen.
Also, das ist meine Frage:
ALTER TABLE [dbo].[Educators] WITH CHECK
ADD CONSTRAINT [FK_Educators_FacultyId_Faculties_FacultyId]
FOREIGN KEY ([FacultyId], [InstitutionUserId])
REFERENCES [dbo].[Faculties] ([FacultyId], [InstitutionUserId])
Aber ich diese Fehlermeldung bekommen:
Msg 1776, Ebene 16, Status 0, Zeile 7
Es gibt keine primären oder Kandidat Schlüssel in der referenzierten Tabelle 'dbo.Faculties', die mit der referenzierenden Spaltenliste im Fremdschlüssel 'FK_Educators_FacultyId_Faculties_FacultyId' übereinstimmen.Msg 1750, Ebene 16, Status 1, Zeile 7
Constraint oder Index konnte nicht erstellt werden. Siehe vorherige Fehler.
Wie löst man dieses Problem?