2012-10-21 83 views
10
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'; 

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ; 
USE `mydb` ; 

-- ----------------------------------------------------- 
-- Table `mydb`.`restaurants` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`restaurants` (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `name` VARCHAR(128) NOT NULL , 
    `description` VARCHAR(1024) NOT NULL , 
    `address` VARCHAR(1024) NOT NULL , 
    `phone` VARCHAR(16) NOT NULL , 
    `url` VARCHAR(128) NOT NULL , 
    `min_order` INT NOT NULL , 
    `food_types` SET('pizza', 'sushi', 'osetian_pie') NOT NULL , 
    PRIMARY KEY (`id`) , 
    UNIQUE INDEX `name_UNIQUE` (`name` ASC) , 
    UNIQUE INDEX `id_UNIQUE` (`id` ASC)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mydb`.`regions` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`regions` (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `restaurant` INT NOT NULL , 
    `name` VARCHAR(128) NOT NULL , 
    PRIMARY KEY (`id`) , 
    INDEX `restaurant_idx` (`restaurant` ASC) , 
    UNIQUE INDEX `id_UNIQUE` (`id` ASC) , 
    CONSTRAINT `restaurant` 
    FOREIGN KEY (`restaurant`) 
    REFERENCES `mydb`.`restaurants` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mydb`.`food` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`food` (
    `id` INT NOT NULL , 
    `type` ENUM('pizza', 'sushi', 'osetian_pie') NOT NULL , 
    `name` VARCHAR(45) NOT NULL , 
    `ingredients` VARCHAR(256) NULL , 
    `image` VARCHAR(256) NOT NULL , 
    PRIMARY KEY (`id`) , 
    UNIQUE INDEX `id_UNIQUE` (`id` ASC)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mydb`.`food_variant` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`food_variant` (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `size` VARCHAR(16) NOT NULL , 
    `weight` VARCHAR(16) NOT NULL , 
    `price` INT NOT NULL , 
    `food` INT NOT NULL , 
    `restaurant` INT NOT NULL , 
    PRIMARY KEY (`id`) , 
    UNIQUE INDEX `id_UNIQUE` (`id` ASC) , 
    INDEX `food_idx` (`food` ASC) , 
    INDEX `restaurant_idx` (`restaurant` ASC) , 
    CONSTRAINT `food` 
    FOREIGN KEY (`food`) 
    REFERENCES `mydb`.`food` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `restaurant` 
    FOREIGN KEY (`restaurant`) 
    REFERENCES `mydb`.`restaurants` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 



SET [email protected]_SQL_MODE; 
SET [email protected]_FOREIGN_KEY_CHECKS; 
SET [email protected]_UNIQUE_CHECKS; 

Error is: 
    Executing SQL script in server 
    ERROR: Error 1005: Can't create table 'mydb.food_variant' (errno: 121) 

我看不到重複的約束。它在哪裏?無法在Workbench中創建表,errno 121

回答

20

這可能是因爲你有一個名爲至少一個約束具有相同標識符爲列:

/* You already have a column named `restaurant` in this table, 
    but are naming the FK CONSTRAINT `restaurant` also... */ 
CONSTRAINT `restaurant` 
    FOREIGN KEY (`restaurant`) 
    REFERENCES `mydb`.`restaurants` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 

應該使用不同的標識符約束像fk_restaurant爲:

CONSTRAINT `fk_restaurant` 
    FOREIGN KEY (`restaurant`) 
    REFERENCES `mydb`.`restaurants` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 

food表中同樣的東西:

/* Name it fk_food */ 
    CONSTRAINT `fk_food` 
    FOREIGN KEY (`food`) 
    REFERENCES `mydb`.`food` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    /* Name it fk_restaurant */ 
    CONSTRAINT `fk_restaurant` 
    FOREIGN KEY (`restaurant`) 
    REFERENCES `mydb`.`restaurants` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 

這些是我看到的唯一三個,但可能有其他我錯過了。

+0

謝謝,這對我有用! – arts777

+3

萬一有人遇到這個問題。即使在多個表格中更改約束名稱後,我仍然得到了errno 121。問題是即使在不同的表格中,你也不能有相同的約束名稱。我在table1和table2中使用'fk_entryid',並且必須分別將它們更改爲'fk_table1_entryid'和'fk_table2_entryid'才能使其工作。如果發生問題,MySQLWorkbench和MariaDB會發生這種情況。 –

相關問題