ALTER
оператор за множество FOREIGN KEYS
не работи.
Всеки CONSTRAINT
трябва да се добавят индивидуално :
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
Ако генерираното от системата име на ограничение не е желано, тогава използвайте тези;
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);
може да е за предпочитане.
P.S. :
- Не използвайте
NOT NULL
сPRIMARY KEY
,PRIMARY KEY
вече включваNOT NULL
. - Дължината не може да бъде дефинирана за
INT
колона.