MySQL外键约束错误

时间:2014-10-26 18:39:12

标签: mysql foreign-keys

我有四张桌子。前三个中的每一个都应该作为第四个表中的外键引用,替换。

以下是我的顾问表:

CREATE TABLE IF NOT EXISTS `virtual_adviser`.`advisers` (
   `adviser_id` VARCHAR(9) NOT NULL,
   `first_name` VARCHAR(45) NOT NULL,
   `middle_intial` VARCHAR(3) NULL DEFAULT NULL,
   `last_name` VARCHAR(45) NOT NULL,
   `email` VARCHAR(255) NOT NULL,
   `username` VARCHAR(45) NOT NULL,
   `password` VARCHAR(45) NOT NULL,
   PRIMARY KEY (`adviser_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

这是course_list表:

CREATE TABLE IF NOT EXISTS `virtual_adviser`.`course_list` (
   `course_id` VARCHAR(11) NOT NULL,
   `course_name` VARCHAR(225) NOT NULL,
   `credit_hours` TINYINT(2) UNSIGNED NOT NULL,
   PRIMARY KEY (`course_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

学生表:

CREATE TABLE IF NOT EXISTS `virtual_adviser`.`students` (
   `student_id` VARCHAR(9) NOT NULL,
   `first_name` VARCHAR(45) NOT NULL,
   `middle_initial` VARCHAR(3) NULL,
   `last_name` VARCHAR(45) NOT NULL,
   `email` VARCHAR(255) NOT NULL,
   `adviser_id` VARCHAR(9) NOT NULL,
   `program_id` VARCHAR(100) NOT NULL,
   `concentration_id` VARCHAR(100) NOT NULL,
   `outside_concentration_id` VARCHAR(100) NULL DEFAULT NULL,
   `username` VARCHAR(45) NOT NULL,
   `password` VARCHAR(45) NOT NULL,
   PRIMARY KEY (`student_id`),
   INDEX `program_id_idx` (`program_id` ASC),
   INDEX `concentration_id_idx` (`concentration_id` ASC),
   INDEX `outside_concentration_id_idx` (`outside_concentration_id` ASC),
   INDEX `adviser_id` (`adviser_id` ASC),
   INDEX `program_id` (`program_id` ASC, `concentration_id` ASC),
   CONSTRAINT `students_ibfk_1`
   FOREIGN KEY (`adviser_id`)
   REFERENCES `virtual_adviser`.`advisers` (`adviser_id`)
      ON DELETE CASCADE
      ON UPDATE CASCADE,
   CONSTRAINT `students_ibfk_2`
   FOREIGN KEY (`program_id` , `concentration_id`)
   REFERENCES `virtual_adviser`.`degree_program` (`program_id` , `concentration_id`)
      ON DELETE CASCADE
      ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

最后是替换表,导致错误:

CREATE TABLE IF NOT EXISTS `virtual_adviser`.`substitutions` (
   `student_id` VARCHAR(9) NOT NULL,
   `adviser_id` VARCHAR(9) NOT NULL,
   `course_id` VARCHAR(11) NOT NULL,
   `course_sub` VARCHAR(11) NOT NULL,
   PRIMARY KEY (`student_id`,`adviser_id`,`course_id`,`course_sub`),
   INDEX `course_id_idx` (`course_id` ASC),
   INDEX `student_id_idx` (`student_id` ASC),
   INDEX `FK_course_sub_idx` (`course_sub` ASC),
   CONSTRAINT `substitutions_ibfk_1`
   FOREIGN KEY (`student_id`) REFERENCES `virtual_adviser`.`students` (`student_id`)
      ON DELETE CASCADE
      ON UPDATE CASCADE,
   CONSTRAINT `substitutions_ibfk_2`
   FOREIGN KEY (`course_id`) REFERENCES `virtual_adviser`.`course_list` (`course_id`)
      ON DELETE CASCADE
      ON UPDATE CASCADE,
  CONSTRAINT `substitutions_ibfk_3`
  FOREIGN KEY (`course_sub`) REFERENCES `virtual_adviser`.`course_list` (`course_id`)
     ON DELETE CASCADE
     ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

我已尝试单独使用每个外键创建表,但它没有区别,所有这些都会触发相同的错误。我还检查了原始和引用表中的数据类型是否相同,它们是,所以我不相信这是错误的原因。谁能帮我弄清楚这里发生了什么?

0 个答案:

没有答案