错误:错误1005:无法创建表(错误号:150)

时间:2013-08-15 10:13:45

标签: mysql foreign-key-relationship mysql-workbench workbench

我收到此错误“错误:错误1005:无法创建表(错误号:150)”,我的所有表都有外键约束。

Executing SQL script in server    
ERROR: Error 1005: Can't create table 'shop_online.t_newsletters' (errno: 150)
CREATE  TABLE IF NOT EXISTS `shop_online`.`t_newsletters` (
  `id_newsletter` INT(11) NOT NULL AUTO_INCREMENT ,   
  `title` VARCHAR(45) NULL DEFAULT NULL ,
  `content` TEXT NULL DEFAULT NULL ,
  `author` INT(11) NOT NULL ,
  `creation` DATE NULL DEFAULT NULL ,
  PRIMARY KEY (`id_newsletter`, `author`) ,  
  INDEX `fk_t_newsletters_t_administrators1` (`author` ASC) ,  
  CONSTRAINT `fk_t_newsletters_t_administrators1`
    FOREIGN KEY (`author` )
    REFERENCES `shop_online`.`t_administrators` (`id_administrator` ) 
    ON DELETE SET NULL 
    ON UPDATE CASCADE) 
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci

导致:

   
SQL script execution finished: statements: 3 succeeded, 1 failed

我正在使用MySQL Workbench,这是模型:http://puu.sh/42CTd.png

我正在尝试这样做,因为如果删除用户,我不想丢失数据库中的任何条目。

如果您有其他解决方案,我已准备好尝试了!

