错误代码:1215。无法添加外键约束
我不确定我是否会遇到此问题,这是有问题的两个表格,任何帮助将不胜感激!
CREATE TABLE IF NOT EXISTS `PHL_db`.`Session` (
`year` INT NOT NULL,
`season` VARCHAR(50) NOT NULL,
`division` VARCHAR(25) NOT NULL,
PRIMARY KEY (`year`, `season`, `division`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `PHL_db`.`Team` (
`name` VARCHAR(100) NOT NULL DEFAULT 'Free Agent',
`Session_season` VARCHAR(50) NOT NULL,
`Session_year` INT NOT NULL,
`Session_division` VARCHAR(25) NOT NULL,
PRIMARY KEY (`name`, `Session_season`, `Session_year`, `Session_division`),
FOREIGN KEY (`Session_season` , `Session_year` , `Session_division`)
REFERENCES `PHL_db`.`Session` ( `year`, `season` , `division`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
答案 0 :(得分:0)
您正试图将主键用作外键,其中主键不能用作同一张表上的外键。
答案 1 :(得分:0)
FOREIGN KEY (`Session_season` , `Session_year` , `Session_division`)
REFERENCES `PHL_db`.`Session` ( `year`, `season` , `division`)
应该是
FOREIGN KEY (`Session_year`, `Session_season` , `Session_division`)
REFERENCES `PHL_db`.`Session` ( `year`, `season` , `division`)
外键的列必须与引用的主键的列匹配。它们必须相同:
您可能还想阅读这份清单,以了解我协助提供的外键问题:MySQL Creating tables with Foreign Keys giving errno: 150