Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 任何一个表上都必须存在外键_Sql_Plsql_Foreign Keys_Constraints_Check Constraints - Fatal编程技术网

Sql 任何一个表上都必须存在外键

Sql 任何一个表上都必须存在外键,sql,plsql,foreign-keys,constraints,check-constraints,Sql,Plsql,Foreign Keys,Constraints,Check Constraints,我正在尝试让以下场景正常工作: 我有三个表,其中一个是IncidentDetail,用来保存另外两个事件和PendEvent的事件信息。我希望IncidentDetail表引用incident中的某个事件,或者PendIncident表,以便它可以存在于任意位置。我将如何设置约束 表1-事故: 表2-未决事件: 表3-附带细节: IncidentDetail表将具有FK约束,因此IncidentDetail.IncidentNbr需要在Incident.IncidentNbr列或PendInci

我正在尝试让以下场景正常工作:

我有三个表,其中一个是IncidentDetail,用来保存另外两个事件和PendEvent的事件信息。我希望IncidentDetail表引用incident中的某个事件,或者PendIncident表,以便它可以存在于任意位置。我将如何设置约束

表1-事故:

表2-未决事件:

表3-附带细节:

IncidentDetail表将具有FK约束,因此IncidentDetail.IncidentNbr需要在Incident.IncidentNbr列或PendIncident.PendIncidentNbr列中具有值

是否可能对引用到两个不同表中的单个列具有FK约束,或者是否需要IncidentDetail表中的第二个PendIncidentNbr列对PendIncident.PendIncidentNbr具有自己的FK约束

这是否足以确保IncidentDetail表满足至少一个FK约束


我可以想到的另一种方法是将FK约束全部删除,并在IncidentDetail.IncidentNbr列或IncidentDetail.PendIncidentNbr列有值的情况下使用检查约束。

可以对引用到两个不同表中的单个列使用FK约束,但这不适用于您的使用案例

由于incidentNbr在任何给定的时间点存在于Incident表或PendIncident表中,因此在IncidentDetail表中具有两个FK约束将在您尝试在此子表中插入记录时失败。由于事件存在于一个父表中,而不存在于另一个父表中,因此它将抛出完整性约束冲突错误w.r.t.second FK

对于这种情况,使用检查约束可能是一种可行的解决方案

快速参考的代码段-

Create table table_a(col_a number primary key);
Create table table_b(col_b number primary key);
Create table table_c(col_c number);

ALTER TABLE table_c
ADD CONSTRAINT fk_c_a
  FOREIGN KEY (col_c)
  REFERENCES table_a(col_a);

ALTER TABLE table_c
ADD CONSTRAINT fk_c_b
  FOREIGN KEY (col_c)
  REFERENCES table_b(col_b);

Insert into table_a values(100);
Insert into table_b values(200);
Insert into table_c values(100); —-This statement will throw integrity constraint violation error

否,外键只能引用一个父表

您将需要事件详细信息中的两个单独的列,每个列都有自己的FK,或者将事件和PENDINCIDENT合并到具有类型或状态列的单个表中


您发现自己有一列似乎引用了两个父表中的任何一个,这一事实向我表明,在不同的处理状态下,它们可能实际上是相同的东西。

这是否回答了您的问题?
+------------------------+
| PendIncidentNbr | Desc |
+------------------------+
+-----------------------+
| IncidentNbr | Details |
+-----------------------+
Create table table_a(col_a number primary key);
Create table table_b(col_b number primary key);
Create table table_c(col_c number);

ALTER TABLE table_c
ADD CONSTRAINT fk_c_a
  FOREIGN KEY (col_c)
  REFERENCES table_a(col_a);

ALTER TABLE table_c
ADD CONSTRAINT fk_c_b
  FOREIGN KEY (col_c)
  REFERENCES table_b(col_b);

Insert into table_a values(100);
Insert into table_b values(200);
Insert into table_c values(100); —-This statement will throw integrity constraint violation error