编辑:所有数据库

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

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_newsletters` (
  `id_newsletter` INT(11) NOT NULL AUTO_INCREMENT ,
  `title` VARCHAR(45) NULL DEFAULT NULL ,
  `content` TEXT NULL DEFAULT NULL ,
  `author` INT(11) NULL DEFAULT NULL ,
  `creation` DATE NULL DEFAULT NULL ,
  PRIMARY KEY (`id_newsletter`, `author`) ,
  INDEX `fk_t_newsletters_t_administrators1` (`author` ASC) ,
  CONSTRAINT `fk_t_newsletters_t_administrators1`
  FOREIGN KEY (`author` )
    REFERENCES `shop_online`.`t_administrators` (`id_administrator` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_daily_tips` (
`id_daily_tip` INT(11) NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NULL DEFAULT NULL ,
  `content` TEXT NULL DEFAULT NULL ,
  `author` INT(11) NULL DEFAULT NULL ,
  PRIMARY KEY (`id_daily_tip`, `author`) ,
  INDEX `fk_t_daily_tips_t_administrators1` (`author` ASC) ,
  CONSTRAINT `fk_t_daily_tips_t_administrators1`
    FOREIGN KEY (`author` )
    REFERENCES `shop_online`.`t_administrators` (`id_administrator` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_news` (
  `id_news` INT(11) NOT NULL AUTO_INCREMENT ,
  `title` VARCHAR(45) NULL DEFAULT NULL ,
  `introduction` TEXT NULL DEFAULT NULL ,
  `content` TEXT NULL DEFAULT NULL ,
  `author` INT(11) NULL DEFAULT NULL ,
  `creation` DATE NULL DEFAULT NULL ,
  `modification` DATE NULL DEFAULT NULL ,
  `modificator` INT(11) NULL DEFAULT NULL ,
  PRIMARY KEY (`id_news`, `modificator`, `author`) ,
  INDEX `fk_t_news_t_administrators1` (`author` ASC) ,
  INDEX `fk_t_news_t_administrators2` (`modificator` ASC) ,
  CONSTRAINT `fk_t_news_t_administrators1`
    FOREIGN KEY (`author` )
    REFERENCES `shop_online`.`t_administrators` (`id_administrator` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `fk_t_news_t_administrators2`
    FOREIGN KEY (`modificator` )
    REFERENCES `shop_online`.`t_administrators` (`id_administrator` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_magazines` (
  `id_magazine` INT(11) NOT NULL AUTO_INCREMENT ,
  `file` VARCHAR(128) NULL DEFAULT NULL ,
  `view` INT(11) NULL DEFAULT NULL ,
  `author` INT(11) NULL DEFAULT NULL ,
  PRIMARY KEY (`id_magazine`, `author`) ,
  INDEX `fk_t_magazines_t_administrators1` (`author` ASC) ,
  CONSTRAINT `fk_t_magazines_t_administrators1`
    FOREIGN KEY (`author` )
    REFERENCES `shop_online`.`t_administrators` (`id_administrator` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_members` (
  `id_member` INT(11) NOT NULL AUTO_INCREMENT ,
  `email` VARCHAR(326) NULL DEFAULT NULL ,
  `nickname` VARCHAR(45) NULL DEFAULT NULL ,
  `title` ENUM('M','Mme','Mlle','Dr','Me') NULL DEFAULT NULL ,
  `firstname` VARCHAR(45) NULL DEFAULT NULL ,
  `lastname` VARCHAR(45) NULL DEFAULT NULL ,
  `address` VARCHAR(128) NULL DEFAULT NULL ,
  `postal_code` VARCHAR(45) NULL DEFAULT NULL ,
  `city` VARCHAR(45) NULL DEFAULT NULL ,
  `country` VARCHAR(45) NULL DEFAULT NULL ,
  `phone` VARCHAR(45) NULL DEFAULT NULL ,
  PRIMARY KEY (`id_member`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_categories` (
  `id_category` INT(11) NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NULL DEFAULT NULL ,
  PRIMARY KEY (`id_category`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_sub_categories` (
  `id_sub_category` INT(11) NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NULL DEFAULT NULL ,
  `category` INT(11) NOT NULL ,
  PRIMARY KEY (`id_sub_category`, `category`) ,
  INDEX `fk_t_sub_categories_t_categories1` (`category` ASC) ,
  CONSTRAINT `fk_t_sub_categories_t_categories1`
    FOREIGN KEY (`category` )
    REFERENCES `shop_online`.`t_categories` (`id_category` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_articles` (
 `id_article` INT(11) NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(256) NULL DEFAULT NULL ,
  `description` TEXT NULL DEFAULT NULL ,
  `price` DECIMAL(5,2) NULL DEFAULT NULL ,
  `stock` INT(11) NULL DEFAULT NULL ,
  `informations` TEXT NULL DEFAULT NULL ,
  `author` INT(11) NULL DEFAULT NULL ,
  `creation` DATE NULL DEFAULT NULL ,
  `subcategory` INT(11) NOT NULL ,
  PRIMARY KEY (`id_article`, `author`, `subcategory`) ,
  INDEX `fk_t_articles_t_administrators` (`author` ASC) ,
  INDEX `fk_t_articles_t_sub_categories1` (`subcategory` ASC) ,
  CONSTRAINT `fk_t_articles_t_administrators`
    FOREIGN KEY (`author` )
    REFERENCES `shop_online`.`t_administrators` (`id_administrator` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `fk_t_articles_t_sub_categories1`
    FOREIGN KEY (`subcategory` )
    REFERENCES `shop_online`.`t_sub_categories` (`id_sub_category` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

CREATE  TABLE IF NOT EXISTS `shop_online`.`t_administrators` (

id_administrator INT(11)NOT NULL AUTO_INCREMENT,

name VARCHAR(45)NULL DEFAULT NULL,

email VARCHAR(326)NULL DEFAULT NULL,

login VARCHAR(45)NULL DEFAULT NULL,

password VARCHAR(256)NULL DEFAULT NULL,

last_connection DATE NULL DEFAULT NULL,

PRIMARY KEY(id_administrator))

ENGINE = InnoDB

DEFAULT CHARACTER SET = latin1

COLLATE = latin1_swedish_ci;

1 个答案:

答案 0 :(得分:4)

你有author作为主键的一部分,这意味着它不可为空,但你对该字段的外键约束有ON DELETE SET NULL。这两个条件不相容。

要解决此问题,请从author声明中删除PRIMARY KEY;您的id_newsletter字段已经是auto_increment,因此除非您的应用专门设置它,否则应该是唯一的(在这种情况下,您不应使用auto_increment)。

最好验证引用的字段是否已编入索引并且具有兼容的数据类型;我无法从提供的信息中看到这一点,但添加到“errno 150清单”中是一件好事:)