MySQL多列引用相同的列

时间:2014-09-04 14:53:26

标签: mysql foreign-keys

我有一个有5列的表作为[id,s1,s2,s3,n] 和名为s

的表的s1,s2,s3和n引用id列

但是MySQL拒绝添加外键约束。我试图搜索,但没有解决方案的工作。我尝试设置索引仍然无法正常工作。 SQL

CREATE TABLE IF NOT EXISTS `char` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `s1` int(11) NOT NULL,
  `s2` int(11) NOT NULL,
  `s3` int(11) NOT NULL,
  `n` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

ALTER TABLE `landuse`.`char` 
  ADD CONSTRAINT `FK_Char_S1`
  FOREIGN KEY (`s1` )
  REFERENCES `landuse`.`s` (`id` )
  ON DELETE CASCADE
  ON UPDATE CASCADE,  
  ADD CONSTRAINT `FK_Char_S2`
  FOREIGN KEY (`s2` )
  REFERENCES `landuse`.`s` (`id` )
  ON DELETE CASCADE
  ON UPDATE CASCADE, 
  ADD CONSTRAINT `FK_Char_S3`
  FOREIGN KEY (`s3` )
  REFERENCES `landuse`.`s` (`id` )
  ON DELETE CASCADE
  ON UPDATE CASCADE, 
  ADD CONSTRAINT `FK_Char_N`
  FOREIGN KEY (`n` )
  REFERENCES `landuse`.`s` (`id` )
  ON DELETE CASCADE
  ON UPDATE CASCADE;

//当我执行上述语句时,出现以下错误

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails
   (`landuse`.`#sql-14ec_2`, CONSTRAINT `FK_Char_S2` FOREIGN KEY (`s2`) REFERENCES `s` 
   (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

Link to MySQL dump of the current database 我该如何设置外键?

0 个答案:

没有答案