所以Subthing是一种概括。东西要么是用金属制成的,要么用木材制成,它不能只是一个副东西。这是创建代码:
CREATE TABLE Thing (
ThingID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE Subthing (
consists_of INTEGER REFERENCES Thing(ThingID),
SubthingID INTEGER NOT NULL,
PRIMARY KEY (SubthingID, consists_of)
);
CREATE TABLE Wood (
Wstuff VARCHAR(40) NOT NULL,
consists_of INTEGER NOT NULL,
SubthingID INTEGER NOT NULL,
FOREIGN KEY (SubthingID, consists_of) REFERENCES Subthing(SubthingID, consists_of),
PRIMARY KEY (SubthingID, consists_of)
);
CREATE TABLE Dust (
DustID INTEGER PRIMARY KEY NOT NULL
);
CREATE TABLE Metal (
Mstuff VARCHAR(40) NOT NULL,
consists_of INTEGER NOT NULL,
SubthingID INTEGER NOT NULL,
requires INTEGER NOT NULL REFERENCES Dust(DustID),
FOREIGN KEY (SubthingID, consists_of) REFERENCES Subthing(SubthingID, consists_of),
PRIMARY KEY (SubthingID, consists_of)
);
我想要的是Metal同时拥有一个引用(SubthingID,composed_of)的主键和一个带有requires的外键,引用DustID。事实之后,我无法将其声明为外键,因为(SubthingID,composed_of)已经是外键。 你怎么能解决这个问题,除了把要求变成一张桌子?
答案 0 :(得分:0)
没关系,我太傻了。
CREATE TABLE Metal (
Mstuff VARCHAR(40) NOT NULL,
consists_of INTEGER NOT NULL,
SubthingID INTEGER NOT NULL,
requires INTEGER NOT NULL,
FOREIGN KEY (requires) REFERENCES Dust(DustID),
FOREIGN KEY (SubthingID, consists_of) REFERENCES Subthing(SubthingID, consists_of),
PRIMARY KEY (SubthingID, consists_of)
);
工作得很好。