在MySQL中创建表失败

时间:2014-03-22 08:45:34

标签: mysql mysql-workbench

我已经使用MySQLWorkbench设计了模式并在下面生成了创建SQL。

此查询通过此错误:无法创建表'museum.statue_details'。有人可以告诉我什么似乎是问题,如何解决?感谢。

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 `museum` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
    USE `museum` ;

    -- -----------------------------------------------------
    -- Table `museum`.`collection`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`collection` ;

    CREATE TABLE IF NOT EXISTS `museum`.`collection` (
      `collection_id` INT NOT NULL,
      `name` VARCHAR(45) NULL,
      `type` VARCHAR(45) NULL,
      `description` VARCHAR(45) NULL,
      `address` VARCHAR(45) NULL,
      `phone` VARCHAR(45) NULL,
      `current_contact_person` VARCHAR(45) NULL,
      PRIMARY KEY (`collection_id`),
      UNIQUE INDEX `name_UNIQUE` (`name` ASC))
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`country`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`country` ;

    CREATE TABLE IF NOT EXISTS `museum`.`country` (
      `country_id` INT NOT NULL,
      `country_name` VARCHAR(45) NULL,
      PRIMARY KEY (`country_id`))
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`epoch`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`epoch` ;

    CREATE TABLE IF NOT EXISTS `museum`.`epoch` (
      `epoch_id` INT NOT NULL,
      `epoch_name` VARCHAR(45) NULL,
      PRIMARY KEY (`epoch_id`))
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`artefact`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`artefact` ;

    CREATE TABLE IF NOT EXISTS `museum`.`artefact` (
      `artefact_id` INT NOT NULL,
      `collection_id` INT NOT NULL,
      `country_id` INT NOT NULL,
      `epoch_id` INT NOT NULL,
      `title` VARCHAR(50) NULL,
      `description` TEXT NULL,
      `year` DATE NULL,
      PRIMARY KEY (`artefact_id`, `collection_id`, `country_id`, `epoch_id`),
      INDEX `collection_id_idx` (`collection_id` ASC),
      INDEX `country_id_idx` (`country_id` ASC),
      INDEX `epoch_id_idx` (`epoch_id` ASC),
      CONSTRAINT `collection_id`
        FOREIGN KEY (`collection_id`)
        REFERENCES `museum`.`collection` (`collection_id`)
        ON DELETE RESTRICT
        ON UPDATE NO ACTION,
      CONSTRAINT `country_id`
        FOREIGN KEY (`country_id`)
        REFERENCES `museum`.`country` (`country_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
      CONSTRAINT `epoch_id`
        FOREIGN KEY (`epoch_id`)
        REFERENCES `museum`.`epoch` (`epoch_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`painting_details`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`painting_details` ;

    CREATE TABLE IF NOT EXISTS `museum`.`painting_details` (
      `artefact_id` INT NOT NULL,
      `type` VARCHAR(45) NULL,
      `material` VARCHAR(45) NULL,
      `style` VARCHAR(45) NULL,
      PRIMARY KEY (`artefact_id`),
      CONSTRAINT `artefact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_id`)
        ON DELETE RESTRICT
        ON UPDATE NO ACTION)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`statue_details`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`statue_details` ;

    CREATE TABLE IF NOT EXISTS `museum`.`statue_details` (
      `artefact_id` INT NOT NULL,
      `material` VARCHAR(45) NULL,
      `height` DECIMAL(10,2) NULL,
      `weight` DECIMAL(10,2) NULL,
      `style` VARCHAR(45) NULL,
      PRIMARY KEY (`artefact_id`),
      CONSTRAINT `artefact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`relic_details`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`relic_details` ;

    CREATE TABLE IF NOT EXISTS `museum`.`relic_details` (
      `artefact_id` INT NOT NULL,
      `material` VARCHAR(45) NULL,
      `usage` VARCHAR(45) NULL,
      PRIMARY KEY (`artefact_id`),
      CONSTRAINT `artefact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`owner`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`owner` ;

    CREATE TABLE IF NOT EXISTS `museum`.`owner` (
      `owner_id` INT NOT NULL,
      `owner_name` VARCHAR(45) NULL,
      PRIMARY KEY (`owner_id`))
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`other_artefact`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`other_artefact` ;

    CREATE TABLE IF NOT EXISTS `museum`.`other_artefact` (
      `artefact_id` INT NOT NULL,
      `name` VARCHAR(45) NULL,
      `description` TEXT NULL,
      PRIMARY KEY (`artefact_id`),
      CONSTRAINT `artifact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`borrowed_artefact`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`borrowed_artefact` ;

    CREATE TABLE IF NOT EXISTS `museum`.`borrowed_artefact` (
      `artefact_id` INT NOT NULL,
      `owner_id` INT NULL,
      `collection_id` INT NOT NULL,
      `date_borrowed` DATE NULL,
      `date_returned` DATE NULL,
      INDEX `owner_id_idx` (`owner_id` ASC),
      INDEX `artifact_id_idx` (`artefact_id` ASC),
      INDEX `collection_id_idx` (`collection_id` ASC),
      CONSTRAINT `artefact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
      CONSTRAINT `owner_id`
        FOREIGN KEY (`owner_id`)
        REFERENCES `museum`.`owner` (`owner_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
      CONSTRAINT `collection_id`
        FOREIGN KEY (`collection_id`)
        REFERENCES `museum`.`collection` (`collection_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`permenent_artefact`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`permenent_artefact` ;

    CREATE TABLE IF NOT EXISTS `museum`.`permenent_artefact` (
      `artefact_id` INT NOT NULL,
      `cost` DECIMAL(10,2) NULL,
      `status` VARCHAR(45) NULL,
      `date_acquired` DATE NULL,
      PRIMARY KEY (`artefact_id`),
      CONSTRAINT `artefact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`creator`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`creator` ;

    CREATE TABLE IF NOT EXISTS `museum`.`creator` (
      `creator_id` INT NOT NULL,
      `country_id` INT NULL COMMENT 'Country of origin',
      `epoch_id` INT NULL,
      `creator_name` VARCHAR(45) NULL COMMENT 'The creator name is unique',
      `creator_dob` DATE NULL COMMENT 'Date of birth',
      `creator_dod` DATE NULL COMMENT 'Date of died',
      `main_style` VARCHAR(45) NULL,
      `description` TEXT NULL,
      PRIMARY KEY (`creator_id`),
      UNIQUE INDEX `creator_name_UNIQUE` (`creator_name` ASC),
      CONSTRAINT `country_id`
        FOREIGN KEY (`creator_id`)
        REFERENCES `museum`.`country` (`country_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
      CONSTRAINT `epoch_id`
        FOREIGN KEY ()
        REFERENCES `museum`.`epoch` ()
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`created_artefact`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`created_artefact` ;

    CREATE TABLE IF NOT EXISTS `museum`.`created_artefact` (
      `artefact_id` INT NOT NULL,
      `creator_id` INT NULL,
      INDEX `creator_id_idx` (`creator_id` ASC),
      CONSTRAINT `artefact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
      CONSTRAINT `creator_id`
        FOREIGN KEY (`creator_id`)
        REFERENCES `museum`.`creator` (`creator_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT)
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`exhibition`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`exhibition` ;

    CREATE TABLE IF NOT EXISTS `museum`.`exhibition` (
      `exhibition_id` INT NOT NULL,
      `country_id` VARCHAR(45) NULL,
      `start_date` DATE NOT NULL,
      `end_date` DATE NOT NULL,
      PRIMARY KEY (`exhibition_id`))
    ENGINE = InnoDB;


    -- -----------------------------------------------------
    -- Table `museum`.`artifact_displayed_during_exhibition`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `museum`.`artifact_displayed_during_exhibition` ;

    CREATE TABLE IF NOT EXISTS `museum`.`artifact_displayed_during_exhibition` (
      `exhibition_id` INT NOT NULL,
      `artefact_id` INT NULL,
      PRIMARY KEY (`exhibition_id`),
      INDEX `artifact_id_idx` (`artefact_id` ASC),
      CONSTRAINT `exhibition_id`
        FOREIGN KEY (`exhibition_id`)
        REFERENCES `museum`.`exhibition` (`exhibition_id`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
      CONSTRAINT `artefact_id`
        FOREIGN KEY (`artefact_id`)
        REFERENCES `museum`.`artefact` (`artefact_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;

3 个答案:

答案 0 :(得分:3)

您有许多具有相同名称的约束,更改它们或让mysql命名它们。

CONSTRAINT `artefact_id` 

这无处不在。还有其他人。 同样在museum.creator中你缺少列:

 CONSTRAINT `epoch_id`
    FOREIGN KEY ()
    REFERENCES `museum`.`epoch` ()

你可能想要介于epoch_id之间。所有这些都是固定的,它在我的机器上运行。

答案 1 :(得分:1)

你应该给出你的外键的其他名称:

而不是

CONSTRAINT `artefact_id`
FOREIGN KEY (`artefact_id`)
REFERENCES `museum`.`artefact` (`artefact_id`)

做的:

CONSTRAINT FK_mykey  --> give name as you like here to distinguich between others
FOREIGN KEY (`artefact_id`) 
REFERENCES `artefact` (`artefact_id`)

答案 2 :(得分:0)

创建表语法:

http://dev.mysql.com/doc/refman/5.7/en/create-table.html

我认为问题在于您使用{name1}声明表名。{name2}。

CREATE TABLE IF NOT EXISTS `collection` (
  `collection_id` INT NOT NULL,
  `name` VARCHAR(45) NULL,
  `type` VARCHAR(45) NULL,
  `description` VARCHAR(45) NULL,
  `address` VARCHAR(45) NULL,
  `phone` VARCHAR(45) NULL,
  `current_contact_person` VARCHAR(45) NULL,
  PRIMARY KEY (`collection_id`),
  UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;

`

为我工作