MySQL Workbench“errno 150外键”

时间:2017-05-09 20:28:54

标签: mysql mysql-workbench

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';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE 
utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`Diploma`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Diploma` (
`diploma_id` VARCHAR(5) NOT NULL,
`diploma_name` VARCHAR(90) NULL,
PRIMARY KEY (`diploma_id`)
)  ENGINE=InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`School`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`School` (
`school_id` INT(1) NOT NULL,
`school_name` VARCHAR(45) NULL,
PRIMARY KEY (`school_id`)
)  ENGINE=InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Student`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Student` (
`student_number` INT(8) NOT NULL,
`student_id` INT(8) NULL,
`student_name` VARCHAR(45) NULL,
`student_password` VARCHAR(45) NULL,
`student_mobile` INT(8) NULL,
`student_email` VARCHAR(45) NULL,
`diploma_id` VARCHAR(5) NOT NULL,
`school_id` INT(1) NOT NULL,
PRIMARY KEY (`student_number`),
INDEX `fk_Student_Diploma1_idx` (`diploma_id` ASC),
INDEX `fk_Student_School1_idx` (`school_id` ASC),
CONSTRAINT `fk_Student_Diploma1` FOREIGN KEY (`diploma_id`)
    REFERENCES `mydb`.`Diploma` (`diploma_id`)
    ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Student_School1` FOREIGN KEY (`school_id`)
    REFERENCES `mydb`.`School` (`school_id`)
    ON DELETE NO ACTION ON UPDATE NO ACTION
)  ENGINE=InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`OFN`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`OFN` (
`ofn_id` INT(8) NOT NULL,
`ofn_username` VARCHAR(45) NULL,
`ofn_password` VARCHAR(45) NULL,
`ofn_email` VARCHAR(45) NULL,
PRIMARY KEY (`ofn_id`)
)  ENGINE=InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Appointment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Appointment` (
`appointment_id` INT(8) NOT NULL,
`appointment_date` DATE NULL,
`appointment_time` TIME NULL,
`ofn_id` INT(8) NOT NULL,
PRIMARY KEY (`appointment_id`),
INDEX `fk_Appointment_OFN1_idx` (`ofn_id` ASC),
CONSTRAINT `fk_Appointment_OFN1` FOREIGN KEY (`ofn_id`)
    REFERENCES `mydb`.`OFN` (`ofn_id`)
    ON DELETE NO ACTION ON UPDATE NO ACTION
)  ENGINE=InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`Booking`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Booking` (
`student_number` INT(8) NOT NULL,
`appointment_id` INT(8) NOT NULL,
`booking_date` DATE NULL,
`booking_time` TIME NULL,
PRIMARY KEY (`student_number` , `appointment_id`),
INDEX `fk_Student_has_Appointment_Appointment1_idx` (`appointment_id` ASC),
INDEX `fk_Student_has_Appointment_Student1_idx` (`student_number` ASC),
CONSTRAINT `fk_Student_has_Appointment_Student1` FOREIGN KEY 
 (`student_number`)
    REFERENCES `mydb`.`Student` (`student_number`)
    ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Student_has_Appointment_Appointment1` FOREIGN KEY (`appointment_id`)
    REFERENCES `mydb`.`Appointment` (`appointment_id`)
    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;

这是我正在构建的代码,但它给出了一个错误 错误代码:1005:无法创建表'mydb'。'预订' 我试图通过遇到同样问题的人查看前面的一些问题,但我仍然坚持这一点。 非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

不知何故,它是外键student_number。

我正在思考这些可能性:

  1. 外键名称是已存在键的副本。检查外键的名称在数据库中是否唯一。只需在密钥名称的末尾添加一些随机字符即可对其进行测试。

  2. 您的一个或两个表是MyISAM表。为了使用外键,表必须都是InnoDB。 (实际上,如果两个表都是MyISAM,那么您将不会收到错误消息 - 它只是不会创建密钥。)在查询浏览器中,您可以指定表类型。

  3. 您尝试引用的关键字段之一没有索引和/或不是主键。如果关系中的某个字段不是主键,则必须为该字段创建索引。

  4. 同时检查: SET FOREIGN_KEY_CHECKS=0;