I’m having issues creating a simple join table (associative table, etc. whatever your flavor is).
These are test tables, so there is not much to them.
First table is
CREATE TABLE dbo.CareerField(
CareerFieldID int IDENTITY(1,1) NOT NULL,
CareerFieldName varchar(100) NOT NULL
)
Second Table is
CREATE TABLE dbo.Cluster(
ClusterID int IDENTITY(1,1) NOT NULL,
ClusterName varchar(100) NOT NULL
)
Third table (join table) to create a many-to-many relationship is
CREATE TABLE dbo.CareerField_Cluster(
CareerFieldID int NOT NULL,
ClusterID int NOT NULL
)
I am trying to set foreign keys in this third table using the following
ALTER TABLE dbo.CareerField_Cluster
ADD CONSTRAINT FK_CareerField_Cluster_CareerFieldID
FOREIGN KEY(CareerFieldID) REFERENCES dbo.CareerField(CareerFieldID)
ALTER TABLE dbo.CareerField_Cluster
ADD CONSTRAINT FK_CareerField_Cluster_ClusterID
FOREIGN KEY(ClusterID) REFERENCES dbo.Cluster(ClusterID)
However, I keep getting an error of
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table ‘dbo.CareerField’ that match the referencing column list in the foreign key ‘FK_CareerField_Cluster_CareerFieldID’.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
I try setting both fields as a primary key, but it will not allow me to select a separate table when creating the key – I can’t select CareerFieldID to reference CareerField Table and then ClusterID to reference Cluster Table.
I’ve not had this issue with MySQL and I am new to SQL Server. Any help is much appreciated.
For each of your tables
CareerFieldandCluster, ensure you have a PK designated via theCONSTRAINTdirective.