错误:错误1215:无法添加外键约束(已尝试过所有内容)

时间:2015-12-05 08:32:46

标签: mysql foreign-keys mysql-workbench

找不到这个错误是不可能的。

我用

SHOW ENGINE INNODB STATUS

得到此错误:

2015-12-05 03:20:44 16c8 Error in foreign key constraint of table mydb/enroll:

    FOREIGN KEY (`sectno`)
    REFERENCES `mydb`.`section` (`sectno`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.

所以我认为可以安全地假设它是sectno键,它所引用的表格给我带来了麻烦。

所以这是我的#34;注册"表格(它不允许我创建的表格)和"部分"表

    -- -----------------------------------------------------
-- Table `mydb`.`section`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`section` (
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `cno` INT NOT NULL COMMENT '',
  `sectno` INT NOT NULL COMMENT '',
  `pname` VARCHAR(45) NOT NULL COMMENT '',
  PRIMARY KEY (`sectno`, `dname`, `cno`)  COMMENT '',
  INDEX `fk_cno_section_idx` (`cno` ASC)  COMMENT '',
  INDEX `fk_pname_section_idx` (`pname` ASC)  COMMENT '',
  CONSTRAINT `fk_dname_section`
    FOREIGN KEY (`dname`)
    REFERENCES `mydb`.`dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_cno_section`
    FOREIGN KEY (`cno`)
    REFERENCES `mydb`.`course` (`cno`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_pname_section`
    FOREIGN KEY (`pname`)
    REFERENCES `mydb`.`prof` (`pname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`enroll`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`enroll` (
  `sid` INT NOT NULL COMMENT '',
  `grade` DOUBLE NULL COMMENT '',
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `cno` INT NOT NULL COMMENT '',
  `sectno` INT NOT NULL COMMENT '',
  PRIMARY KEY (`sid`, `dname`, `cno`, `sectno`)  COMMENT '',
  INDEX `fk_dname_enroll_idx` (`dname` ASC)  COMMENT '',
  INDEX `fk_cno_enroll_idx` (`cno` ASC)  COMMENT '',
  INDEX `fk_sectno_enroll_idx` (`sectno` ASC)  COMMENT '',
  CONSTRAINT `fk_sid_enroll`
    FOREIGN KEY (`sid`)
    REFERENCES `mydb`.`student` (`sid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_dname_enroll`
    FOREIGN KEY (`dname`)
    REFERENCES `mydb`.`dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_cno_enroll`
    FOREIGN KEY (`cno`)
    REFERENCES `mydb`.`course` (`cno`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_sectno_enroll`
    FOREIGN KEY (`sectno`)
    REFERENCES `mydb`.`section` (`sectno`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

我使用MySQL工作台正向工程,所以我自己并没有真正输入代码。我已检查并进行了双重检查和三重检查,并且所有数据类型似乎都匹配,因此问题必定是此错误:"无法在引用的表中找到引用列所显示的索引第一列。"我并不完全确定我100%理解它,但我认为这意味着它们被引用的FK必须位于引用表的第一列。所以在节表中,我喜欢..declared

 `sectno` INT NOT NULL COMMENT '',`

在所有其他列之前,它是"首先,"但仍然没有。我一小时又一小时地花了一个小时,我只是......精神疲惫。有人请帮忙

编辑:以下是其余表格:

- MySQL Workbench正向工程

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 mydb
-- -----------------------------------------------------

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

-- -----------------------------------------------------
-- Table `mydb`.`student`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`student` (
  `sid` INT NOT NULL COMMENT '',
  `sname` VARCHAR(45) NULL COMMENT '',
  `sex` VARCHAR(10) NULL COMMENT '',
  `age` INT NULL COMMENT '',
  `year` INT NULL COMMENT '',
  `gpa` DOUBLE NULL COMMENT '',
  PRIMARY KEY (`sid`)  COMMENT '')
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`dept`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`dept` (
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `numphds` INT NULL COMMENT '',
  PRIMARY KEY (`dname`)  COMMENT '')
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`prof`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`prof` (
  `pname` VARCHAR(45) NOT NULL COMMENT '',
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  PRIMARY KEY (`pname`)  COMMENT '',
  INDEX `fk_dname_prof_idx` (`dname` ASC)  COMMENT '',
  CONSTRAINT `fk_dname_prof`
    FOREIGN KEY (`dname`)
    REFERENCES `mydb`.`dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`course`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`course` (
  `cno` INT NOT NULL COMMENT '',
  `cname` VARCHAR(45) NULL COMMENT '',
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  PRIMARY KEY (`cno`, `dname`)  COMMENT '',
  INDEX `pk_dname_course_idx` (`dname` ASC)  COMMENT '',
  CONSTRAINT `pk_dname_course`
    FOREIGN KEY (`dname`)
    REFERENCES `mydb`.`dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`major`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`major` (
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `sid` INT NOT NULL COMMENT '',
  PRIMARY KEY (`dname`, `sid`)  COMMENT '',
  INDEX `fk_sid_major_idx` (`sid` ASC)  COMMENT '',
  CONSTRAINT `fk_dname_major`
    FOREIGN KEY (`dname`)
    REFERENCES `mydb`.`dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_sid_major`
    FOREIGN KEY (`sid`)
    REFERENCES `mydb`.`student` (`sid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

1 个答案:

答案 0 :(得分:1)

此脚本一直运行。如果没有,请将顶部的2行更改为其他模式名称,并在不激活任何comment个部分的情况下运行它。

由于这个原因,我暂时无法找到参考资料,但我曾在这里偶然发现它。

要明确的是,在某些界面中,comment部分会抛出错误。

毋庸置疑,他们的运作顺序非常重要。

你所有的FK状况都很好看。数据类型和符号很好,并且从引用引用的表匹配。在引用的表中,它们在这些列上都有最左边的索引。

create schema asdf789;

use asdf789;


CREATE TABLE IF NOT EXISTS `section` (
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `cno` INT NOT NULL COMMENT '',
  `sectno` INT NOT NULL COMMENT '',
  `pname` VARCHAR(45) NOT NULL COMMENT '',
  PRIMARY KEY (`sectno`, `dname`, `cno`)  COMMENT '',
  INDEX `fk_cno_section_idx` (`cno` ASC)  COMMENT '',
  INDEX `fk_pname_section_idx` (`pname` ASC)  COMMENT ''
)ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS `student` (
  `sid` INT NOT NULL COMMENT '',
  `sname` VARCHAR(45) NULL COMMENT '',
  `sex` VARCHAR(10) NULL COMMENT '',
  `age` INT NULL COMMENT '',
  `year` INT NULL COMMENT '',
  `gpa` DOUBLE NULL COMMENT '',
  PRIMARY KEY (`sid`)  COMMENT '')
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`dept`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dept` (
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `numphds` INT NULL COMMENT '',
  PRIMARY KEY (`dname`)  COMMENT '')
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`prof`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `prof` (
  `pname` VARCHAR(45) NOT NULL COMMENT '',
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  PRIMARY KEY (`pname`)  COMMENT '',
  INDEX `fk_dname_prof_idx` (`dname` ASC)  COMMENT '',
  CONSTRAINT `fk_dname_prof`
    FOREIGN KEY (`dname`)
    REFERENCES `dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`course`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `course` (
  `cno` INT NOT NULL COMMENT '',
  `cname` VARCHAR(45) NULL COMMENT '',
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  PRIMARY KEY (`cno`, `dname`)  COMMENT '',
  INDEX `pk_dname_course_idx` (`dname` ASC)  COMMENT '',
  CONSTRAINT `pk_dname_course`
    FOREIGN KEY (`dname`)
    REFERENCES `dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`major`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `major` (
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `sid` INT NOT NULL COMMENT '',
  PRIMARY KEY (`dname`, `sid`)  COMMENT '',
  INDEX `fk_sid_major_idx` (`sid` ASC)  COMMENT '',
  CONSTRAINT `fk_dname_major`
    FOREIGN KEY (`dname`)
    REFERENCES `dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_sid_major`
    FOREIGN KEY (`sid`)
    REFERENCES `student` (`sid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS `enroll` (
  `sid` INT NOT NULL COMMENT '',
  `grade` DOUBLE NULL COMMENT '',
  `dname` VARCHAR(45) NOT NULL COMMENT '',
  `cno` INT NOT NULL COMMENT '',
  `sectno` INT NOT NULL COMMENT '',
  PRIMARY KEY (`sid`, `dname`, `cno`, `sectno`)  COMMENT '',
  INDEX `fk_dname_enroll_idx` (`dname` ASC)  COMMENT '',
  INDEX `fk_cno_enroll_idx` (`cno` ASC)  COMMENT '',
  INDEX `fk_sectno_enroll_idx` (`sectno` ASC)  COMMENT '',
  CONSTRAINT `fk_sid_enroll`
    FOREIGN KEY (`sid`)
    REFERENCES `student` (`sid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_dname_enroll`
    FOREIGN KEY (`dname`)
    REFERENCES `dept` (`dname`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_cno_enroll`
    FOREIGN KEY (`cno`)
    REFERENCES `course` (`cno`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_sectno_enroll`
    FOREIGN KEY (`sectno`)
    REFERENCES `section` (`sectno`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;