簡體   English   中英

MySQL 工作台正向工程師錯誤 1215:無法添加外鍵約束

[英]MySQL Workbench Forward Engineer Error 1215: Cannot add foreign key constraint

當我執行此腳本以創建 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,我檢查了外鍵列的數據類型,並確保我在引用表中使用了主鍵。

有任何想法嗎?

也許父表中的列是INT UNSIGNED

它們在兩個表中的數據類型必須完全相同。

外鍵約束

可能出現外鍵約束錯誤的原因:

  1. 您正在嘗試引用目標表上不存在的鍵。 確保它是另一個表上的鍵(它可以是primary or unique key ),

  2. 列的類型不同(引用表上的列可以為nullable除外)。

列定義必須在父表和子表中匹配。

家長

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

參考MySQL:外鍵約束

En la tabla part_finder users設置:

id INT UNSIGNED NOT NULL AUTO_INCREMENT

  • Campo llamado id de Tipo entero (sin asignar longitud del campo) no acepta valores nulos y el valor es auto incrementable。

En la tabla part_finder customer se tiene el campo store el cual es utilizado para hacer referencia a la tabla 用戶。

store INT(10) NOT NULL

  • Campo llamado store de tipo entero, con una longitud definida de 10 caracteres y no acepta valores nulos。

前路; se observa que los campos de las diferentes tablas, que se quieren relacionar entre sí, no son iguales, es por ello que no se podrá crear la llave foránea。

Es importante que contengan las mismas características。

Ejemlo 更正: part_finder users PK id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT

part_finder customer FK store INT(10) UNSIGNED NOT NULL

重要提示: Para evitar este Tipo de Problemas,es recomendable siempre especificar la longitud de cada campo。 Mysql puede asignar de forma automática 10 u 11 caracteres si se deja predeterminado el tipo de dato INT en un campo。 Por lo que en unas tablas puede asignar la longitud 10 y en otras 11. Como no se especifico de forma explicita, no sabremos que longitud tiene cada campo y se podría 一般錯誤1215:無法添加外鍵約束

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM