错误1452:外键错误

时间:2013-08-10 02:40:46

标签: mysql-error-1452

我是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;


- 表nj1368843Users


DROP TABLE IF EXISTS nj1368843Users;

CREATE TABLE IF NOT NOT EXISTS nj1368843Users

idUsers INT NOT NULL AUTO_INCREMENT,

UserName VARCHAR(45)NOT NULL,

pw VARCHAR(45)NOT NULL,

PRIMARY KEY(idUsersUserNamepw))

ENGINE = InnoDB;

INSERT INTO nj1368843UsersUserNamepw)VALUES('njack2','123');


- 表nj1368843Teachers


DROP TABLE IF EXISTS nj1368843Teachers;

CREATE TABLE IF NOT NOT EXISTS nj1368843Teachers

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_Users1Users_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 nj1368843TeachersidTeachersLnameFnameUsers_idUsersUsers_pw)价值观(105 ,'Stacey','Sheila','1','123');


- 表nj1368843Schedule


DROP TABLE IF EXISTS nj1368843Schedule;

CREATE TABLE IF NOT NOT EXISTS nj1368843Schedule

course_id INT NOT NULL,

Semester VARCHAR(45)NULL,

Year YEAR NULL,

Teachers_idTeachers INT NOT NULL,

PRIMARY KEY(course_id),

INDEX fk_Grades_Teachers1Teachers_idTeachers ASC),

约束fk_Grades_Teachers1

FOREIGN KEY (`Teachers_idTeachers` )

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

ON DELETE NO ACTION

ON UPDATE NO ACTION)

ENGINE = InnoDB;


- 表nj1368843Assignments


DROP TABLE IF EXISTS nj1368843Assignments;

CREATE TABLE IF NOT NOT EXISTS nj1368843Assignments

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


- 表nj1368843Student


DROP TABLE IF EXISTS nj1368843Student;

CREATE TABLE IF NOT NOT EXISTS nj1368843Student

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_Schedule1Schedule_course_id ASC),

INDEX fk_Student_Users1Users_idUsers ASC,Users_pw ASC),

INDEX fk_Student_Assignments1Assignments_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;


- 表nj1368843Classes


DROP TABLE IF EXISTS nj1368843Classes;

CREATE TABLE IF NOT NOT EXISTS nj1368843Classes

cid INT NOT NULL,

Name VARCHAR(45)NULL,

Schedule_course_id INT NOT NULL,

PRIMARY KEY(cid),

INDEX fk_Classes_Schedule1Schedule_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;


- 表nj1368843Teachers


DROP TABLE IF EXISTS nj1368843Teachers;

CREATE TABLE IF NOT NOT EXISTS nj1368843Teachers

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_Users1Users_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:无法添加或更新子行:外键约束失败(nj1368843Teachers,CONSTRAINT fk_Teachers_Users1 FOREIGN KEY(Users_idUsers,{{1参考文献Users_pwUsersidUsers)在更新时不执行任何操作

SQL语句:

INSERT INTO UserNamenj1368843TeachersidTeachersLnameFnameUsers_idUsers)价值观(105 ,'Stacey','Sheila',1,'123')

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

0 个答案:

没有答案