没有添加具有多个约束的init.sql表

时间:2019-04-09 03:44:12

标签: mysql database

我正在创建数据库,但是我注意到并没有创建具有多个约束行的表。

我尝试在CONSTRAINT语句的末尾添加逗号。这没用。

最初的代码是由MySQL Workbench编写的,但是我发现最好手动完成大多数工作,因此我删除了很多内容,并自己编写了其余代码。似乎只添加了一个或没有CONSTRAINT语句的表就可以了。

在CONSTRAINT部分添加逗号。在Google和W3Schools上搜索。

-- 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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, 
   ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
    */

CREATE DATABASE studentdb;

    use studentdb;

CREATE SCHEMA IF NOT EXISTS studentdb DEFAULT CHARACTER SET utf8 ;
    USE studentdb ;


CREATE TABLE IF NOT EXISTS studentdb.student (
  idStudent INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  firstName VARCHAR(45) NULL,
  lastName VARCHAR(45) NULL,
  address VARCHAR(45) NULL,
  phone VARCHAR(45) NULL,
  studentEmail VARCHAR(45) NULL,
  personalEmail VARCHAR(45) NULL,
  interest VARCHAR(45) NULL,
  active BIT(0) NULL,
  program VARCHAR(45) NULL

  );


CREATE TABLE IF NOT EXISTS studentdb.jobs (
  idjobs INT /*UNSIGNED*/ NOT NULL AUTO_INCREMENT PRIMARY KEY,
  idStudent INT NULL,
  /*jobActive BOOLEAN,*/

  /*UNIQUE INDEX idStudent_UNIQUE (idStudent ASC) VISIBLE,*/
  CONSTRAINT idStudent
    FOREIGN KEY (idStudent)
    REFERENCES studentdb.student (idStudent)
    ON DELETE CASCADE
    ON UPDATE CASCADE

    );

CREATE TABLE IF NOT EXISTS studentdb.notes (
  idNotes INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  idStudentNotes INT NULL,
  /*UNIQUE INDEX idNotes_UNIQUE (idNotes ASC) VISIBLE,
  UNIQUE INDEX idStudent_UNIQUE (idStudent ASC) VISIBLE,*/
  CONSTRAINT idStudentNotes
    FOREIGN KEY (idStudentNotes)
    REFERENCES studentdb.student (idStudent)
    ON DELETE CASCADE
    ON UPDATE CASCADE

    );


CREATE TABLE IF NOT EXISTS studentdb.graduated (
  idgraduated  BIT(0) NOT NULL PRIMARY KEY,
  idStudentGraduated INT NOT NULL,

  CONSTRAINT idStudentGraduated
    FOREIGN KEY (idStudentGraduated)
    REFERENCES studentdb.student (idStudent)
    ON DELETE CASCADE
    ON UPDATE CASCADE

    );


CREATE TABLE IF NOT EXISTS studentdb.internship (
  idinternship INT /*UNSIGNED*/ NOT NULL PRIMARY KEY,
  idStudentInternship INT NULL,
  internshipCompany VARCHAR(45) NULL,
  internshipSemester VARCHAR(45) NULL,

   /*INDEX idStudent_idx (idStudent ASC) VISIBLE,*/
  CONSTRAINT idStudentInternship
    FOREIGN KEY (idStudentInternship)
    REFERENCES studentdb.student (idStudent)
    ON DELETE CASCADE
    ON UPDATE CASCADE

);

CREATE TABLE IF NOT EXISTS studentdb.courseSubstitutions (
  idcourseSubstitutions INT UNSIGNED NOT NULL PRIMARY KEY,
  courseSubstitutionsClass VARCHAR(45) NULL,
  idStudentCourseSub INT NULL,

  /*INDEX idStudent_idx (idStudent ASC) VISIBLE,*/
  CONSTRAINT idStudentCourseSub
    FOREIGN KEY (idStudentCourseSub)
    REFERENCES studentdb.student (idStudent)
    ON DELETE CASCADE
    ON UPDATE CASCADE

    );

CREATE TABLE IF NOT EXISTS studentdb.course (
    idCourse INT NOT NULL PRIMARY KEY,
    idStudentCourse INT NULL,
    name varchar(255),
    description varchar(255),
    Prereq varchar(255),
    Coreq varchar(255),
    Credits INT NULL,

    CONSTRAINT idStudentCourse
      FOREIGN KEY (idStudentCourse)
      REFERENCES studentdb.student (idStudent)
      ON DELETE CASCADE
      ON UPDATE CASCADE

    );

CREATE TABLE IF NOT EXISTS studentdb.teacher (
    idTeacher INT NOT NULL PRIMARY KEY,
    idStudentTeacher INT NULL,
    firstName varchar(255),
    lastName varchar(255),
    course varchar(255),

    CONSTRAINT idStudentTeacher
      FOREIGN KEY (idStudentTeacher)
      REFERENCES studentdb.student (idStudent)
      ON DELETE CASCADE
      ON UPDATE CASCADE

    );

CREATE TABLE IF NOT EXISTS studentdb.class (
    idClass INT NOT NULL PRIMARY KEY,
    idStudentClass INT NULL,
    idCourseClass INT NULL,
    idTeacherClass INT NULL,
    deliveryMethod varchar(255),
    semester varchar(255),
    schoolYear date(),
    grade DECIMAL(1, 3),

    CONSTRAINT idStudentClass
      FOREIGN KEY (idStudentClass)
      REFERENCES studentdb.student (idStudent)
      ON DELETE CASCADE
      ON UPDATE CASCADE,

    CONSTRAINT idCourseClass
      FOREIGN KEY (idCourseClass)
      REFERENCES studentdb.class (idClass)
      ON DELETE CASCADE
      ON UPDATE CASCADE,

     CONSTRAINT idTeacherClass
       FOREIGN KEY (idTeacherClass)
       REFERENCES studentdb.teacher (idTeacher)
       ON DELETE CASCADE
       ON UPDATE CASCADE

    );


CREATE TABLE IF NOT EXISTS studentdb.studentProgram
    idStudnetProgram INT NOT NULL PRIMARY KEY,
    idStudentStudentProgram INT NULL,
    idStudentProgramProgram INT NULL,
    startDate DATE(),
    endDate DATE(),

     CONSTRAINT idStudentStudentProgram
        FOREIGN KEY (idStudentStudentProgram)
        REFERENCES studentdb.student (idStudent)
        ON DELETE CASCADE
        ON UPDATE CASCADE,

     CONSTRAINT idStudentProgramProgram 
      FOREIGN KEY (idStudnetProgramProgram)
      REFERENCES studentdb.program (idProgram)
      ON DELETE CASCADE
      ON UPDATE CASCADE
    );

我希望添加表格。

0 个答案:

没有答案