使用InnoDB =是
拼写=使用工作台的关系引用(或多或少应该正确)
主键=是
我真的不知道如何解决这个问题...我希望PEO-MVAlign的SCHOOLID被引用为MVALIGN和PEO的SchoolID ...但是在PEO-MVALIGN表中应该只能看到一个或存在一个SCHOOL ID。
错误信息
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
CREATE TABLE IF NOT EXISTS `mydb`.`PEO-MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`PEOCode` CHAR(4) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`, `DegreeProgram`, `PEOCode`),
INDEX `fk_PEO-MVAlign_MVAlign1_idx` (`SchoolID` ASC, `values` ASC),
INDEX `fk_PEO-MVAlign_PEO1_idx` (`SchoolID` ASC, `DegreeProgram` ASC, `PEOCode` ASC),
CONSTRAINT `fk_PEO-MVAlign_MVAlign1`
FOREIGN KEY (`SchoolID` , `values`)
REFERENCES `mydb`.`MVAlign` (`SchoolID` , `values`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_PEO-MVAlign_PEO1`
FOREIGN KEY (`DegreeProgram` , `PEOCode` , `SchoolID`)
REFERENCES `mydb`.`PEO` (`DegreeProgram` , `PEOCode` , `SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 23 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
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`REF-School`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-School` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-School` (
`SchoolID` DECIMAL(5) NOT NULL,
`SchoolName` VARCHAR(50) NOT NULL,
PRIMARY KEY (`SchoolID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`MVAlign`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`MVAlign` ;
CREATE TABLE IF NOT EXISTS `mydb`.`MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`),
INDEX `fk_MVAlign_REF-School` (`SchoolID` ASC),
CONSTRAINT `fk_MVAlign_REF-School`
FOREIGN KEY (`SchoolID`)
REFERENCES `mydb`.`REF-School` (`SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`REF-Degree`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-Degree` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-Degree` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`),
INDEX `fk_REF-Degree_REF-School1` (`SchoolID` ASC),
CONSTRAINT `fk_REF-Degree_REF-School1`
FOREIGN KEY (`SchoolID`)
REFERENCES `mydb`.`REF-School` (`SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`REF-Courses`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-Courses` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-Courses` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`CourseCode` VARCHAR(25) NOT NULL,
`Unit` DECIMAL(2,1) NOT NULL,
`CourseDescription` VARCHAR(200) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`, `CourseCode`),
INDEX `FK_COURSEStoDEGREE` (`SchoolID` ASC, `DegreeProgram` ASC),
CONSTRAINT `FK_COURSEStoDegree`
FOREIGN KEY (`SchoolID` , `DegreeProgram`)
REFERENCES `mydb`.`REF-Degree` (`SchoolID` , `DegreeProgram`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`REF-Courses`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`REF-Courses` ;
CREATE TABLE IF NOT EXISTS `mydb`.`REF-Courses` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`CourseCode` VARCHAR(25) NOT NULL,
`Unit` DECIMAL(2,1) NOT NULL,
`CourseDescription` VARCHAR(200) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`, `CourseCode`),
INDEX `FK_COURSEStoDEGREE` (`SchoolID` ASC, `DegreeProgram` ASC),
CONSTRAINT `FK_COURSEStoDegree`
FOREIGN KEY (`SchoolID` , `DegreeProgram`)
REFERENCES `mydb`.`REF-Degree` (`SchoolID` , `DegreeProgram`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`IGOA`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`IGOA` ;
CREATE TABLE IF NOT EXISTS `mydb`.`IGOA` (
`SchoolID` DECIMAL(5) NOT NULL,
`IGOCode` CHAR(25) NOT NULL,
`IGA` CHAR(50) NOT NULL,
`GradOutcome` VARCHAR(500) NOT NULL,
PRIMARY KEY (`SchoolID`, `IGOCode`),
INDEX `FK_IGOAtoSchool` (`SchoolID` ASC),
CONSTRAINT `FK_IGOAtoSchool`
FOREIGN KEY (`SchoolID`)
REFERENCES `mydb`.`REF-School` (`SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`IGO-MVAlign`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`IGO-MVAlign` ;
CREATE TABLE IF NOT EXISTS `mydb`.`IGO-MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
`IGOCode` CHAR(25) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`, `IGOCode`),
INDEX `fk_IGO-MVAlign_IGOA1_idx` (`SchoolID` ASC, `IGOCode` ASC),
INDEX `fk_IGO-MVAlign_MVAlign1_idx` (`SchoolID` ASC, `values` ASC),
CONSTRAINT `fk_IGO-MVAlign_IGOA1`
FOREIGN KEY (`SchoolID` , `IGOCode`)
REFERENCES `mydb`.`IGOA` (`SchoolID` , `IGOCode`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_IGO-MVAlign_MVAlign1`
FOREIGN KEY (`SchoolID` , `values`)
REFERENCES `mydb`.`MVAlign` (`SchoolID` , `values`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`PEO`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`PEO` ;
CREATE TABLE IF NOT EXISTS `mydb`.`PEO` (
`SchoolID` DECIMAL(5) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`PEOCode` CHAR(4) NOT NULL,
`EducObj` VARCHAR(500) NOT NULL,
PRIMARY KEY (`SchoolID`, `DegreeProgram`, `PEOCode`),
INDEX `FK_PEOtoREF-DEGREE` (`SchoolID` ASC, `DegreeProgram` ASC),
CONSTRAINT `FK_PEOtoREF-DEGREE`
FOREIGN KEY (`SchoolID` , `DegreeProgram`)
REFERENCES `mydb`.`REF-Degree` (`SchoolID` , `DegreeProgram`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`PEO-MVAlign`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`PEO-MVAlign` ;
CREATE TABLE IF NOT EXISTS `mydb`.`PEO-MVAlign` (
`SchoolID` DECIMAL(5) NOT NULL,
`values` CHAR(25) NOT NULL,
`DegreeProgram` VARCHAR(50) NOT NULL,
`PEOCode` CHAR(4) NOT NULL,
PRIMARY KEY (`SchoolID`, `values`, `DegreeProgram`, `PEOCode`),
INDEX `fk_PEO-MVAlign_MVAlign1_idx` (`SchoolID` ASC, `values` ASC),
INDEX `fk_PEO-MVAlign_PEO1_idx` (`SchoolID` ASC, `DegreeProgram` ASC, `PEOCode` ASC),
CONSTRAINT `fk_PEO-MVAlign_MVAlign1`
FOREIGN KEY (`SchoolID` , `values`)
REFERENCES `mydb`.`MVAlign` (`SchoolID` , `values`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_PEO-MVAlign_PEO1`
FOREIGN KEY (`DegreeProgram` , `PEOCode` , `SchoolID`)
REFERENCES `mydb`.`PEO` (`DegreeProgram` , `PEOCode` , `SchoolID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
最佳答案
一个表中的SQL FOREIGN KEY引用另一表中的相应列列表。 FK和引用表之间的列对应关系不是按名称而是按列表中列的顺序。
在引用表中,必须将引用列列表声明为UNIQUE NOT NULL或PRIMARY KEY。对应列的类型必须具有一定的协议(包括整数到整数和字符串到字符串)。同样,在MySQL中,FK列必须具有索引,并且该列的第一列必须以相同的顺序索引。在带有InnoDB的MySQL下,被引用的列必须在其表中具有索引,并且这些列以相同的顺序作为其第一列。 (某些索引可以是隐式的,例如对于PRIMARY KEY或FOREIGN KEY。)
对于PEO-MVAlign PEO FK,参考列的顺序与参考列的顺序不同。这将在PEO-MVAlign和PEO中配对不同名称的列。您可能不想要哪一个;可能您希望引用列表列和引用列列表的顺序相同。但是(对应的)所有列的类型都不匹配,因此不允许进行对应。但是,即使允许使用,被引用的列列表也不具有与PEO-MVAlign或PEO中的索引相同的名称和顺序。
关于mysql - hhERROR:错误1215:无法添加外键约束:MULTI VALUED REFERENCED TABLE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27448117/