ALTER
istruzione per una multipla FOREIGN KEYS
non funziona.
Ogni CONSTRAINT
devono essere aggiunti singolarmente :
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
Se il nome del vincolo generato dal sistema non è desiderato, utilizzare questi;
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);
può essere preferito.
PS :
- Non utilizzare
NOT NULL
conPRIMARY KEY
,PRIMARY KEY
include giàNOT NULL
. - Non è possibile definire la lunghezza per un
INT
colonna.