我有一张桌子
TABLE PARTECIPATION
(PARTY_ID INT,
GUEST ...
PRIMARY KEY(PARTY_ID, GUEST)
FOREIGN KEY(PARTY_ID) REFERENCES PARTY(ID)
FOREIGN KEY(GUEST) REFERENCES GUEST(GUEST)
)
还有一张客人桌
TABLE GUEST
(NAME VARCHAR(20),
SURNAME VARCHAR(20),
AGE INT,
PRIMARY KEY(NAME, SURNAME)
)
现在,我如何将PARTECIPATION的GUEST元素设置为GUEST TABLE的外键?
我应该在GUEST中创建一个名为idk,ID的列并将其设置为主键吗?或者我可以做这样的事情:
TABLE PARTECIPATION
(PARTY_ID INT,
GUEST_NAME VARCHAR(20),
GUEST_SURNAME VARCHAR(20)
PRIMARY KEY(PARTY_ID, GUEST_NAME, GUEST_SURNAME)
FOREIGN KEY(PARTY_ID) REFERENCES PARTY(ID),
FOREIGN KEY(GUEST_NAME) REFERENCES GUEST(NAME),
FOREIGN KEY(GUEST_SURNAME) REFERENCES GUEST(SURNAME))
答案 0 :(得分:0)
外键必须具有与其引用的主键相同的列数和相同的顺序。这意味着,如果PK具有多个列,则必须将多个列放入一个外键定义中。
TABLE PARTECIPATION
(PARTY_ID INT,
GUEST_NAME VARCHAR(20),
GUEST_SURNAME VARCHAR(20)
PRIMARY KEY(PARTY_ID, GUEST_NAME, GUEST_SURNAME)
FOREIGN KEY(PARTY_ID) REFERENCES PARTY(ID),
FOREIGN KEY(GUEST_NAME, GUEST_SURNAME) REFERENCES GUEST(NAME, SURNAME))