-1

我收到了这个错误“错误:错误 1005:无法创建表(errno:150)”,我的所有表都对外键有约束。

在服务器中执行 SQL 脚本    
错误:错误 1005:无法创建表“shop_online.t_newsletters”(错误号: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 脚本执行完成:语句:3 成功,1 失败

我正在使用 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_administratorINT(11) NOT NULL AUTO_INCREMENT ,

nameVARCHAR(45) NULL 默认 NULL ,

emailVARCHAR(326) NULL 默认 NULL ,

loginVARCHAR(45) NULL 默认 NULL ,

passwordVARCHAR(256) NULL 默认 NULL ,

last_connection日期 NULL 默认 NULL ,

主键 ( id_administrator) )

引擎 = InnoDB

默认字符集 = latin1

整理 = latin1_swedish_ci;

4

1 回答 1

4

您已将author其作为主键的一部分,这意味着它不可为空,但您已获得ON DELETE SET NULL该字段的外键约束。这两个条件是不相容的。

要修复它,author请从您的PRIMARY KEY声明中删除;您的id_newsletter字段已经auto_increment是唯一的,除非您的应用程序专门设置它(在这种情况下您不应该使用auto_increment)。

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

于 2013-08-15T10:26:38.610 回答