Workbench和InnoDB错误150 varchar FK

时间:2014-04-05 19:46:31

标签: mysql sql workbench

我正在使用Workbench中的数据库,但我遇到了postlanguage之间的外键问题。我假设fk触发了此错误,因为当我删除此fk时,查询运行没有任何问题。我检查了type兼容性并确保索引存在。我在这里错过了什么?

fk的目标是在varchar上引用这两个表格。

触发150(InnoDB)的SQL:

-- MySQL Script generated by MySQL Workbench
-- 04/05/14 22:08:25
-- Model: New Model    Version: 1.0
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';

-- -----------------------------------------------------
-- Schema SBDB_v0.1.2
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SBDB_v0.1.2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `SBDB_v0.1.2` ;

-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`language`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`language` (
  `id` INT NOT NULL,
  `language` VARCHAR(120) NOT NULL,
  `lang_abr` CHAR(3) NULL,
  PRIMARY KEY (`id`, `language`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`post`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`post` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `language` VARCHAR(120) NOT NULL,
  `thumbnail_name` VARCHAR(255) NOT NULL,
  `id_sk` INT NULL,
  `title` VARCHAR(255) NULL,
  `author` VARCHAR(45) NULL,
  `message` TEXT NULL,
  `date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  `last_time_modified` TIMESTAMP NULL,
  PRIMARY KEY (`id`),
  INDEX `language_1_idx` (`language` ASC),
  CONSTRAINT `language_1`
    FOREIGN KEY (`language`)
    REFERENCES `SBDB_v0.1.2`.`language` (`language`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`image`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`image` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `language` VARCHAR(255) NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `upload_date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`, `language`),
  INDEX `name_1_idx` (`name` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`category` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `language` VARCHAR(255) NOT NULL,
  `title` VARCHAR(255) NULL,
  `description` VARCHAR(255) NULL,
  `date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`, `language`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`post_category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`post_category` (
  `id_post` INT NOT NULL,
  `id_category` INT NOT NULL,
  PRIMARY KEY (`id_post`, `id_category`),
  INDEX `id_category_idx` (`id_category` ASC),
  CONSTRAINT `id_post`
    FOREIGN KEY (`id_post`)
    REFERENCES `SBDB_v0.1.2`.`post` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `id_category`
    FOREIGN KEY (`id_category`)
    REFERENCES `SBDB_v0.1.2`.`category` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`page`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`page` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `id_page_pass` INT NULL,
  `title` VARCHAR(255) NULL,
  `content` MEDIUMTEXT NULL,
  `date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  `last_time_modified` TIMESTAMP NULL,
  PRIMARY KEY (`id`, `id_page_pass`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`page_password`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`page_password` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `password` VARCHAR(255) NOT NULL,
  `date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `id_page211`
    FOREIGN KEY (`id`)
    REFERENCES `SBDB_v0.1.2`.`page` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`search_keyword`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`search_keyword` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `language` VARCHAR(255) NOT NULL,
  `keyword` VARCHAR(255) NULL,
  PRIMARY KEY (`id`, `language`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`post_search_keyword`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`post_search_keyword` (
  `id_post` INT NOT NULL,
  `id_sk` INT NOT NULL,
  PRIMARY KEY (`id_post`, `id_sk`),
  INDEX `id_sk_1_idx` (`id_sk` ASC),
  CONSTRAINT `id_post_2`
    FOREIGN KEY (`id_post`)
    REFERENCES `SBDB_v0.1.2`.`post` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `id_sk_1`
    FOREIGN KEY (`id_sk`)
    REFERENCES `SBDB_v0.1.2`.`search_keyword` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`user` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `login` VARCHAR(25) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  `firstname` VARCHAR(45) NULL,
  `lastname` VARCHAR(45) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `SBDB_v0.1.2`.`social_media`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`social_media` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `language` VARCHAR(255) NOT NULL,
  `name` VARCHAR(45) NULL,
  `link` VARCHAR(255) NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


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

1 个答案:

答案 0 :(得分:1)

当建立FOREIGN KEY关系时,MySQL期望引用和引用列都被索引。如果它是单个列FOREIGN KEY,则引用列上必须有单个索引。

在此language表格中,您已在PRIMARY KEY之间定义了化合物 (id, language),但仅在(language)上没有单独的索引。

CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`language` (
  `id` INT NOT NULL,
  `language` VARCHAR(120) NOT NULL,
  `lang_abr` CHAR(3) NULL,
  /* compound key only */
  PRIMARY KEY (`id`, `language`))
ENGINE = InnoDB;

仅在(language)添加另一个索引,因此引用列的索引指向:

CREATE TABLE IF NOT EXISTS `SBDB_v0.1.2`.`language` (
  `id` INT NOT NULL,
  `language` VARCHAR(120) NOT NULL,
  `lang_abr` CHAR(3) NULL,
  PRIMARY KEY (`id`, `language`),
  /* single index on language */
  INDEX (`language`)
) ENGINE = InnoDB;
添加了额外索引的

Here it is working properly