ALTER
sætning for en flere FOREIGN KEYS
virker ikke.
Hver CONSTRAINT
skal tilføjes individuelt :
CREATE TABLE Gust ( Gust_ID INT PRIMARY KEY, First_Name VARCHAR(50), Last_Name VARCHAR(50), Email VARCHAR(20), phone_number INT, Address VARCHAR(30) );
CREATE TABLE Reservation ( Reservation_ID INT PRIMARY KEY, Start_Date Date, End_Date Date );
CREATE TABLE RoomType ( RoomType_ID INT NOT NULL PRIMARY KEY, Class VARCHAR(10), ExtraPrice INT );
CREATE TABLE Room(Room_ID int PRIMARY KEY, Price INT, Reservation_ID int,Gust_ID int );
ALTER TABLE Room ADD FOREIGN KEY (Room_ID) REFERENCES RoomType(RoomType_ID);
ALTER TABLE Room ADD FOREIGN KEY (Reservation_ID) REFERENCES Reservation(Reservation_ID);
-- the table Room is assumed to have a column Reservation_ID
ALTER TABLE Room ADD FOREIGN KEY (Gust_ID) REFERENCES Gust(Gust_ID);
-- the table Room is assumed to have a column Gust_ID
Hvis systemgenereret begrænsningsnavn ikke ønskes, så brug disse;
ALTER TABLE Room ADD CONSTRAINT fk_RoomType_ID FOREIGN KEY (Room_ID)
REFERENCES RoomType(RoomType_ID);
ALTER TABLE Room ADD CONSTRAINT fk_Reservation_ID FOREIGN KEY (Reservation_ID)
REFERENCES Reservation(Reservation_ID);
ALTER TABLE Room ADD CONSTRAINT fk_Gust_ID FOREIGN KEY (Gust_ID)
REFERENCES Gust(Gust_ID);
kan foretrækkes.
P.S. :
- Brug ikke
NOT NULL
medPRIMARY KEY
,PRIMARY KEY
indeholder alleredeNOT NULL
. - Længden kan ikke defineres for en
INT
kolonne.