3

我已经阅读了大约十亿次,但我无法弄清楚......我在工作台中生成了这个,所以它应该可以工作,但它会创建除 SECTIONS 之外的所有表......如果我删除它们之间的关系,它将创建它SECTION 和 INSTRUCTOR 但我需要这种关系才能发挥作用....任何帮助将不胜感激!代码如下...希望有人可以帮助我!谢谢!

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

CREATE SCHEMA IF NOT EXISTS `385_s11_turpinvp` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `385_s11_turpinvp` ;

-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`TUTORS`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`TUTORS` (
  `Name` VARCHAR(45) NOT NULL ,
  `Banner_Id` CHAR(8) NOT NULL ,
  `Email` VARCHAR(45) NOT NULL ,
  `Ssn` CHAR(9) NOT NULL ,
  `Address` VARCHAR(45) NOT NULL ,
  `Phone` INT NOT NULL ,
  PRIMARY KEY (`Banner_Id`) )
ENGINE = InnoDB;

CREATE UNIQUE INDEX `Ssn_UNIQUE` ON `385_s11_turpinvp`.`TUTORS` (`Ssn` ASC) ;

CREATE UNIQUE INDEX `Banner_Id_UNIQUE` ON `385_s11_turpinvp`.`TUTORS` (`Banner_Id` ASC) ;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`STUDENTS`
-    - -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`STUDENTS` (
  `Name` VARCHAR(45) NOT NULL ,
  `Banner_Id` CHAR(8) NOT NULL ,
  `Email` VARCHAR(45) NOT NULL ,
  `Phone` INT NOT NULL ,
  PRIMARY KEY (`Banner_Id`) )
ENGINE = InnoDB;

CREATE UNIQUE INDEX `Banner_Id_UNIQUE` ON `385_s11_turpinvp`.`STUDENTS` (`Banner_Id` ASC) ;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`INSTRUCTORS`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`INSTRUCTORS` (
  `Name` VARCHAR(45) NOT NULL ,
  `Banner_Id` CHAR(8) NOT NULL ,
  `Email` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`Banner_Id`, `Name`) )
ENGINE = InnoDB;

