1

我是 mysql 新手,我正在尝试创建一个成绩簿数据库来跟踪某个班级的成绩。我正在使用 Mysql 工作台,这是我的代码:

设置@OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS,UNIQUE_CHECKS=0;

设置@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0;

SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

如果存在则删除模式nj1368843

CREATE SCHEMA IF NOT EXISTS nj1368843DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;

使用nj1368843


- 表nj1368843Users


如果存在则删除表nj1368843Users;

如果不存在则创建表nj1368843Users(

idUsersINT NOT NULL AUTO_INCREMENT ,

UserNameVARCHAR(45) 非空,

pwVARCHAR(45) 非空,

主键 ( idUsers, UserName, pw) )

引擎 = InnoDB;

插入到nj1368843. Users( UserName, pw) 值 ('njack2', '123');


- 表nj1368843Teachers


如果存在则删除表nj1368843Teachers;

如果不存在则创建表nj1368843Teachers(

idTeachersINT NOT NULL ,

LnameVARCHAR(45) 空值,

FnameVARCHAR(45) 空值,

Users_idUsersINT NOT NULL ,

Users_pwVARCHAR(45) 非空,

主键 ( idTeachers) ,

指数fk_Teachers_Users1Users_idUsersASC,Users_pwASC),

约束fk_Teachers_Users1

FOREIGN KEY (`Users_idUsers` , `Users_pw` )

REFERENCES `nj1368843`.`Users` (`idUsers` , `UserName` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

引擎 = InnoDB;

插入到nj1368843. Teachers( idTeachers, Lname, Fname, Users_idUsers, Users_pw) 值 (105, 'Stacey', 'Sheila', '1', '123');


- 表nj1368843Schedule


如果存在则删除表nj1368843Schedule;

如果不存在则创建表nj1368843Schedule(

course_idINT NOT NULL ,

SemesterVARCHAR(45) 空值,

Year年空,

Teachers_idTeachersINT NOT NULL ,

主键 ( course_id) ,

指数fk_Grades_Teachers1( Teachers_idTeachersASC) ,

约束fk_Grades_Teachers1

FOREIGN KEY (`Teachers_idTeachers` )

REFERENCES `nj1368843`.`Teachers` (`idTeachers` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

引擎 = InnoDB;


- 表nj1368843Assignments


如果存在则删除表nj1368843Assignments;

如果不存在则创建表nj1368843Assignments(

idAssignmentsINT NOT NULL ,

Assignment 1INT NULL ,

AVG_GradeINT(11)空,

Schedule_course_idINT NOT NULL ,

主键 ( idAssignments) ,

指数fk_Assignments_Schedule1( Schedule_course_idASC) ,

约束fk_Assignments_Schedule1

FOREIGN KEY (`Schedule_course_id` )

REFERENCES `nj1368843`.`Schedule` (`course_id` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

引擎 = InnoDB;


- 表nj1368843Student


如果存在则删除表nj1368843Student;

如果不存在则创建表nj1368843Student(

idStudentINT NOT NULL ,

lnameVARCHAR(45) 空值,

fnameVARCHAR(45) 空值,

Schedule_course_idINT NOT NULL ,

Users_idUsersINT NOT NULL ,

Users_pwVARCHAR(45) 非空,

Assignments_idAssignmentsINT NOT NULL ,

主键 ( idStudent) ,

指数fk_Student_Schedule1( Schedule_course_idASC) ,

指数fk_Student_Users1Users_idUsersASC,Users_pwASC),

指数fk_Student_Assignments1( Assignments_idAssignmentsASC) ,

约束fk_Student_Schedule1

FOREIGN KEY (`Schedule_course_id` )

REFERENCES `nj1368843`.`Schedule` (`course_id` )

ON DELETE NO ACTION

ON UPDATE NO ACTION,

约束fk_Student_Users1

FOREIGN KEY (`Users_idUsers` , `Users_pw` )

REFERENCES `nj1368843`.`Users` (`idUsers` , `UserName` )

ON DELETE NO ACTION

ON UPDATE NO ACTION,

约束fk_Student_Assignments1

FOREIGN KEY (`Assignments_idAssignments` )

REFERENCES `nj1368843`.`Assignments` (`idAssignments` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

引擎 = InnoDB;


- 表nj1368843Classes


如果存在则删除表nj1368843Classes;

如果不存在则创建表nj1368843Classes(

cidINT NOT NULL ,

NameVARCHAR(45) 空值,

Schedule_course_idINT NOT NULL ,

主键 ( cid) ,

指数fk_Classes_Schedule1( Schedule_course_idASC) ,

约束fk_Classes_Schedule1

FOREIGN KEY (`Schedule_course_id` )

REFERENCES `nj1368843`.`Schedule` (`course_id` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

引擎 = InnoDB;


- 表nj1368843Teachers


如果存在则删除表nj1368843Teachers;

如果不存在则创建表nj1368843Teachers(

idTeachersINT NOT NULL ,

LnameVARCHAR(45) 空值,

FnameVARCHAR(45) 空值,

Users_idUsersINT NOT NULL ,

Users_pwVARCHAR(45) 非空,

主键 ( idTeachers) ,

指数fk_Teachers_Users1Users_idUsersASC,Users_pwASC),

约束fk_Teachers_Users1

FOREIGN KEY (`Users_idUsers` , `Users_pw` )

REFERENCES `nj1368843`.`Users` (`idUsers` , `UserName` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

引擎 = InnoDB;

SET SQL_MODE=@OLD_SQL_MODE;

SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

我从 erd 图生成了这个,我无法在数据库中插入任何信息,因为我得到:

错误 1452:无法添加或更新子行:外键约束失败(nj1368843. Teachers, CONSTRAINT fk_Teachers_Users1FOREIGN KEY ( Users_idUsers, Users_pw) REFERENCES Users( idUsers, UserName) ON DELETE NO ACTION ON UPDATE NO ACTION)

SQL 语句:

插入到nj1368843. Teachers( idTeachers, Lname, Fname, Users_idUsers, Users_pw) 值 (105, 'Stacey', 'Sheila', 1, '123')

我尝试了每个人的想法,将代码查看了一百万次,仍然找不到问题。我无法插入此 db.help 的任何表中。

4

0 回答 0