mysql中有多个外键作为主键的sql表,出现错误1215:无法添加外键约束

gg58donl  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(403)

我创建了这些表:

CREATE TABLE `course` (
  `idcourse` varchar(2) NOT NULL,
  `courseName` varchar(45) NOT NULL,
  `subjectID` varchar(2) NOT NULL,
  PRIMARY KEY (`idcourse`),
  KEY `subjectID_idx` (`subjectID`),
  CONSTRAINT `subjectID` FOREIGN KEY (`subjectID`) REFERENCES `subject` (`idsubject`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `exam` (
  `subjectID` varchar(2) NOT NULL,
  `courseID` varchar(2) NOT NULL,
  `examNumber` varchar(2) NOT NULL,
  `duration` int(11) DEFAULT NULL,
  PRIMARY KEY (`subjectID`,`courseID`,`examNumber`),
  KEY `idCourse_idx` (`courseID`),
  CONSTRAINT `idCo` FOREIGN KEY (`courseID`) REFERENCES `course` (`idcourse`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `idSu` FOREIGN KEY (`subjectID`) REFERENCES `subject` (`idsubject`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `question` (
  `questionText` varchar(100) DEFAULT NULL,
  `answer1` varchar(100) DEFAULT NULL,
  `answer2` varchar(100) DEFAULT NULL,
  `answer3` varchar(100) DEFAULT NULL,
  `answer4` varchar(100) DEFAULT NULL,
  `subjetID` varchar(2) NOT NULL,
  `questionNumber` varchar(3) NOT NULL,
  `rightAnswer` int(4) DEFAULT NULL,
  PRIMARY KEY (`subjetID`,`questionNumber`),
  CONSTRAINT `idsubject` FOREIGN KEY (`subjetID`) REFERENCES `subject` (`idsubject`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `subject` (
  `idsubject` varchar(2) NOT NULL,
  `subjectName` varchar(45) NOT NULL,
  PRIMARY KEY (`idsubject`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

在主题表i中添加一条记录:
idsubject=02,subjectname=mathematica
在课程表i中添加一条记录:
idcourse=03,coursename=1,subjectid=02
在检查表中,我添加了一条记录:
subjectid=02,courseid=03,examnumber=01,duration=180
现在,我想创建一个表:questionsinexam

CREATE TABLE `test`.`questionsinexam` (
  `idExamSubject` VARCHAR(2) NOT NULL,
  `idExamCourse` VARCHAR(2) NOT NULL,
  `idExamNumber` VARCHAR(2) NOT NULL,
  `idQuestionNumber` VARCHAR(3) NOT NULL,
  `pointsPerQuestion` INT NULL,
  PRIMARY KEY (`idExamSubject`, `idExamCourse`, `idExamNumber`, `idQuestionNumber`),
  INDEX `idExamCourse_idx` (`idExamCourse` ASC),
  INDEX `idExamNumber_idx` (`idExamNumber` ASC),
  INDEX `idQuestionNumber_idx` (`idQuestionNumber` ASC),
  CONSTRAINT `idExamSubject`
    FOREIGN KEY (`idExamSubject`)
    REFERENCES `test`.`exam` (`subjectID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `idExamCourse`
    FOREIGN KEY (`idExamCourse`)
    REFERENCES `test`.`exam` (`courseID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `idExamNumber`
    FOREIGN KEY (`idExamNumber`)
    REFERENCES `test`.`exam` (`examNumber`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `idQuestionNumber`
    FOREIGN KEY (`idQuestionNumber`)
    REFERENCES `test`.`question` (`questionNumber`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

为什么我会犯这个错误?
谢谢。

wswtfjt7

wswtfjt71#

这个约束肯定是错误的:

CONSTRAINT `idExamSubject`
    FOREIGN KEY (`idExamSubject`)
    REFERENCES `test`.`exam` (`subjectID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,

也许你打算:

CONSTRAINT `idExamSubject`
    FOREIGN KEY (`idExamSubject`)
    REFERENCES `test`.`subject` (`subjectID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,

确保在创建外键约束之前创建目标表。引用的表需要首先存在(这样引擎就可以验证用于外键的列上的类型)。

相关问题