当我执行此脚本来创建2个表时,CUSTOMER表中的STORE列引用USERS表中的ID列(两列都是INT):
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `part_finder` DEFAULT CHARACTER SET utf8 ;
USE `part_finder` ;
-- -----------------------------------------------------
-- Table `part_finder`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `part_finder`.`users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`account` MEDIUMINT(7) UNSIGNED NOT NULL COMMENT 'Account organisation user belongs to',
`name` VARCHAR(32) NULL DEFAULT NULL,
`passenc` VARCHAR(32) NULL DEFAULT NULL,
`email` VARCHAR(55) NULL DEFAULT NULL,
`rank` DECIMAL(1,0) NULL DEFAULT '0',
`ip_reg` VARCHAR(15) NULL DEFAULT NULL,
`ip_visit` VARCHAR(15) NULL DEFAULT NULL,
`dtreg` INT(11) NOT NULL,
`dtvisit` INT(11) NOT NULL,
`visits` SMALLINT(5) UNSIGNED NULL DEFAULT '0',
`pass` VARCHAR(25) NULL DEFAULT NULL,
`make_filter_on` TINYINT(1) NULL DEFAULT FALSE,
`brand_filter_on` TINYINT(1) NULL DEFAULT FALSE,
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 3;
-- -----------------------------------------------------
-- Table `part_finder`.`customer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `part_finder`.`customer` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`store` INT(10) NOT NULL,
`name` VARCHAR(32) NULL,
`address` VARCHAR(45) NULL,
`address_2` VARCHAR(45) NULL,
`city` VARCHAR(15) NULL,
`state` TINYINT UNSIGNED NOT NULL,
`zip` CHAR(5) NULL,
`phone` VARCHAR(15) NULL,
`website` VARCHAR(45) NULL,
`email` VARCHAR(55) NULL,
PRIMARY KEY (`id`),
INDEX `fk_customer_users_idx` (`store` ASC),
CONSTRAINT `fk_customer_users`
FOREIGN KEY (`store`)
REFERENCES `part_finder`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
我收到此错误:
ERROR: Error 1215: Cannot add foreign key constraint
-- -----------------------------------------------------
-- Table `part_finder`.`customer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `part_finder`.`customer` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`store` INT(10) NOT NULL,
`name` VARCHAR(32) NULL,
`address` VARCHAR(45) NULL,
`address_2` VARCHAR(45) NULL,
`city` VARCHAR(15) NULL,
`state` TINYINT UNSIGNED NOT NULL,
`zip` CHAR(5) NULL,
`phone` VARCHAR(15) NULL,
`website` VARCHAR(45) NULL,
`email` VARCHAR(55) NULL,
PRIMARY KEY (`id`),
INDEX `fk_customer_users_idx` (`store` ASC),
CONSTRAINT `fk_customer_users`
FOREIGN KEY (`store`)
REFERENCES `part_finder`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 6 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
我使用InnoDB,我检查了外键列的数据类型,并确保我在引用的表格中使用主键。
有什么想法吗?
答案 0 :(得分:6)
父表中的列可能是INT UNSIGNED
?
两个表中的数据类型必须完全相同。
您可能会遇到外键约束错误的原因:
您正尝试在目标表上引用不存在的键。
确保它是另一张桌子上的钥匙(可以是primary or unique key
),
列的类型不相同(例外是列
在参考表上可以是nullable
)。
答案 1 :(得分:0)
列定义必须在父表和子表中都匹配。
家长
CREATE TABLE IF NOT EXISTS `part_finder`.`users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, -- <----- this is unsigned
...
PRIMARY KEY (`id`))
儿童
CREATE TABLE IF NOT EXISTS `part_finder`.`customer` (
...
`store` INT(10) NOT NULL, -- <----------- this is signed
...
INDEX `fk_customer_users_idx` (`store` ASC),
CONSTRAINT `fk_customer_users`
FOREIGN KEY (`store`)
REFERENCES `part_finder`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
更改:
`store` INT(10) NOT NULL, -- <----------- this is signed
致:
`store` INT(10) UNSIGNED NOT NULL, -- <---- now this too is unsigned