作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
很难找到这个错误是什么。
我用
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 键和它引用的表给我带来了麻烦。
这是我的“注册”表(它不允许我创建的表)和“部分”表
-- -----------------------------------------------------
-- 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 Workbench 正向工程,所以我实际上并没有自己输入代码。我已经检查、双重检查和三次检查,所有数据类型似乎都匹配,所以问题一定是这个错误:“在引用的表中找不到索引,其中引用的列显示为第一列。”我不完全确定我是否 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;
最佳答案
此脚本会一直运行。如果没有,将顶部的两行更改为不同的模式名称,并在没有任何 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;
关于mysql - 错误 : Error 1215: Cannot add foreign key constraint (have tried everything),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102904/
我是一名优秀的程序员,十分优秀!