问题描述
将数据插入空表,但出现错误 1452.我不确定 MySQL 为什么在错误中提到 NameInfo 表.
Inserting data into an empty table, but got error 1452. I am not sure why MySQL mentions the NameInfo table within the error.
CREATE TABLE NameInfo (
Language VARCHAR(7) NOT NULL,
Status VARCHAR(13) NOT NULL,
Standard VARCHAR(13) NOT NULL,
Name VARCHAR(13) NOT NULL,
Name_ID INT(4) NOT NULL,
Place_ID INT(9) NOT NULL,
Supplier_ID INT(4) NOT NULL,
Date_Supplied DATE NOT NULL,
PRIMARY KEY (Name_ID),
FOREIGN KEY (Supplier_ID) REFERENCES Supplier(Supplier_ID),
FOREIGN KEY (Place_ID) REFERENCES Place(Place_ID)
);
CREATE TABLE Departments (
Dept_ID INT(6) NOT NULL,
Dept_NAME VARCHAR(25) NOT NULL,
DeptHead_ID INT(6) NOT NULL,
DeptAA VARCHAR(20) NOT NULL,
ParentDept_ID INT(4) NOT NULL,
Location VARCHAR(10) NOT NULL,
DeptType VARCHAR(12) NOT NULL,
Primary key (Dept_ID)
);
CREATE TABLE Employee (
Emp_ID INT(6) NOT NULL,
Name VARCHAR(15) NOT NULL,
Dept_ID INT(6) NOT NULL,
Tax_ID INT(4) NOT NULL,
Country VARCAR(15) NOT NULL,
Hire_Date DATE NOT NULL,
Birth_Date DATE NOT NULL,
Salary INT(6) NOT NULL,
Bonus INT(6) NOT NULL,
AddressInfo VARCHAR(30) NOT NULL,
PRIMARY KEY(Emp_ID),
FOREIGN KEY(Dept_ID) REFERENCES Departments(Dept_ID)
);
在子表 Employee 之前将数据插入到父表 Departments.
Inserted data to parent table, Departments, before child table, Employee.
INSERT INTO Departments
VALUES (040124,'Human Resource Division',405802,'Mohammed Siddiqui',1001,'California','HR');
INSERT INTO Employee
VALUES (901126,'Kenneth Tran',040126,3013,'United States',06/01/2013,06/01/1992,80430,500,'N. 2nd St. Santa Clara, CA.');
ERROR 1452 (23000): Cannot add or update a child: a foreign key constraint fails ('namesinc'_'employee', CONSTRAINT 'employee-ibfk_1 'FOREIGN KEY ('Dept_ID') REFERENCES 'DEPARTMENTS' ('DEPT_ID'))
如果我可以提供更多信息,请告诉我.
Please let me know if I can provide additional information.
推荐答案
错误 1452 表示插入失败,因为无法遵守外键约束.
Error 1452 indicates an insertion failed because a foreign key constraint couldn't be honoured.
您的查询将数据插入到 Employee
表中,该表具有引用 Departments
表的外键约束.如果您没有设置 Employeee
行引用的 Department
条目,则插入将失败.
Your query is inserting data into the Employee
table, which has a foreign key constraint referring to the Departments
table. If you haven't got a Department
entry set up that the Employeee
row refers to the insertion will fail.
您需要先插入 Departments
条目,否则您的 Employee
插入将无法通过此测试.
You need to insert the Departments
entries first, or your Employee
insertions will fail this test.
这篇关于错误 1452 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!