Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than one key specified in column level FOREIGN KEY constraint

Tags:

sql

sql-server

I am doing a database project for my school and I stumble upon this strange issue that I can't seem to solve it. Here is the code i have when trying the create the tables:

Create table Offering(
StartDate       DATE            NOT NULL    ,       
PRIMARY KEY(StartDate) 
);

Create table OfferPerWeek(
StartDate       DATE            NOT NULL    ,
StartTime       TIME(0)         NOT NULL    ,
[Day]           VARCHAR(10)     NOT NULL
PRIMARY KEY (StartDate,[Day],StartTime)
FOREIGN KEY (StartDate) 
REFERENCES Offering(StartDate)
);

--The table that have the issues 
Create table OfferPerWeek_Venue(  
[Day]           VARCHAR(10)     NOT NULL    ,
StartDate       DATE            NOT NULL    , 
StartTime       TIME(0)         NOT NULL    
PRIMARY KEY (StartDate, [Day], StartTime)
FOREIGN KEY (StartDate, [Day], StartTime) 
REFERENCES OfferPerWeek (StartDate, [Day] , StartTime)
);

The error message said that:

More than one key specified in column level FOREIGN KEY constraint

I am really confused by the problem, I tried to create multiple foreign keys that references to that many primary key of the parent table.

Really hope that this can be fixed, thank you

like image 743
Xunrui Chow Avatar asked Dec 03 '25 09:12

Xunrui Chow


2 Answers

You are missing commas. Try:

Create table OfferPerWeek_Venue(  
[Day]           VARCHAR(10)     NOT NULL    ,
StartDate       DATE            NOT NULL    , 
StartTime       TIME(0)         NOT NULL , <-- comma there  
PRIMARY KEY (StartDate, [Day], StartTime), <-- comma there
FOREIGN KEY (StartDate, [Day], StartTime) 
REFERENCES OfferPerWeek (StartDate, [Day] , StartTime)
);

You can try the following, naming the foreign key's:

Create table OfferPerWeek_Venue(  
[Day]           VARCHAR(10)     NOT NULL    ,
StartDate       DATE            NOT NULL    , 
StartTime       TIME(0)         NOT NULL ,  
CONSTRAINT [PK_GoodName] PRIMARY KEY (StartDate, [Day], StartTime), 
CONSTRAINT [FK_GoodName] FOREIGN KEY (StartDate, [Day], StartTime) 
REFERENCES OfferPerWeek (StartDate, [Day] , StartTime)
);
like image 167
HoneyBadger Avatar answered Dec 04 '25 23:12

HoneyBadger


The CREATE TABLE statement is missing a comma before each CONSTRAINT

like image 32
Léo R. Avatar answered Dec 05 '25 01:12

Léo R.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!