2016-07-17 39 views
4

表1:產生從兩個不同的表主鍵在Oracle複合外鍵

TID(主鍵) //無外鍵這裏

表2:

SID (主鍵) //這裏也沒有外鍵

表3:

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 
+0

尚不清楚你想要達到的目標。僅僅爲兩個表創建兩個單獨的外鍵並不容易? – Carlo

+0

我想從兩個不同表中的主鍵創建一個外鍵 –

+0

它不能完成。您只能創建兩個單獨的外鍵,每個外鍵都引用給定表中的一個主鍵。 – krokodilko

回答

1

這是不可能發生在不同的表一個外鍵引用字段,它沒有任何意義可言。兩個或更多字段的外鍵意味着字段值的組合必須與被引用表的單個記錄相匹配,並且如果引用的字段位於不同的表上,則無法完成此操作。

你可以做的是創造兩個不同的foreing鍵的兩個表,如下:

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) 
);