Creating a complex foreign key from two different primary key tables in oracle

table1:

tid (primary key) // there are no foreign keys

table2:

sid (primary key) // there are no foreign keys

Table3:

 Tid
 Sid
 iid(primary key)
 foreign key(Tid,Sid) references table1(tid).table2(sid)

 In table3 i want to make a composite foreign key or composite foreign key constraint but failed . there are many questions related to this .But none of them seems helpful to me . How can i do that ? Is it valid ? Then what is the syntax of making composite foreign key from two different tables primary key   
+4
source share
1 answer

It is not possible to have one foreign key referring to fields in different tables, and this makes no sense. A foreign key of two or more fields implies that the combination of field values ​​must correspond to one record of the table referenced, and this is not possible if the specified fields are in different tables.

What you can do is create two different keys for both tables:

CREATE TABLE table3(
    iid NUMBER,
    Tid NUMBER,
    Sid NUMBER,
    CONSTRAINT pk    PRIMARY KEY (iid) USING INDEX TABLESPACE idx,
    CONSTRAINT fk001 FOREIGN KEY (tid) REFERENCES table1(tid),
    CONSTRAINT fk002 FOREIGN KEY (sid) REFERENCES table2(sid)
);
+1
source

Source: https://habr.com/ru/post/1648145/


All Articles