我有這個示例bellow其中customers_b不能創建。 錯誤代碼1005/errno:121. 但是,如果我首先創建customers_b而不是customers_a,那麼customers_a是不會創建的。MySQL innodb - 外鍵:只有第一個作品?
出了什麼問題?爲什麼我不能將多個FK鏈接到PK'id_state'? 謝謝!
SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS `testdb` ;
CREATE SCHEMA IF NOT EXISTS `testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `testdb` ;
-- -----------------------------------------------------
-- Table `testdb`.`state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`state` ;
CREATE TABLE IF NOT EXISTS `testdb`.`state` (
`id_state` INT NOT NULL,
`abbr` CHAR(2) NOT NULL,
PRIMARY KEY (`id_state`),
UNIQUE INDEX `id_state_UNIQUE` (`id_state` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testdb`.`customers_a`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`customers_a` ;
CREATE TABLE IF NOT EXISTS `testdb`.`customers_a` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `testdb`.`state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testdb`.`customers_b`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`customers_b` ;
CREATE TABLE IF NOT EXISTS `testdb`.`customers_b` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `testdb`.`state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
@DaImTo在執行任何插入操作之前創建表時發生錯誤。 – Barmar