我一直在尝试将这些密钥添加到我的表中,但是我收到了错误
ORA-01735:ALTER TABLE选项无效
我的代码:
ALTER TABLE Room
ADD FOREIGN KEY (RoomType_ID) REFERENCES RoomType(RoomType_ID),
ADD FOREIGN KEY (Reservation_ID) REFERENCES Reservation(Reservation_ID),
ADD FOREIGN KEY (Gust_ID) REFERENCES Gust(Gust_ID);
表
CREATE TABLE Gust ( Gust_ID INT NOT NULL PRIMARY KEY, First_Name VARCHAR(50), Last_Name VARCHAR(50), Email VARCHAR(20), phone_number INT(10), Address VARCHAR(30) )
CREATE TABLE Reservation ( Reservation_ID INT NOT NULL PRIMARY KEY, Start_Date Date, End_Date Date )
CREATE TABLE Room ( Room_ID INT NOT NULL PRIMARY KEY, Price INT )
CREATE TABLE RoomType ( RoomType_ID INT NOT NULL PRIMARY KEY, Class VARCHAR(10), ExtraPrice INT )
答案 0 :(得分:1)
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
列定义长度。