2012-05-21 125 views
1

我有兩個表:SQL,兩個外鍵引用另一個表的相同主鍵

Person(personID, name, phone, email); 
Relation(child_personID, parent_playerID); 

的關係表有助於識別兒童和他們的父母,但要做到這一點,將personId從人表必須是引用兩次作爲國外。我該如何去做這件事?

回答

2

可能看起來像這樣。

create table Person 
(
    personID int primary key, 
    name varchar(50), 
    phone varchar(50), 
    email varchar(50) 
) 

create table Relation 
(
    child_personID int references Person(personID), 
    parent_playerID int references Person(personID), 
    primary key (child_personID, parent_playerID) 
) 
+0

那麼這將返回來自關係表的記錄,不管哪一列匹配? – holaSenor

+0

不確定你的意思。這是具有外鍵約束的表結構,它確保您不將行添加到'Person'中沒有相關行的'relation'表中。要返回行,您需要查詢以及查詢返回的行取決於您如何編寫查詢。 –

相關問題