Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL innodb - Foreign Key: only 1st works?

I have this example bellow where customers_b cannot be created. Error Code 1005 / errno: 121. However, if I create customers_b first than customers_a, then customers_a is the one which won't be created.

What is wrong? Why I cant link more than one FK to the PK 'id_state'? Thanks!

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';

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;
like image 990
Azevedo Avatar asked Mar 05 '14 20:03

Azevedo


People also ask

Does InnoDB support foreign keys?

InnoDB does not currently support foreign keys for tables with user-defined partitioning. This includes both parent and child tables.

Can a foreign key appear more than once?

It cannot be a duplicate, meaning the same value should not appear more than once in the table. A table can have more than one primary key. Primary key can be defined at the column or the table level.

Are foreign keys indexed by default in MySQL?

Foreign keys are not automatically indexed in MySQL*.


2 Answers

You can't use the same name fk_state for both constraints. Give one of them a different name.

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;
like image 152
Barmar Avatar answered Sep 18 '22 09:09

Barmar


Your both tables constraint name is same CONSTRAINT fk_state. for customer_b change that name like below

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;
like image 29
Rahul Avatar answered Sep 20 '22 09:09

Rahul