简体   繁体   English

MySQL在单个表上有多个外键约束

[英]MySQL multiple foreign key constraints on single table

I have 2 unrelated tables but each one has the same column type I called 'somefield' 我有2个不相关的表,但每个表都具有称为“ somefield”的相同列类型

CREATE TABLE `table1` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `somefield` varchar(255) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`),
 UNIQUE KEY `somefield` (`somefield`),
)

CREATE TABLE `table2` (
 `id` int(5) NOT NULL AUTO_INCREMENT,
 `somefield` int(20) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `somefield` (`somefield`)
)

After I insert into either of these tables, I insert into this table the 'somefield' value. 在插入这些表中的任何一个之后,将“ somefield”值插入该表中。

CREATE TABLE `table3` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `somefield` varchar(255) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`),
 UNIQUE KEY `somefield` (`somefield`),
 CONSTRAINT `FK_table3` FOREIGN KEY (`somefield`) REFERENCES `table1` (`somefield`) ON DELETE CASCADE ON UPDATE CASCADE
)

At the moment, if i delete a row in table1, the same row is deleted in table3. 目前,如果我在table1中删除了一行,则在table3中将删除同一行。 I can insert into table1 and do an insert on table3 without any problems but if i insert into table2 and try to insert into table3 i get this error 我可以插入table1并在table3上进行插入而没有任何问题,但是如果我插入table2并尝试插入到table3中,则会出现此错误

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 违反完整性约束:1452无法添加或更新子行:外键约束失败

I know this is to do with the foreign key constraint in that it expects the 'somefield' value to be present in table1. 我知道这与外键约束有关,因为它希望table1中存在“ somefield”值。 What I want to know is how to add another constraint. 我想知道的是如何添加另一个约束。

ALTER TABLE table3 ADD CONSTRAINT FK_table3 FOREIGN KEY (`somefield`) REFERENCES `table2` (`somefield`)  ON DELETE CASCADE ON UPDATE CASCADE;

Would i need to create 2 tables to act as a go between for table1/table3 and table2/table3? 我是否需要创建2个表以充当table1 / table3和table2 / table3之间的路径?

I was able to sort this by creating a table which the other 3 tables all link to. 通过创建一个表,其他所有3个表都链接到此表,我能够对它进行排序。

If a row is deleted in the new table, the resulting rows are deleted in the other tables. 如果在新表中删除了一行,则结果行将在其他表中删除。

CREATE TABLE IF NOT EXISTS `table1` (
  `id` tinyint(3) NOT NULL AUTO_INCREMENT,
  `somefield` varchar(12) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `somefield` (`somefield`)
);

CREATE TABLE IF NOT EXISTS `table2` (
  `id` tinyint(3) NOT NULL AUTO_INCREMENT,
  `somefield` varchar(12) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `somefield` (`somefield`)
);

CREATE TABLE IF NOT EXISTS `table3` (
  `id` tinyint(3) NOT NULL AUTO_INCREMENT,
  `somefield` varchar(12) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `somefield` (`somefield`)
);

CREATE TABLE IF NOT EXISTS `supertable` (
  `somefield` varchar(12) NOT NULL DEFAULT '',
  PRIMARY KEY (`somefield`),
  UNIQUE KEY `somefield` (`somefield`)
);

ALTER TABLE `table1`
  ADD CONSTRAINT `FK_table1` FOREIGN KEY (`somefield`) REFERENCES `supertable` (`somefield`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `table2`
  ADD CONSTRAINT `FK_table2` FOREIGN KEY (`somefield`) REFERENCES `supertable` (`somefield`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `table3`
  ADD CONSTRAINT `FK_table3` FOREIGN KEY (`somefield`) REFERENCES `supertable` (`somefield`) ON DELETE CASCADE ON UPDATE CASCADE;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM