我的数据库设计看起来像这样。简短形式:
CREATE TABLE LANDSLIDE (
LNUMBER SERIAL NOT NULL,
PRIMARY KEY (LNUMBER)
...
有4个表(像滑坡的一部分)
CREATE TABLE SCARP (
SCPNUM CHAR(7) NOT NULL,
LNUMBER SMALLINT NOT NULL,
FOREIGN KEY (LNUMBER) REFERENCES LANDSLIDE (LNUMBER) ON DELETE CASCADE,
...
);
e.g。 scpnum = scp0001
CREATE TABLE ACCUMULATION (
ACCUNUM CHAR(8) NOT NULL,
LNUMBER SMALLINT NOT NULL,
FOREIGN KEY (LNUMBER) REFERENCES LANDSLIDE (LNUMBER) ON DELETE CASCADE,
...
);
e.g。 accunum = accu0001
CREATE TABLE FRONT (
FRNUM CHAR(6) NOT NULL,
LNUMBER SMALLINT NOT NULL,
FOREIGN KEY (LNUMBER) REFERENCES LANDSLIDE (LNUMBER) ON DELETE CASCADE,
...
);
e.g。 frnum = fr0001
CREATE TABLE OTHER (
OTHERNUM CHAR(9) NOT NULL,
LNUMBER SMALLINT NOT NULL,
FOREIGN KEY (LNUMBER) REFERENCES LANDSLIDE (LNUMBER) ON DELETE CASCADE,
...
);
e.g。 othernum = other0001
还有另一张桌子,称为材料(不同的石头,不同部分的土壤)。
CREATE TABLE MATERIAL (
MATNUMBER VARCHAR(9) NOT NULL,
ROCK,
DISTRIBUTION,
...
PRIMARY KEY(MATNUMBER,ROCK,DISTRIBUTION,...),
CONSTRAINT material_matnumber_scarp_fkey FOREIGN KEY
(MATNUMBER) REFERENCES SCARP (SCPNUM) ON DELETE CASCADE,
CONSTRAINT material_matnumber_accumulation_fkey FOREIGN KEY
(MATNUMBER) REFERENCES ACCUMULATION (ACCUNUM) ON DELETE CASCADE,
CONSTRAINT material_matnumber_front_fkey FOREIGN KEY
(MATNUMBER) REFERENCES FRONT (FRNUM) ON DELETE CASCADE,
CONSTRAINT material_matnumber_other_fkey FOREIGN KEY
(MATNUMBER) REFERENCES OTHER (OTHERNUM) ON DELETE CASCADE
);
当我向材料插入数据时(在对其他表进行输入之后),它会返回错误。 错误:在表“material”上插入或更新违反外键约束“material_matnumber_accumulation_fkey” 详细信息:表“累积”中没有键(matnumber)=(scp0001)
表格材料应如下所示:
MATNUMBER | ROCK | DISTRIBUTION | ...
----------+------+--------------+------
scp0001 | A | lateral | ...
scp0001 | B | lateral | ...
accu0001 | B | central | ...
scp0002 | C | NULL |
accu0002 | A | lateral |
fr0002 | A | NULL |
scp0003 | B | cantral |
accu0003 | B | lateral |
other0003 | C | NULL |
每个条目都是唯一的,因为它的数量很多!
答案 0 :(得分:2)
如果我理解正确,这是一个常见的问题,可以用超类型/子类型模式解决。你可以在Material
表中有4个可以为空的FK列,但这是我的建议:
CREATE TABLE Landslide ( --- no change here
LNumber SERIAL NOT NULL,
PRIMARY KEY (LNumber)
...
你说:“有4个像(滑坡的一部分)的桌子”
因此,我们创建了一个额外的LandslidePart
表(这是我们的“超类型”表):
CREATE TABLE LandslidePart (
PartType CHAR(1) NOT NULL,
PartNumber INT NOT NULL,
LNumber SMALLINT NOT NULL,
PRIMARY KEY (PartType, PartNumber),
CHECK (PartType IN ('S', 'A', 'F', 'O')), --- the 4 subtypes
FOREIGN KEY (LNumber) REFERENCES Landslide (LNumber) ON DELETE CASCADE,
... --- other columns that are
--- common in all 4 tables
);
然后我们有4个(“子类型”)表,略有改动:
CREATE TABLE Scarp (
PartType CHAR(1) NOT NULL,
PartNumber INT NOT NULL,
PRIMARY KEY (PartType, PartNumber),
CHECK (PartType = 'S'),
FOREIGN KEY (PartType, PartNumber)
REFERENCES LandslidePart (PartType, PartNumber)
ON DELETE CASCADE,
... --- columns that are related to Scarp
);
CREATE TABLE Accumulation (
PartType CHAR(1) NOT NULL,
PartNumber INT NOT NULL,
PRIMARY KEY (PartType, PartNumber),
CHECK (PartType = 'A'),
FOREIGN KEY (PartType, PartNumber)
REFERENCES LandslidePart (PartType, PartNumber)
ON DELETE CASCADE,
... --- columns that are related to Accumulation
);
--- We define in a similar way the other 2 tables: "Front" and "Other"
现在我们可以定义引用“超类型”(Material
)表的LandslidePart
表,而不是四个中的任何一个:
CREATE TABLE Material (
PartType CHAR(1) NOT NULL,
MatNumber INT NOT NULL,
Rock,
Distribution,
...
PRIMARY KEY(PartType, MatNumber, Rock, Distribution, ...),
CONSTRAINT material_matnumber_landslidepart_fkey
FOREIGN KEY (PartType, MatNumber)
REFERENCES LandslidePart (PartType, PartNumber)
ON DELETE CASCADE,
);
答案 1 :(得分:-1)
检查您的外键长度和数据类型。
MATNUMBER VARCHAR(9) NOT NULL
MATERIAL
的以下所有引用
SCPNUM CHAR(7) NOT NULL, *should be* SCPNUM VARCHAR(9) NOT NULL,
ACCUNUM CHAR(8) NOT NULL *should be* ACCUNUM VARCHAR(9) NOT NULL
FRNUM CHAR(6) NOT NULL, *should be* FRNUM VarCHAR(9) NOT NULL,