CREATE UNIQUE INDEX `Banner_Id_UNIQUE` ON `385_s11_turpinvp`.`INSTRUCTORS` (`Banner_Id` ASC) ;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`COURSES`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`COURSES` (
  `Course_Id` VARCHAR(45) NOT NULL ,
  `Course_Name` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`Course_Id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`APPOINTMENTS`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`APPOINTMENTS` (
  `Courses_Course_Id` VARCHAR(45) NOT NULL ,
  `TUTORS_Banner_Id` CHAR(8) NOT NULL ,
  `STUDENTS_Banner_Id` CHAR(8) NOT NULL ,
      `Start_Time` DATETIME NOT NULL ,
  `End_Time` DATETIME NOT NULL ,
  PRIMARY KEY (`Courses_Course_Id`, `TUTORS_Banner_Id`, `STUDENTS_Banner_Id`, `Start_Time`, `End_Time`) ,
  CONSTRAINT `fk_COURSES_has_TUTORS_Courses1`
    FOREIGN KEY (`Courses_Course_Id` )
    REFERENCES `385_s11_turpinvp`.`COURSES` (`Course_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_COURSES_has_TUTORS_TUTORS1`
    FOREIGN KEY (`TUTORS_Banner_Id` )
    REFERENCES `385_s11_turpinvp`.`TUTORS` (`Banner_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_APPOINTMENTS_STUDENTS1`
    FOREIGN KEY (`STUDENTS_Banner_Id` )
    REFERENCES `385_s11_turpinvp`.`STUDENTS` (`Banner_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE INDEX `fk_COURSES_has_TUTORS_TUTORS1` ON `385_s11_turpinvp`.`APPOINTMENTS` (`TUTORS_Banner_Id` ASC) ;

CREATE INDEX `fk_COURSES_has_TUTORS_Courses1` ON `385_s11_turpinvp`.`APPOINTMENTS` (`Courses_Course_Id` ASC) ;

CREATE INDEX `fk_APPOINTMENTS_STUDENTS1` ON `385_s11_turpinvp`.`APPOINTMENTS` (`STUDENTS_Banner_Id` ASC) ;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`QUALIFIED`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`QUALIFIED` (
  `Courses_Course_Id` VARCHAR(45) NOT NULL ,
  `TUTORS_Banner_Id` CHAR(8) NOT NULL ,
  PRIMARY KEY (`Courses_Course_Id`, `TUTORS_Banner_Id`) ,
  CONSTRAINT `fk_Courses_has_TUTORS_Courses2`
    FOREIGN KEY (`Courses_Course_Id` )
    REFERENCES `385_s11_turpinvp`.`COURSES` (`Course_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_Courses_has_TUTORS_TUTORS2`
    FOREIGN KEY (`TUTORS_Banner_Id` )
    REFERENCES `385_s11_turpinvp`.`TUTORS` (`Banner_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE INDEX `fk_Courses_has_TUTORS_TUTORS2` ON `385_s11_turpinvp`.`QUALIFIED` (`TUTORS_Banner_Id` ASC) ;

CREATE INDEX `fk_Courses_has_TUTORS_Courses2` ON `385_s11_turpinvp`.`QUALIFIED` (`Courses_Course_Id` ASC) ;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`Availability`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`Availability` (
  `Start_Time` DATETIME NOT NULL ,
  `End_time` DATETIME NOT NULL ,
  `TUTORS_Banner_Id` CHAR(8) NOT NULL ,
  PRIMARY KEY (`TUTORS_Banner_Id`, `End_time`, `Start_Time`) ,
  CONSTRAINT `fk_Availability_TUTORS1`
    FOREIGN KEY (`TUTORS_Banner_Id` )
    REFERENCES `385_s11_turpinvp`.`TUTORS` (`Banner_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`SECTIONS`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`SECTIONS` (
  `COURSES_Course_Id` VARCHAR(45) NOT NULL ,
  `INSTRUCTORS_Name` VARCHAR(45) NOT NULL ,
  `Section_Id` CHAR(1) NOT NULL ,
  PRIMARY KEY (`COURSES_Course_Id`, `INSTRUCTORS_Name`, `Section_Id`) ,
  CONSTRAINT `fk_COURSES_has_ENROLLED_COURSES1`
    FOREIGN KEY (`COURSES_Course_Id` )
    REFERENCES `385_s11_turpinvp`.`COURSES` (`Course_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_SECTIONS_INSTRUCTORS1`
    FOREIGN KEY (`INSTRUCTORS_Name` )
    REFERENCES `385_s11_turpinvp`.`INSTRUCTORS` (`Name` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE INDEX `fk_COURSES_has_ENROLLED_COURSES1` ON `385_s11_turpinvp`.`SECTIONS` (`COURSES_Course_Id` ASC) ;

CREATE INDEX `fk_SECTIONS_INSTRUCTORS1` ON `385_s11_turpinvp`.`SECTIONS` (`INSTRUCTORS_Name` ASC) ;


-- -----------------------------------------------------
-- Table `385_s11_turpinvp`.`ENROLLED`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `385_s11_turpinvp`.`ENROLLED` (
  `STUDENTS_Banner_Id` CHAR(8) NOT NULL ,
  `COURSES_Course_Id` VARCHAR(45) NOT NULL ,
  `SECTIONS_Section_Id` CHAR(1) NOT NULL ,
  PRIMARY KEY (`STUDENTS_Banner_Id`, `COURSES_Course_Id`, `SECTIONS_Section_Id`) ,
  CONSTRAINT `fk_COURSES_has_STUDENTS_STUDENTS1`
    FOREIGN KEY (`STUDENTS_Banner_Id` )
    REFERENCES `385_s11_turpinvp`.`STUDENTS` (`Banner_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_ENROLLED_COURSES1`
    FOREIGN KEY (`COURSES_Course_Id` )
    REFERENCES `385_s11_turpinvp`.`COURSES` (`Course_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_ENROLLED_SECTIONS1`
    FOREIGN KEY (`SECTIONS_Section_Id` )
    REFERENCES `385_s11_turpinvp`.`SECTIONS` (`Section_Id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE INDEX `fk_COURSES_has_STUDENTS_STUDENTS1` ON `385_s11_turpinvp`.`ENROLLED` (`STUDENTS_Banner_Id` ASC) ;

CREATE INDEX `fk_ENROLLED_COURSES1` ON `385_s11_turpinvp`.`ENROLLED` (`COURSES_Course_Id` ASC) ;

C    REATE INDEX `fk_ENROLLED_SECTIONS1` ON `385_s11_turpinvp`.`ENROLLED` (`SECTIONS_Section_Id` ASC) ;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
4

1 回答 1

13

要获取有关外键错误的详细信息,请运行SHOW ENGINE INNODB STATUS\G并查看“最新外键错误”部分。

它应该告诉您外键无效,因为 . 上没有唯一索引或主键索引INSTRUCTORS.Name。主键是INSTRUCTORS.(Banner_Id, Name),并且您在INSTRUCTORS.Banner_Id. 您要么需要为外键使用这些列组合之一,要么在INSTRUCTORS.Name

于 2011-04-11T18:27:27.320 回答