2014-03-05 42 views
5

我有這個示例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; 
+0

@DaImTo在執行任何插入操作之前創建表時發生錯誤。 – Barmar

回答

7

對於這兩個約束,您不能使用相同名稱fk_state。給他們其中一個不同的名字。

CREATE TABLE IF NOT EXISTS `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_b` 
FOREIGN KEY (`addr_state`) 
REFERENCES `state` (`id_state`) 
ON DELETE NO ACTION 
ON UPDATE NO ACTION) 
ENGINE = InnoDB; 
+0

你懂了!謝謝! – Azevedo

2

您的兩個表約束名稱是相同的CONSTRAINT fk_state。爲customer_b更改該名稱,如下所示

CREATE TABLE IF NOT EXISTS `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 `state` (`id_state`) 
ON DELETE NO ACTION 
ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


CREATE TABLE IF NOT EXISTS `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_state1` <-- Here 
FOREIGN KEY (`addr_state`) 
REFERENCES `state` (`id_state`) 
ON DELETE NO ACTION 
ON UPDATE NO ACTION) 
ENGINE = InnoDB;