Executing:
CREATE TABLE `calls`.`called` (
`date` DATETIME NULL,
`rate` VARCHAR(10) NULL,
`duration` TIME NULL,
`Name` VARCHAR(20) NOT NULL,
`Code` VARCHAR(10) NOT NULL,
`Number` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Name`, `Code`, `Number`),
INDEX `Code_idx` (`Code` ASC),
INDEX `Number_idx` (`Number` ASC),
CONSTRAINT `Name`
FOREIGN KEY (`Name`)
REFERENCES `calls`.`city` (`Name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Code`
FOREIGN KEY (`Code`)
REFERENCES `calls`.`city` (`Code`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Number`
FOREIGN KEY (`Number`)
REFERENCES `calls`.`subscriber` (`Number`)
ON DELETE CASCADE
ON UPDATE CASCADE);
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1215: Cannot add foreign key constraint
SQL Statement:
CREATE TABLE `calls`.`called` (
`date` DATETIME NULL,
`rate` VARCHAR(10) NULL,
`duration` TIME NULL,
`Name` VARCHAR(20) NOT NULL,
`Code` VARCHAR(10) NOT NULL,
`Number` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Name`, `Code`, `Number`),
INDEX `Code_idx` (`Code` ASC),
INDEX `Number_idx` (`Number` ASC),
CONSTRAINT `Name`
FOREIGN KEY (`Name`)
REFERENCES `calls`.`city` (`Name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Code`
FOREIGN KEY (`Code`)
REFERENCES `calls`.`city` (`Code`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Number`
FOREIGN KEY (`Number`)
REFERENCES `calls`.`subscriber` (`Number`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ERROR 1215: Cannot add foreign key constraint
SQL Statement:
CREATE TABLE `calls`.`called` (
`date` DATETIME NULL,
`rate` VARCHAR(10) NULL,
`duration` TIME NULL,
`Name` VARCHAR(20) NOT NULL,
`Code` VARCHAR(10) NOT NULL,
`Number` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Name`, `Code`, `Number`),
INDEX `Code_idx` (`Code` ASC),
INDEX `Number_idx` (`Number` ASC),
CONSTRAINT `Name`
FOREIGN KEY (`Name`)
REFERENCES `calls`.`city` (`Name`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Code`
FOREIGN KEY (`Code`)
REFERENCES `calls`.`city` (`Code`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Number`
FOREIGN KEY (`Number`)
REFERENCES `calls`.`subscriber` (`Number`)
ON DELETE CASCADE
ON UPDATE CASCADE)
我收到错误的原因是什么?
我看到了the回答。
您可能会收到外键约束错误的原因:
1.您没有将InnoDB用作所有表格的引擎。
2.您正在尝试在目标表上引用不存在的键。确保它是另一个表上的键(它可以是主键或唯一键)。
3.列的类型不相同(例外情况是引用表上的列可以为空)。
我检查了所有这些点,他们对我没问题。
以下是我在创建表格之前运行的代码:
CREATE SCHEMA `calls` ;
CREATE TABLE `calls`.`subscriber` (
`first name` VARCHAR(20) NULL,
`second name` VARCHAR(20) NULL,
`last name` VARCHAR(20) NULL,
`address` VARCHAR(20) NULL,
`Number` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Number`));
CREATE TABLE `calls`.`city` (
`Name` VARCHAR(20) NOT NULL,
`Code` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Name`, `Code`));
答案 0 :(得分:0)
我测试了你的表创建。
然后我获得了有关外键错误的更多信息:
mysql> show engine innodb status\G
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2018-02-20 14:51:33 700002d90000 Error in foreign key constraint of table calls/called:
FOREIGN KEY (`Code`)
REFERENCES `calls`.`city` (`Code`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Number`
FOREIGN KEY (`Number`)
REFERENCES `calls`.`subscriber` (`Number`)
ON DELETE CASCADE
ON UPDATE CASCADE):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
...
我发现了问题:您在列city
上的(Name, Code)
中有一个复合主键。为此,您必须创建一个外键约束,引用父主键的两个列。
像这样:
CONSTRAINT `Name`
FOREIGN KEY (`Name`, `Code`)
REFERENCES `calls`.`city` (`Name`, `Code`)
ON DELETE CASCADE
ON UPDATE CASCADE
不要为每列声明约束 - 声明一个引用键的两列的约束。