我是 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 nj1368843
DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
使用nj1368843
;
- 表nj1368843
。Users
如果存在则删除表nj1368843
。Users
;
如果不存在则创建表nj1368843
。Users
(
idUsers
INT NOT NULL AUTO_INCREMENT ,
UserName
VARCHAR(45) 非空,
pw
VARCHAR(45) 非空,
主键 ( idUsers
, UserName
, pw
) )
引擎 = InnoDB;
插入到nj1368843
. Users
( UserName
, pw
) 值 ('njack2', '123');
- 表nj1368843
。Teachers
如果存在则删除表nj1368843
。Teachers
;
如果不存在则创建表nj1368843
。Teachers
(
idTeachers
INT NOT NULL ,
Lname
VARCHAR(45) 空值,
Fname
VARCHAR(45) 空值,
Users_idUsers
INT NOT NULL ,
Users_pw
VARCHAR(45) 非空,
主键 ( idTeachers
) ,
指数fk_Teachers_Users1
(Users_idUsers
ASC,Users_pw
ASC),
约束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');
- 表nj1368843
。Schedule
如果存在则删除表nj1368843
。Schedule
;
如果不存在则创建表nj1368843
。Schedule
(
course_id
INT NOT NULL ,
Semester
VARCHAR(45) 空值,
Year
年空,
Teachers_idTeachers
INT NOT NULL ,
主键 ( course_id
) ,
指数fk_Grades_Teachers1
( Teachers_idTeachers
ASC) ,
约束fk_Grades_Teachers1
FOREIGN KEY (`Teachers_idTeachers` )
REFERENCES `nj1368843`.`Teachers` (`idTeachers` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
引擎 = InnoDB;
- 表nj1368843
。Assignments
如果存在则删除表nj1368843
。Assignments
;
如果不存在则创建表nj1368843
。Assignments
(
idAssignments
INT NOT NULL ,
Assignment 1
INT NULL ,
AVG_Grade
INT(11)空,
Schedule_course_id
INT NOT NULL ,
主键 ( idAssignments
) ,
指数fk_Assignments_Schedule1
( Schedule_course_id
ASC) ,
约束fk_Assignments_Schedule1
FOREIGN KEY (`Schedule_course_id` )
REFERENCES `nj1368843`.`Schedule` (`course_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
引擎 = InnoDB;
- 表nj1368843
。Student
如果存在则删除表nj1368843
。Student
;
如果不存在则创建表nj1368843
。Student
(
idStudent
INT NOT NULL ,
lname
VARCHAR(45) 空值,
fname
VARCHAR(45) 空值,
Schedule_course_id
INT NOT NULL ,
Users_idUsers
INT NOT NULL ,
Users_pw
VARCHAR(45) 非空,
Assignments_idAssignments
INT NOT NULL ,
主键 ( idStudent
) ,
指数fk_Student_Schedule1
( Schedule_course_id
ASC) ,
指数fk_Student_Users1
(Users_idUsers
ASC,Users_pw
ASC),
指数fk_Student_Assignments1
( Assignments_idAssignments
ASC) ,
约束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;
- 表nj1368843
。Classes
如果存在则删除表nj1368843
。Classes
;
如果不存在则创建表nj1368843
。Classes
(
cid
INT NOT NULL ,
Name
VARCHAR(45) 空值,
Schedule_course_id
INT NOT NULL ,
主键 ( cid
) ,
指数fk_Classes_Schedule1
( Schedule_course_id
ASC) ,
约束fk_Classes_Schedule1
FOREIGN KEY (`Schedule_course_id` )
REFERENCES `nj1368843`.`Schedule` (`course_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
引擎 = InnoDB;
- 表nj1368843
。Teachers
如果存在则删除表nj1368843
。Teachers
;
如果不存在则创建表nj1368843
。Teachers
(
idTeachers
INT NOT NULL ,
Lname
VARCHAR(45) 空值,
Fname
VARCHAR(45) 空值,
Users_idUsers
INT NOT NULL ,
Users_pw
VARCHAR(45) 非空,
主键 ( idTeachers
) ,
指数fk_Teachers_Users1
(Users_idUsers
ASC,Users_pw
ASC),
约束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_Users1
FOREIGN 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 的任何表中。