我的表A是否应该从表B主键和通过经文中有外键?
我想知道我的以下代码,它可以作为我的主要键是serial_no,而外键是从我的计划表speguled_no中获取的。通过经文主键将进行计划,并从我的飞机表serial_no中取出外键。
我尝试为我的计划表插入值,但是它出现了一个错误,该错误无法从父键中获取数据,这是我的serial_no。
我应该在预定表中删除serial_no作为我的外键,还是有其他方法可以做?
/* CREATE TABLE FOR AIRPLANE */
CREATE TABLE AIRPLANE (
Serial_No Varchar2(10) NOT NULL,
Scheduled_No Varchar2(10) NOT NULL,
Flight_No Varchar2(6) NOT NULL,
Model_No Number(3) NOT NULL,
Capacity Number(3) NOT NULL,
Maintenance_Date DATE NULL,
PRIMARY KEY (Serial_No)
);
/* CREATE TABLE FOR SCHEDULED */
CREATE TABLE SCHEDULED (
Scheduled_No Varchar2(10) NOT NULL,
Serial_No Varchar2(10) NOT NULL,
Route_No Varchar2(10) NOT NULL,
Job_No Varchar2(10) NOT NULL,
Flight_Fly_On DATE NOT NULL,
PRIMARY KEY (Scheduled_No),
FOREIGN KEY (Serial_No) REFERENCES AIRPLANE (Serial_No)
);
/* ADDING FK FOR Scheduled_No FROM SCHEDULED */
ALTER TABLE AIRPLANE
ADD FOREIGN KEY (Scheduled_No) REFERENCES SCHEDULED (Scheduled_No);
I would like to know my below code, will it be able to work as my PRIMARY KEY for Airplane is Serial_No and FOREIGN KEY is taken from my Scheduled table, Scheduled_No. Via verse PRIMARY KEY for Scheduled will be Scheduled_No and FOREIGN KEY is taken from my Airplane table, Serial_No.
I try to insert value for my Scheduled table, but it gives an error that, is unable to take data from parent key which is my Serial_No.
Should I remove Serial_No as my FOREIGN KEY in the Scheduled table or is there any other way to do it?
/* CREATE TABLE FOR AIRPLANE */
CREATE TABLE AIRPLANE (
Serial_No Varchar2(10) NOT NULL,
Scheduled_No Varchar2(10) NOT NULL,
Flight_No Varchar2(6) NOT NULL,
Model_No Number(3) NOT NULL,
Capacity Number(3) NOT NULL,
Maintenance_Date DATE NULL,
PRIMARY KEY (Serial_No)
);
/* CREATE TABLE FOR SCHEDULED */
CREATE TABLE SCHEDULED (
Scheduled_No Varchar2(10) NOT NULL,
Serial_No Varchar2(10) NOT NULL,
Route_No Varchar2(10) NOT NULL,
Job_No Varchar2(10) NOT NULL,
Flight_Fly_On DATE NOT NULL,
PRIMARY KEY (Scheduled_No),
FOREIGN KEY (Serial_No) REFERENCES AIRPLANE (Serial_No)
);
/* ADDING FK FOR Scheduled_No FROM SCHEDULED */
ALTER TABLE AIRPLANE
ADD FOREIGN KEY (Scheduled_No) REFERENCES SCHEDULED (Scheduled_No);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有循环引用。
您应该弄清楚表所指的内容:
飞机
代表具有容量,唯一标识符(即尾巴上的数字)的物理对象,并且是特定的品牌和模型。计划
表示物理对象在某个时间点所做的工作。即在特定日期,它将飞行特定的路线。在这种情况下,
飞机
拥有单个scheduled_no
,因为有很多时间点,而且飞机可以在不同的时间内具有不同的时间表,因此您希望每个飞机及其时间表之间都有一对多的关系。飞机
表中不应有一个scheduled_no
列,并且由于不应该有一列,因此不应有外键约束。您只需要serial_no
返回另一个方向上的约束。删除该列和相应的约束将解决您的循环参考约束问题。You have circular references.
You should work out what your tables refer to:
AIRPLANE
represents the physical object which has a capacity, a unique identifier (i.e. the number on the tail) and is a particular make and model.SCHEDULE
represents what the physical object does at a point in time. I.e. on a particular date it will be flying a particular route.In this case, it does not make sense for an
AIRPLANE
to have a singleScheduled_No
as there can be many points in time and the airplane can have different schedules at different times so you want there to be a one-to-many relationship between each airplane and its schedules.There should not be a
Scheduled_No
column in theAIRPLANE
table and, since there shouldn't be a column, there should not be the foreign key constraint. You just want the constraint onSerial_No
going back in the other direction. Removing that column and the corresponding constraint will solve your issue of circular referential constraints.你能做到;修改外键约束(在
飞机
上),使其可延迟(即在提交时检查一切是否正常,而不是插入)。像这样的事情:
注意这个命令:
测试:
插入飞机
现在可以工作,尽管其父scheduled_no
行/值不存在Yet:在
scheduled
表中插入一行。外键必须匹配。也成功了;现在,提交:
You can do it; modify foreign key constraint (on
airplane
) so that it is deferrable (i.e. check whether everything is OK at time of commit, not insert).Something like this:
Pay attention to this command:
Testing:
insert into airplane
now works, although its parentscheduled_no
row / value doesn't exist yet:Insert a row into the
scheduled
table. Foreign keys must match.Succeeded as well; now, commit: