Search code examples
oracleforeign-keysoracle-sqldeveloperoracle-apex

How to assign two foreign keys in child table ORACLE SQL?


How can I inherit from two(or more) parent classes in ORACLE SQL? I've tried something like this:

    ID(13),
    OTHER_ID(9),
    CONSTRAINT FK_ID FOREIGN 
    KEY(ID) REFERENCES TABLE_ONE(ID),
    CONSTRAINT FK_OTHER_GROUP FOREIGN 
    KEY(OTHER_ID) REFERENCES TABLE_TWO(OTHER_ID)

I've read the documentation and the code I've found is this one:

      INDEX (product_category, product_id),
      INDEX (customer_id),

      FOREIGN KEY (product_category, product_id)
      REFERENCES product(category, id)
      ON UPDATE CASCADE ON DELETE RESTRICT,

      FOREIGN KEY (customer_id)
      REFERENCES customer(id)

It doesn't quite work for me. Is there any special way to define these indexes? I haven't met them before. The first example had a problem that data types weren't specified. But I'd like someone to explain me how could I do it the second(oracle documentation) way.


Solution

  • This is how I understood the problem:

    • there are two tables, each having primary key constraint
      • in my example, those are t_emp and t_dept
    • there's also the third table whose columns are supposed to reference primary keys from previous two tables
      • those are fk_th_emp and fk_th_dep foreign key constraints

    If that's so, here's how to do that:

    SQL> create table t_emp
      2    (empno    number   constraint pk_temp primary key,
      3     ename    varchar2(20)
      4    );
    
    Table created.
    
    SQL> create table t_dept
      2    (deptno   number   constraint pk_dept primary key,
      3     dname    varchar2(20)
      4    );
    
    Table created.
    
    SQL> create table third
      2    (id       number   constraint pk_third primary key,
      3     other_id number,
      4     --
      5     constraint fk_th_emp foreign key (id)       references t_emp (empno),
      6     constraint fk_th_dep foreign key (other_id) references t_dept (deptno)
      7    );
    
    Table created.
    
    SQL>