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)
What is the reason here that I am getting the error?
I saw the answer.
Reasons you may get a foreign key constraint error:
1.You are not using InnoDB as the engine on all tables.
2.You are trying to reference a nonexistent key on the target table. Make sure it is a key on the other table (it can be a primary or unique key).
3.The types of the columns are not the same (exception is the column on the referencing table can be nullable).
- One of the reasons may also be that the column you are using for ON DELETE SET NULL is not defined to be null. So make sure that the column is set default null.
I checked all those points and they are fine for me.
Here is the code I run before creating the table:
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`));
See Question&Answers more detail:
os