当前位置: 首页 > 知识库问答 >
问题:

在SQL中拥有子类外键的正确方法?

郦祺
2023-03-14

护士通过请购单向三种不同供应品中的一种订购医疗供应品,所有供应品均由供应商提供。

护士

由于项目可以是三种类型中的一种,并且一个项目的申请可能还不存在,所以申请表具有3种供应类型的外键。

问题是:我正确列出的外键都指向3个不同的表,除了其中一个之外,每个条目都没有相应的外键。

我得到以下错误:

第1行错误:ORA-02091:事务回滚
ORA-02291:违反完整性约束(MMM1339.ITEMNO\u PHAR\u FK)-未找到父密钥

CREATE TABLE SUPPLIER
(SUPPLIERNO   INT,
 SUPPLIERNAME VARCHAR2(100),
 PHONENO VARCHAR2(12),
 ADDRESS VARCHAR(100),
 FAXNO VARCHAR(12),
 CONSTRAINT SUPPLIERNO_SSPL_PK PRIMARY KEY(SUPPLIERNO));

CREATE TABLE SUPPLIES_PHARMACEUTICAL
(ITEMNO         INT,
SUPPLIERNO      INT,
NAME            VARCHAR2(25),
DESCRIPTION     VARCHAR2(25),
QUANTITYINSTOCK INT,
REORDERLEVEL    INT,
COSTPERUNIT     DECIMAL(6,2),
DOSAGE          VARCHAR2(12),
CONSTRAINT ITEMNO_PHAR_PK PRIMARY KEY(ITEMNO));

CREATE TABLE SUPPLIES_SURGICAL
(ITEMNO         INT,
NAME            VARCHAR2(25),
DESCRIPTION     VARCHAR2(25),
QUANTITYINSTOCK INT,
REORDERLEVEL    INT,
COSTPERUNIT     DECIMAL(6,2),
SUPPLIERNO      INT,
CONSTRAINT ITEMNO_SUP_PK PRIMARY KEY(ITEMNO));

CREATE TABLE SUPPLIES_NONSURGICAL
(ITEMNO         INT,
NAME            VARCHAR2(25),
DESCRIPTION     VARCHAR2(25),  
QUANTITYINSTOCK INT,
REORDERLEVEL    INT,
COSTPERUNIT     DECIMAL(6,2),
SUPPLIERNO      INT,
CONSTRAINT ITEMNO_NONSURG_PK PRIMARY KEY(ITEMNO));

CREATE TABLE STAFF_CHARGENURSE
(STAFFNO   INT,
 ADDRESS   VARCHAR2(25),
 POSITION  VARCHAR2(12),
 BUDGET    DECIMAL(6,2),
 SPECIALTY VARCHAR2(12),
 CONSTRAINT STAFFNO_CHNURSE_PK PRIMARY KEY(STAFFNO));

 CREATE TABLE REQUISITION
 (REQNO INT,
  STAFFNO INT,
  STAFFNAME VARCHAR2(25),
  WARDNO INT,
  ITEMNO INT,
  QUANTITY INT,
  DATEORDERED DATE,
  DATERECIEVED DATE,
  CONSTRAINT REQ_PK PRIMARY KEY(REQNO));

外键:

 ALTER TABLE SUPPLIES_PHARMACEUTICAL
     ADD CONSTRAINT SUPPLIERNO_PHA_FK FOREIGN KEY(SUPPLIERNO) REFERENCES SUPPLIER(SUPPLIERNO)
     DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE SUPPLIES_SURGICAL
     ADD CONSTRAINT SUPPLIERNO_SURG_FK FOREIGN KEY(SUPPLIERNO) REFERENCES SUPPLIER(SUPPLIERNO)
     DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE SUPPLIES_NONSURGICAL
     ADD CONSTRAINT SUPPLIERNO_NONSURG_FK FOREIGN KEY(SUPPLIERNO) REFERENCES SUPPLIER(SUPPLIERNO)
     DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE REQUISITION
     ADD CONSTRAINT STAFFNO_REQ_FK FOREIGN KEY(STAFFNO) REFERENCES STAFF_CHARGENURSE(STAFFNO)
     DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE REQUISITION
     ADD CONSTRAINT ITEMNO_PHAR_FK FOREIGN KEY(ITEMNO) REFERENCES SUPPLIES_PHARMACEUTICAL(ITEMNO)
     DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE REQUISITION
     ADD CONSTRAINT ITEMNO_SURG_FK FOREIGN KEY(ITEMNO) REFERENCES SUPPLIES_SURGICAL(ITEMNO)
     DEFERRABLE INITIALLY DEFERRED;
 ALTER TABLE REQUISITION
     ADD CONSTRAINT ITEMNO_NONSURG_FK FOREIGN KEY(ITEMNO) REFERENCES SUPPLIES_NONSURGICAL(ITEMNO)
     DEFERRABLE INITIALLY DEFERRED;

测试数据:

 INSERT INTO REQUISITION VALUES(1, 20, 'Julie Wood', 8, 888520, 2, '27-FEB-2018', '15-MAR-2018');
 INSERT INTO REQUISITION VALUES(2, 20, 'Julie Wood', 8, 923956, 1, '25-FEB-2018', '28-FEB-2018');
 INSERT INTO REQUISITION VALUES(3, 21, 'Sarah Michaels', 7, 054802, 3, '20-FEB-2018', '22-FEB-2018');

 INSERT INTO SUPPLIES_PHARMACEUTICAL VALUES (823456, 100001, 'Zanax', 'Anti Depressant', 8, 2, 100.50, '50mg');
 INSERT INTO SUPPLIES_PHARMACEUTICAL VALUES (923956, 100001, 'Zupridol', 'Blood Pressure Treatment', 12, 5, 50, '20mg');
 INSERT INTO SUPPLIES_PHARMACEUTICAL VALUES (3952, 200001, 'Amibreezax', 'Artificial Ear Wax', 2, 1, 200, '5g');
 INSERT INTO SUPPLIES_PHARMACEUTICAL VALUES (4955, 200001, 'Ambridax', 'Skin Treatment', 5, 10, 20, '2mg');

 INSERT INTO SUPPLIES_SURGICAL VALUES (54802, 'Scalpel', 'Surgical Tool', 20, 10, 200.42, 100001);
 INSERT INTO SUPPLIES_SURGICAL VALUES (634520, 'Stitches', 'Suture Tool', 100, 10, 2.50, 200001);

 INSERT INTO SUPPLIES_NONSURGICAL VALUES (888520, 'Cart', '5ftx2ftx3ft', 2, 0, 200.00, 100001);
 INSERT INTO SUPPLIES_NONSURGICAL VALUES (423, 'Tool Holder', 'Holds Inspection Equip.', 4, 2, 50.00, 100001);

 INSERT INTO STAFF_CHARGENURSE VALUES(20, '32 Stark St. Portland, OR', 'Charge Nurse', 8000.99, 'Head Trauma');
 INSERT INTO STAFF_CHARGENURSE VALUES(21, '18 Wilson Rd Portland, OR', 'Charge Nurse', 6000, 'Epidermus');

 INSERT INTO SUPPLIER VALUES (100001,'Company A', '503-222-3333', '100 SE Stark Rd Portland, OR', '503-666-4444');
 INSERT INTO SUPPLIER VALUES (200001,'Company B', '666-333-4444', '500 SE Bilerica Rd Akron, OH', '666-444-3333');

 COMMIT;

共有2个答案

万承志
2023-03-14

这是您使用@William Robertson的想法修改的原始代码(大写,小写:修改):{1}使用单个SUPPLIES表,{2}使用is_外科列,{3}使用pharma_剂量表。也许你喜欢它。。。

CREATE TABLE SUPPLIER
(SUPPLIERNO   integer,
 SUPPLIERNAME VARCHAR2(100),
 PHONENO VARCHAR2(12),
 ADDRESS VARCHAR(100),
 FAXNO VARCHAR(12),
 CONSTRAINT SUPPLIERNO_SSPL_PK PRIMARY KEY(SUPPLIERNO));

-- one table instead of 3
create table supplies (
  ITEMNO          integer,
  SUPPLIERNO      integer,
  NAME            VARCHAR2(25),
  DESCRIPTION     VARCHAR2(25),
  QUANTITYINSTOCK integer,
  REORDERLEVEL    integer,
  COSTPERUNIT     number(6,2),
  is_surgical     varchar2(1) not null, 
  constraint supplies_surgical_yn_chk check( is_surgical in ('Y','N') )
, constraint supplies_pk primary key( itemno )
, constraint supplies_fk foreign key( supplierno )
  references supplier( supplierno )
);

create table pharma_dosage ( 
  itemno          integer
, dosage          varchar2( 64 ) not null 
, constraint pharma_supplies_fk foreign key( itemno )
  references supplies( itemno )
, constraint pharma_supplies_pk primary key( itemno )
);

另外两张表,以及-注释掉的-我们不需要的东西:

-- not needed
-- CREATE TABLE SUPPLIES_PHARMACEUTICAL
-- CREATE TABLE SUPPLIES_SURGICAL
-- CREATE TABLE SUPPLIES_NONSURGICAL
CREATE TABLE STAFF_CHARGENURSE
(STAFFNO   integer,          
 ADDRESS   VARCHAR2(25),
 POSITION  VARCHAR2(12),
 BUDGET    DECIMAL(6,2),
 SPECIALTY VARCHAR2(12),
 CONSTRAINT STAFFNO_CHNURSE_PK PRIMARY KEY(STAFFNO));

 CREATE TABLE REQUISITION
 (REQNO integer,             
  STAFFNO integer,           
  STAFFNAME VARCHAR2(25),
  WARDNO integer,            
  ITEMNO integer,            
  QUANTITY integer,         
  DATEORDERED DATE,
  DATERECIEVED DATE,
  CONSTRAINT REQ_PK PRIMARY KEY(REQNO),
  constraint req_fk foreign key ( itemno ) references supplies ( itemno )
);
-- not needed
-- ALTER TABLE SUPPLIES_PHARMACEUTICAL ADD CONSTRAINT SUPPLIERNO_PHA_FK 
-- ALTER TABLE SUPPLIES_SURGICAL ADD CONSTRAINT SUPPLIERNO_SURG_FK
-- ALTER TABLE SUPPLIES_NONSURGICAL ADD CONSTRAINT SUPPLIERNO_NONSURG_FK 
ALTER TABLE REQUISITION ADD CONSTRAINT STAFFNO_REQ_FK 
FOREIGN KEY(STAFFNO) REFERENCES STAFF_CHARGENURSE(STAFFNO) DEFERRABLE INITIALLY DEFERRED;
-- not needed
-- ALTER TABLE REQUISITION ADD CONSTRAINT ITEMNO_PHAR_FK 
-- ALTER TABLE REQUISITION ADD CONSTRAINT ITEMNO_SURG_FK 
-- ALTER TABLE REQUISITION ADD CONSTRAINT ITEMNO_NONSURG_FK 

插入

-- parents first
begin
  INSERT INTO STAFF_CHARGENURSE VALUES(20, '32 Stark St. Portland, OR', 'Charge Nurse', 8000.99, 'Head Trauma');
  INSERT INTO STAFF_CHARGENURSE VALUES(21, '18 Wilson Rd Portland, OR', 'Charge Nurse', 6000, 'Epidermus');

  INSERT INTO SUPPLIER VALUES (100001,'Company A', '503-222-3333', '100 SE Stark Rd Portland, OR', '503-666-4444');
  INSERT INTO SUPPLIER VALUES (200001,'Company B', '666-333-4444', '500 SE Bilerica Rd Akron, OH', '666-444-3333');
end;
/
PL/SQL procedure successfully completed.

更多测试数据

begin
-- pharmaceutical
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical )
VALUES (823456, 100001, 'Zanax', 'Anti Depressant', 8, 2, 100.50, 'N');
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical )
VALUES (923956, 100001, 'Zupridol', 'Blood Pressure Treatment', 12, 5, 50, 'N');
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical )
VALUES (3952, 200001, 'Amibreezax', 'Artificial Ear Wax', 2, 1, 200, 'N');
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical )
VALUES (4955, 200001, 'Ambridax', 'Skin Treatment', 5, 10, 20, 'N');
-- pharma_dosage
insert into pharma_dosage ( itemno, dosage ) values ( 823456, '50mg' ) ;
insert into pharma_dosage ( itemno, dosage ) values ( 923956, '20mg' ) ;
insert into pharma_dosage ( itemno, dosage ) values ( 3952, '5g' ) ;
insert into pharma_dosage ( itemno, dosage ) values ( 4955, '2mg' ) ;
-- surgical
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical )
VALUES (54802, 100001, 'Scalpel', 'Surgical Tool', 20, 10, 200.42, 'Y');
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical ) 
VALUES (634520, 200001, 'Stitches', 'Suture Tool', 100, 10, 2.50, 'Y');
-- nonsurgical
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical)
VALUES (888520, 100001, 'Cart', '5ftx2ftx3ft', 2, 0, 200.00,  'N');
INSERT INTO SUPPLIES( itemno, supplierno, name, description, quantityinstock
, reorderlevel, costperunit, is_surgical )
VALUES (423, 100001,'Tool Holder', 'Holds Inspection Equip.', 4, 2, 50.00,  'N');
-- requisition
INSERT INTO REQUISITION VALUES(1, 20, 'Julie Wood', 8, 888520, 2, '27-FEB-2018', '15-MAR-2018');
INSERT INTO REQUISITION VALUES(2, 20, 'Julie Wood', 8, 923956, 1, '25-FEB-2018', '28-FEB-2018');
INSERT INTO REQUISITION VALUES(3, 21, 'Sarah Michaels', 7, 054802, 3, '20-FEB-2018', '22-FEB-2018');
end ;
/
PL/SQL procedure successfully completed.

现在,有些人选择。。。

SQL> select * from supplier ;
SUPPLIERNO  SUPPLIERNAME  PHONENO       ADDRESS                       FAXNO         
100001      Company A     503-222-3333  100 SE Stark Rd Portland, OR  503-666-4444  
200001      Company B     666-333-4444  500 SE Bilerica Rd Akron, OH  666-444-3333  

SQL> select * from supplies ;
ITEMNO  SUPPLIERNO  NAME         DESCRIPTION               QUANTITYINSTOCK  REORDERLEVEL  COSTPERUNIT  IS_SURGICAL  
823456  100001      Zanax        Anti Depressant           8                2             100.5        N            
923956  100001      Zupridol     Blood Pressure Treatment  12               5             50           N            
3952    200001      Amibreezax   Artificial Ear Wax        2                1             200          N            
4955    200001      Ambridax     Skin Treatment            5                10            20           N            
54802   100001      Scalpel      Surgical Tool             20               10            200.42       Y            
634520  200001      Stitches     Suture Tool               100              10            2.5          Y            
888520  100001      Cart         5ftx2ftx3ft               2                0             200          N            
423     100001      Tool Holder  Holds Inspection Equip.   4                2             50           N             

SQL> select * from requisition;
REQNO  STAFFNO  STAFFNAME       WARDNO  ITEMNO  QUANTITY  DATEORDERED  DATERECIEVED  
1      20       Julie Wood      8       888520  2         27-FEB-18    15-MAR-18     
2      20       Julie Wood      8       923956  1         25-FEB-18    28-FEB-18     
3      21       Sarah Michaels  7       54802   3         20-FEB-18    22-FEB-18     

SQL> select * from staff_chargenurse;
STAFFNO  ADDRESS                    POSITION      BUDGET   SPECIALTY    
20       32 Stark St. Portland, OR  Charge Nurse  8000.99  Head Trauma  
21       18 Wilson Rd Portland, OR  Charge Nurse  6000     Epidermus    

SQL> select * from pharma_dosage;
ITEMNO  DOSAGE  
823456  50mg    
923956  20mg    
3952    5g      
4955    2mg   

版本2

如果仍然希望有一个“超类型”表和3个“子类型”表,请查看以下DDL。你可能会找到一个介于两者之间的解决方案。(下面是“概念验证”代码,省略了几列和一些原始表格。)

create table supplies (
  supplierno number primary key
, category varchar2( 16 ) 
, constraint unique_parentcategory unique ( supplierno, category )
, constraint check_category check (
    category in ( 'surgical', 'non-surgical', 'pharmaceutical' )
  )
);

然后

create table supplies_pharmaceutical (
  supplierno number primary key
, category varchar2( 16 )
-- 
-- more "specific" columns here 
--
, constraint check_category1 check ( category in ( 'pharmaceutical' ) )
, constraint s_p_fk foreign key ( supplierno, category ) 
  references supplies ( supplierno, category )
) ; 

create table supplies_nonsurgical (
  supplierno number primary key
, category varchar2( 16 )
-- 
-- more "specific" columns here 
--
, constraint check_category2 check ( category in ( 'non-surgical' )  )
, constraint s_n_fk foreign key ( supplierno, category ) 
  references supplies ( supplierno, category )
) ; 

create table supplies_surgical (
  supplierno number primary key
, category varchar2( 16 )
-- 
-- more "specific" columns here 
--
, constraint check_category3 check ( category in ( 'surgical' )  )
, constraint s_s_fk foreign key ( supplierno, category ) 
  references supplies ( supplierno, category ) 
) ; 

测试数据:

begin
  insert into supplies( supplierno, category ) values ( 1000, 'pharmaceutical' ) ;
  insert into supplies( supplierno, category ) values ( 2000, 'non-surgical' ) ;
  insert into supplies( supplierno, category ) values ( 3000, 'surgical' ) ;

  insert into supplies_pharmaceutical( supplierno, category ) 
    values ( 1000, 'pharmaceutical' ) ;
  insert into supplies_nonsurgical( supplierno, category ) 
    values ( 2000, 'non-surgical' ) ;
  insert into supplies_surgical( supplierno, category ) 
    values ( 3000, 'surgical' ) ;
end;
/

-- must fail:
insert into supplies ( supplierno, category ) values ( 1001, 'food' ) ;

insert into supplies_pharmaceutical( supplierno, category ) 
  values ( 2000, 'pharmaceutical' ) ;
程天佑
2023-03-14

正如在回答您的其他相关问题时所提到的,没有超类和子类。SUPPLIES_PHARMACEUTICALSUPPLIES_SURGICALSUPPLIES_NONSURGICAL只是彼此没有连接的表。

所有启用的约束都被强制执行。将它们定义为可推迟只是将验证推迟到下一次提交,这不会真正改变示例中的任何内容。如果你在一个列上定义了三个约束,就没有语法或机制说“只需要强制执行其中一个约束”,我也不知道这样的系统是如何工作的。

数据建模中,您可以使用

  1. 单个表(例如,提供了一个类别或类似的指示符列,或

然后,根据需要,其他表可以具有引用父项或其子项之一的FK约束。

 类似资料:
  • 问题内容: 我正在尝试在react-admin项目中使用多个dataproviders,但出现错误: 我有这样的: 我有文件: 这是正确的方法吗? 问题答案: 您不是在调用找到的对象,而是在从数组中调用映射对象。您可以这样解决:

  • 问题内容: 所以可以说我有这个界面: 我有一个实现它的类: 如果我想使用接口IBox,则无法以这种方式实际创建它的实例: 对?所以我实际上必须这样做: 如果是这样,那么接口的唯一目的是确保实现接口的类中具有接口所描述的正确方法?还是接口还有其他用途? 问题答案: 接口是使代码更灵活的一种方法。你要做的是: 然后,稍后,如果你决定要使用其他类型的框(也许还有另一个库,其中框的类型更好),则将代码切换

  • 问题内容: 我刚刚在我的项目中发现了一些这样的sql查询构建: 这是否达到其目标,即减少内存使用量? 我对此表示怀疑,因为在构造函数中使用了“ +”(字符串concat运算符)。像下面的代码那样,将占用与使用String相同的内存量吗?我了解,使用时有所不同。 两条语句的内存使用量是否相等?请澄清。 提前致谢! 编辑: 顺便说一句, 这不是我的代码 。在一个旧项目中发现它。而且,查询的大小不如我的

  • 我有两个已经存在于postgres中的表,我们称之为表A和表B。表B的一列有一个外键约束,因为它必须是表A的主键。因此,B和A之间存在多对一的关系,表B中的多条记录对应于表A中的一条记录。 这两个表的实体定义如下。 表B的实体定义如下: 其中userId是映射到表a中的主键user_id的外键。这些约束已经在底层postgres数据库中定义了,所以我没有考虑使用@ManyToOne注释关系(仍然试

  • 我更新了我的尝试,将一些内容保留为CHAR,但仍然得到一个类似的错误:第1行错误:ORA-02091:事务回滚ORA-02291:违反完整性约束(MMM1339.ITEMNO_PHAR_FK)-未找到父密钥 需求的外键都在不同的供应主键中有值,所以我不确定为什么仍然存在问题。

  • 问题内容: 我一直在查看Spark的文档,其中提到了这一点: Spark的API在很大程度上依赖于在驱动程序中传递函数以在集群上运行。有两种推荐的方法可以做到这一点: 匿名函数语法,可用于简短的代码段。全局单例对象中的静态方法。例如,您可以定义对象MyFunctions,然后传递MyFunctions.func1,如下所示: 请注意,虽然也可以在类实例中传递对方法的引用(与单例对象相对),但这需要