简体   繁体   English

MySQL错误1452插入新数据

[英]MySQL Error 1452 Inserting new Data

I am fairly new to the world of SqL , I am having this common problem that most people seem to have fix but I am not able to , when inserting data i am getting Error 1452 , i have try the following tread on here Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails 我对SqL的世界还很陌生,我遇到了大多数人似乎都已解决的常见问题,但我却无法解决,在插入数据时出现错误1452,我在这里尝试了以下步骤Mysql错误1452 -无法添加或更新子行:外键约束失败

each solution has failed here is my SqL script: 每个解决方案都失败了,这是我的SqL脚本:

# Author: Waheed Rafiq
# Date: 02/04/2016
# testX database
# list to Create tables 

# tbl_Admin 
create table tbl_admin(
adminID smallint not null  primary key auto_increment,
employeeID smallint null ,
admin_employee_Fname varchar(20),
admin_employee_Sname varchar(20),

admin_accessRight enum ('standard','accounts','classic','admin')
); 
# add dummy data :
insert tbl_admin values(1,1,'Waheed','Rafiq','admin');
insert tbl_admin values(2,2,'Sarah','Rafiq','classic');
insert tbl_admin values(3,3,'Philps','Smiths','standard');
insert tbl_admin values(4,4,'John','Edwards','standard');
insert tbl_admin values(5,5,'Ash','Codi','accounts');
insert tbl_admin values(6,6,'Kath','Womi','admin');

# tbl_employee 
create table tbl_employee(
employeeID smallint not null  primary key auto_increment,
adminID smallint null,
employee_Fname varchar(20),
employee_Sname varchar(20),
employee_AddressL1 varchar(100),
employee_AddressL2 varchar(100),
employee_PostCode varchar(10),
employee_tel_type enum ('work','personal','other'),
employee_telephone varchar(30) ,
employee_JobRole varchar(20),
employee_Salary float

); 

# add dummpy data to employee table

 insert tbl_employee values(1,1,'Waheed','Rafiq','36 do thed        road','Alcala','2345','work','07525177630','Admin','60.568');
insert tbl_employee values(2,2,'Sarah','Rafiq','126 Cala de  Polo','Alcala','4345','personal','034525177630','Office Admin','1.568');
insert tbl_employee values(3,3,'Philps','smiths','367 fake de pounda','Alcala','345','work','09258177630','Maintance','15.568');
insert tbl_employee values(4,4,'John','Edwards','36 Greader los OSO','Madrid','1045','work','07525177888','office clark','2.568');
insert tbl_employee values(5,5,'Ash','Codi','88 do thed road','Alcala','2345','work','07528547858','Finance','30.568');
insert tbl_employee values(6,6,'Kath','Womi','555 Gotham road','Madrid','1111','work','07525177999','Admin Techi','40.568');

# check up to this point all works fine dummpy data is added.

now I add the FK keys : in a separate scripts 现在,我在单独的脚本中添加FK键:

# Author: Waheed Rafiq
# Date: 02/04/2016
# testx database
# Add list of FK to database.

# FK to tbl_admin #FK到tbl_admin

alter table blindx_test1.tbl_admin add foreign key(employeeID) References tbl_employee(employeeID); 更改表blindx_test1.tbl_admin添加外键(employeeID)引用tbl_employee(employeeID);

# add FK to tbl_admin #将FK添加到tbl_admin

alter table blindx_test1.tbl_employee add foreign key(adminID) references  tbl_admin(adminID); 

this part works fine too and now here is the problem when inserting new data into the tables i am getting Error code 1452: 这部分也正常工作,现在这是将新数据插入表时出现的问题,我收到错误代码1452:

insert tbl_admin values( 7,7,'Samia','Fazal','admin');

insert tbl_employee values(7,7 ,'Samia','Fazal','786 Bromford    Lane','Madrid','1110','other','07525177999','Admin Techi','40.568');

any help or support , would be very much appreciated, something so simple is now turning out to be hard :( am sure its something obvious that you guys would be able to spot. 任何帮助或支持,我们将不胜感激,但现在如此简单的事情变得非常困难:(可以肯定的是,你们将能够发现它。

thanks for your support. 谢谢你的支持。 will keep on searching for a fix if I do find one shall update my post. 如果我确实找到一个可以更新我的帖子的信息,它将继续寻找修复程序。

For this query: 对于此查询:

insert tbl_admin values( 7,7,'Samia','Fazal','admin');

Read the entire error: 阅读整个错误:

Cannot add or update a child row: a foreign key constraint fails ( db_9_3137a . tbl_admin , CONSTRAINT tbl_admin_ibfk_1 FOREIGN KEY ( employeeID ) REFERENCES tbl_employee ( employeeID )) 不能添加或更新子行,外键约束失败( db_9_3137atbl_admin ,约束tbl_admin_ibfk_1外键( employeeID )参考tbl_employeeemployeeID ))

This is pretty clear. 这很清楚。 You are trying to insert a value of 7 in employeeID . 您试图在employeeID插入值7。 There is no such value, so you get the error. 没有这样的值,所以您会得到错误。

Here is a SQL Fiddle with the code up to the last two inserts. 是一个SQL Fiddle,其中的代码直到最后两个插入为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM