我是mysql的新手,我正在尝试创建一个成绩簿数据库来跟踪某个班级的成绩。我正在使用Mysql工作台,这是我的代码:
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';
DROP SCHEMA IF EXISTS nj1368843
;
创建模式,如果不是EXISTS nj1368843
默认字符集latin1 COLLATE latin1_swedish_ci;
USE nj1368843
;
- 表nj1368843
。Users
DROP TABLE IF EXISTS nj1368843
。Users
;
CREATE TABLE IF NOT NOT EXISTS nj1368843
。Users
(
idUsers
INT NOT NULL AUTO_INCREMENT,
UserName
VARCHAR(45)NOT NULL,
pw
VARCHAR(45)NOT NULL,
PRIMARY KEY(idUsers
,UserName
,pw
))
ENGINE = InnoDB;
INSERT INTO nj1368843
。Users
(UserName
,pw
)VALUES('njack2','123');
- 表nj1368843
。Teachers
DROP TABLE IF EXISTS nj1368843
。Teachers
;
CREATE TABLE IF NOT NOT EXISTS nj1368843
。Teachers
(
idTeachers
INT NOT NULL,
Lname
VARCHAR(45)NULL,
Fname
VARCHAR(45)NULL,
Users_idUsers
INT NOT NULL,
Users_pw
VARCHAR(45)NOT NULL,
PRIMARY KEY(idTeachers
),
INDEX 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)
ENGINE = InnoDB;
INSERT INTO nj1368843
。Teachers
(idTeachers
,Lname
,Fname
,Users_idUsers
,Users_pw
)价值观(105 ,'Stacey','Sheila','1','123');
- 表nj1368843
。Schedule
DROP TABLE IF EXISTS nj1368843
。Schedule
;
CREATE TABLE IF NOT NOT EXISTS nj1368843
。Schedule
(
course_id
INT NOT NULL,
Semester
VARCHAR(45)NULL,
Year
YEAR NULL,
Teachers_idTeachers
INT NOT NULL,
PRIMARY KEY(course_id
),
INDEX 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)
ENGINE = InnoDB;
- 表nj1368843
。Assignments
DROP TABLE IF EXISTS nj1368843
。Assignments
;
CREATE TABLE IF NOT NOT EXISTS nj1368843
。Assignments
(
idAssignments
INT NOT NULL,
Assignment 1
INT NULL,
AVG_Grade
INT(11)NULL,
Schedule_course_id
INT NOT NULL,
PRIMARY KEY(idAssignments
),
INDEX 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)
ENGINE = InnoDB;
- 表nj1368843
。Student
DROP TABLE IF EXISTS nj1368843
。Student
;
CREATE TABLE IF NOT NOT EXISTS nj1368843
。Student
(
idStudent
INT NOT NULL,
lname
VARCHAR(45)NULL,
fname
VARCHAR(45)NULL,
Schedule_course_id
INT NOT NULL,
Users_idUsers
INT NOT NULL,
Users_pw
VARCHAR(45)NOT NULL,
Assignments_idAssignments
INT NOT NULL,
PRIMARY KEY(idStudent
),
INDEX fk_Student_Schedule1
(Schedule_course_id
ASC),
INDEX fk_Student_Users1
(Users_idUsers
ASC,Users_pw
ASC),
INDEX 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)
ENGINE = InnoDB;
- 表nj1368843
。Classes
DROP TABLE IF EXISTS nj1368843
。Classes
;
CREATE TABLE IF NOT NOT EXISTS nj1368843
。Classes
(
cid
INT NOT NULL,
Name
VARCHAR(45)NULL,
Schedule_course_id
INT NOT NULL,
PRIMARY KEY(cid
),
INDEX 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)
ENGINE = InnoDB;
- 表nj1368843
。Teachers
DROP TABLE IF EXISTS nj1368843
。Teachers
;
CREATE TABLE IF NOT NOT EXISTS nj1368843
。Teachers
(
idTeachers
INT NOT NULL,
Lname
VARCHAR(45)NULL,
Fname
VARCHAR(45)NULL,
Users_idUsers
INT NOT NULL,
Users_pw
VARCHAR(45)NOT NULL,
PRIMARY KEY(idTeachers
),
INDEX 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)
ENGINE = InnoDB;
SET SQL_MODE = @ OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS = @ OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS = @ OLD_UNIQUE_CHECKS;
我是从一个错误的图表生成的,我不能在数据库中插入任何信息,因为我得到:
错误1452:无法添加或更新子行:外键约束失败(nj1368843
。Teachers
,CONSTRAINT fk_Teachers_Users1
FOREIGN KEY(Users_idUsers
,{{1参考文献Users_pw
(Users
,idUsers
)在更新时不执行任何操作
SQL语句:
INSERT INTO UserName
。nj1368843
(Teachers
,idTeachers
,Lname
,Fname
,Users_idUsers
)价值观(105 ,'Stacey','Sheila',1,'123')
我尝试了每个人的想法,并查看了一百万次代码,仍然无法找到问题。我无法插入此db.help的任何表中。