我这里有4张桌子:
CREATE TABLE paper (
id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30)
) ENGINE =INNODB;
CREATE TABLE subscriber (
id VARCHAR(10) PRIMARY KEY,
name VARCHAR(20),
address VARCHAR(30),
suburb VARCHAR(20),
state VARCHAR(3),
postcode VARCHAR(4))
round_id INTEGER NOT NULL,
FOREIGN KEY (round_id) REFERENCES round (id)
ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE = INNODB;
CREATE TABLE current_order (
paper_id VARCHAR(20),
subscriber_id VARCHAR(10),
PRIMARY KEY (paper_id, subscriber_id),
FOREIGN KEY (paper_id) REFERENCES paper (id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY(subscriber_id) REFERENCES subscriber (id)
ON UPDATE CASCADE ON DELETE RESTRICT)
ENGINE = INNODB;
CREATE TABLE receipt (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
receipt_date DATE,
paid_till_date DATE,
paper_id VARCHAR(20),
subscriber_id VARCHAR(10))
ENGINE = INNODB;
现在表纸有一个主键ID,它由表current_order中的paper_id引用。 表订户具有主键ID,它由表current_order中的subscriber_id引用。 Tabe current_order具有复合主键(paper_id,subscriber_id)。 所以这三个表通过外键关系链接在一起。
如果我想将最后一张桌子收据与这三张桌子挂钩,我该怎么做?我的想法是 1:设置引用表current_order中的复合主键(paper_id,subscriber_id)的复合外键(paper_id,subscriber_id)。 2:分别在表current_order中设置两个单个外键(paper_id)和(subscriber_id)引用(paper_id)和)subscriber_id。
两种方法都不起作用,并且出现了错误1452:无法添加或更新子行。 所以我真的很想知道在表收据和表current_order之间设置关系的正确方法是什么? 这是E-R Digram:
表收据和表current_order之间有两个链接,我需要根据它设置关系。
答案 0 :(得分:0)
我不是mysql专家,但我会尝试这个:
CREATE TABLE receipt (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
receipt_date DATE,
paid_till_date DATE,
paper_id VARCHAR(20),
subscriber_id VARCHAR(10),
FOREIGN KEY (paper_id,subscriber_id) REFERENCES current_order (paper_id,subscriber_id)
ON UPDATE CASCADE ON DELETE RESTRICT)
ENGINE = INNODB;
有用吗?