我的数据库设计看起来像这样。简写:
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,
...
);
例如 scpnum = scp0001
CREATE TABLE ACCUMULATION (
ACCUNUM CHAR(8) NOT NULL,
LNUMBER SMALLINT NOT NULL,
FOREIGN KEY (LNUMBER) REFERENCES LANDSLIDE (LNUMBER) ON DELETE CASCADE,
...
);
例如 accumum = accu0001
CREATE TABLE FRONT (
FRNUM CHAR(6) NOT NULL,
LNUMBER SMALLINT NOT NULL,
FOREIGN KEY (LNUMBER) REFERENCES LANDSLIDE (LNUMBER) ON DELETE CASCADE,
...
);
例如 frnum = fr0001
CREATE TABLE OTHER (
OTHERNUM CHAR(9) NOT NULL,
LNUMBER SMALLINT NOT NULL,
FOREIGN KEY (LNUMBER) REFERENCES LANDSLIDE (LNUMBER) ON DELETE CASCADE,
...
);
例如 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”详细信息:表“accumulation”中不存在键(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 |
每个条目都是独一无二的,因为 matnumber!