gpt4 book ai didi

database - 向现有表添加外键

转载 作者:搜寻专家 更新时间:2023-10-30 22:06:45 26 4
gpt4 key购买 nike

我一直在尝试将这些键添加到我的表中,但出现错误

ORA-01735: invalid ALTER TABLE option

我的代码:

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 )

最佳答案

多个 FOREIGN KEYS

ALTER 语句不起作用。

每个 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);

可能是首选。

附言:

  • 不要将 NOT NULLPRIMARY KEY 一起使用,PRIMARY KEY 已经存在包括 NOT NULL
  • 不能为 INT 列定义长度。

关于database - 向现有表添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49886532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com