考慮以下兩個SQL代碼段:一個多列VS單柱的多個外鍵外鍵
SQL代碼1:
CONSTRAINT FK_E
FOREIGN KEY (E1, E2, E3)
REFERENCES F (E1, E2, E3),
SQL代碼2:
CONSTRAINT FK_E1
FOREIGN KEY (E1)
REFERENCES F (E1),
CONSTRAINT FK_E2
FOREIGN KEY (E2)
REFERENCES F (E2),
CONSTRAINT FK_E3
FOREIGN KEY (E3)
REFERENCES F (E3),
我的問題是:SQL代碼1和SQL代碼2是否相等?即它們是否以同樣的方式在DBMS中解釋?如果不是,他們之間有什麼區別?
EDIT1:更具體考慮以下ER圖
是下面的SQL代碼是否正確?
CREATE TABLE E
(EID NUMBER PRIMARY KEY);
CREATE TABLE F
(FID NUMBER PRIMARY KEY);
CREATE TABLE G
(EID NUMBER,
FID NUMBER,
GNAME VARCHAR(50),
CONSTRAINT G_PK
PRIMARY KEY (EID, FID, GNAME),
CONSTRAINT FK_G_EID
FOREIGN KEY (EID)
REFERENCES E (EID),
CONSTRAINT FK_G_FID
FOREIGN KEY (FID)
REFERENCES F (FID)
);
CREATE TABLE R1
(EIDE NUMBER,
EIDG NUMBER,
FIDG NUMBER,
GNAME VARCHAR(50),
ATTR1 NUMBER,
CONSTRAINT PK_R1
PRIMARY KEY (EIDG, FIDG, GNAME),
CONSTRAINT FK_R1_EIDG_FIDG_GNAME
FOREIGN KEY (EIDG, FIDG, GNAME)
REFERENCES G (EID, FID, GNAME),
CONSTRAINT FK_R1_EIDE
FOREIGN KEY (EIDE)
REFERENCES E (EID)
);
CREATE TABLE R2
(FIDF NUMBER UNIQUE,
FIDG NUMBER,
EIDG NUMBER,
GNAME VARCHAR(50),
ATTR2 NUMBER,
CONSTRAINT PK_R2
PRIMARY KEY (EIDG, FIDG, GNAME),
CONSTRAINT FK_R2_EIDG_FIDG_GNAME
FOREIGN KEY (EIDG, FIDG, GNAME)
REFERENCES G (EID, FID, GNAME),
CONSTRAINT FK_R2_FIDF
FOREIGN KEY (FIDF)
REFERENCES F (FID)
);
EDIT2:FK_R1_EIDE和FK_R2_FIDF是多餘的嗎?
不一樣...相同的行與不同的行。 – jarlh
你能解釋一下嗎? – zigzag
將其簡化爲參考文獻F(E1,E2)並考慮有效的fk! – jarlh