我知道这个问题已经在这个网站上出现了很多次,但是可以看出:
1)表中引用的任何外键都是最初定义它的表中的主键或唯一键。
2)没有两个外键具有相同的名称(dname,dname1等)
3)InnoDB用于所有表格。
任何想法可能导致错误1215? 这就是错误所说的:
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`major`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`major` (
`dname2` VARCHAR(40) NOT NULL COMMENT '',
`sid` INT NOT NULL COMMENT '',
PRIMARY KEY (`dname2`, `sid`) COMMENT '',
INDEX `sid_idx` (`sid` ASC) COMMENT '',
CONSTRAINT `dname2`
FOREIGN KEY (`dname2`)
REFERENCES `mydb`.`dept` (`dname`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `sid`
FOREIGN KEY (`sid`)
REFERENCES `mydb`.`student` (`sid`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
这是我的代码开始的地方:
-- MySQL Workbench Forward Engineering
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(40) NOT NULL COMMENT '',
`sex` VARCHAR(7) NOT NULL COMMENT '',
`age` INT NOT NULL COMMENT '',
`year` INT NOT NULL COMMENT '',
`gpa` DOUBLE NOT NULL COMMENT '',
PRIMARY KEY (`sid`) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`dept`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`dept` (
`dname` VARCHAR(40) NOT NULL COMMENT '',
`numphds` INT NOT NULL COMMENT '',
PRIMARY KEY (`dname`) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`prof`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`prof` (
`pname` VARCHAR(40) NOT NULL COMMENT '',
`dname` VARCHAR(40) NOT NULL COMMENT '',
PRIMARY KEY (`pname`) COMMENT '',
INDEX `dname_idx` (`dname` ASC) COMMENT '',
CONSTRAINT `dname`
FOREIGN KEY (`dname`)
REFERENCES `mydb`.`dept` (`dname`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`course`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`course` (
`cno` VARCHAR(10) NOT NULL COMMENT '',
`cname` VARCHAR(40) NOT NULL COMMENT '',
`dname1` VARCHAR(40) NOT NULL COMMENT '',
PRIMARY KEY (`cno`, `dname1`) COMMENT '',
INDEX `dname_idx` (`dname1` ASC) COMMENT '',
CONSTRAINT `dname1`
FOREIGN KEY (`dname1`)
REFERENCES `mydb`.`dept` (`dname`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`major`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`major` (
`dname2` VARCHAR(40) NOT NULL COMMENT '',
`sid` INT NOT NULL COMMENT '',
PRIMARY KEY (`dname2`, `sid`) COMMENT '',
INDEX `sid_idx` (`sid` ASC) COMMENT '',
CONSTRAINT `dname2`
FOREIGN KEY (`dname2`)
REFERENCES `mydb`.`dept` (`dname`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `sid`
FOREIGN KEY (`sid`)
REFERENCES `mydb`.`student` (`sid`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`section`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`section` (
`dname3` VARCHAR(40) NOT NULL COMMENT '',
`cno` VARCHAR(10) NOT NULL COMMENT '',
`sectno` INT NOT NULL COMMENT '',
`pname` VARCHAR(40) NOT NULL COMMENT '',
PRIMARY KEY (`dname3`, `cno`, `sectno`) COMMENT '',
INDEX `cno_idx` (`pname` ASC) COMMENT '',
CONSTRAINT `dname3`
FOREIGN KEY (`cno`)
REFERENCES `mydb`.`dept` (`dname`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `cno`
FOREIGN KEY (`pname`)
REFERENCES `mydb`.`course` (`cno`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `pname`
FOREIGN KEY ()
REFERENCES `mydb`.`prof` ()
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`enroll`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`enroll` (
`sid1` INT NOT NULL COMMENT '',
`grade` INT NOT NULL COMMENT '',
`dname4` VARCHAR(40) NOT NULL COMMENT '',
`cno1` VARCHAR(10) NOT NULL COMMENT '',
`sectno` INT NOT NULL COMMENT '',
PRIMARY KEY (`sid1`, `dname4`, `cno1`, `sectno`) COMMENT '',
INDEX `dname_idx` (`dname4` ASC) COMMENT '',
INDEX `cno_idx` (`cno1` ASC) COMMENT '',
INDEX `sectno_idx` (`sectno` ASC) COMMENT '',
CONSTRAINT `sid1`
FOREIGN KEY (`sid1`)
REFERENCES `mydb`.`student` (`sid`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `dname4`
FOREIGN KEY (`dname4`)
REFERENCES `mydb`.`dept` (`dname`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `cno1`
FOREIGN KEY (`cno1`)
REFERENCES `mydb`.`course` (`cno`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `sectno`
FOREIGN KEY (`sectno`)
REFERENCES `mydb`.`section` (`sectno`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
答案 0 :(得分:0)
这些定义存在两个问题:
没有为此外键定义提供名称。
CONSTRAINT `pname`
FOREIGN KEY ()
REFERENCES `mydb`.`prof` ()
ON DELETE CASCADE
ON UPDATE CASCADE
注册表正在尝试在主键索引中的非前导列上创建外键。
CONSTRAINT `sectno`
FOREIGN KEY (`sectno`)
REFERENCES `mydb`.`section` (`sectno`)
ON DELETE CASCADE
ON UPDATE CASCADE
部分定义的索引是
PRIMARY KEY (`dname3`, `cno`, `sectno`) COMMENT '',
使用此索引结构,无法强制执行外键。将sectno
移动到索引中的第一个键将支持外键。