0
我知道這個問題已經出現在這個網站上很多次了,但可以看出:
1)表中引用的任何外鍵都是表中的主鍵或唯一鍵,是最初定義的。
2)沒有兩個外鍵具有相同的名稱(dname,dname1等)
3)InnoDB用於所有表。嘗試轉發工程師時出錯? SQL錯誤1215
任何想法可能會導致錯誤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 @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @[email protected]@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 [email protected]_SQL_MODE;
SET [email protected]_FOREIGN_KEY_CHECKS;
SET [email protected]_UNIQUE_CHECKS;