我对SqL的世界相当新,我有这个常见的问题,大多数人似乎已修复,但我无法,当插入数据我得到错误1452,我在这里尝试下面的步骤PopOver displayed correctly
每个解决方案都失败了,这是我的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.
现在我添加FK键:在单独的脚本中
# Author: Waheed Rafiq
# Date: 02/04/2016
# testx database
# Add list of FK to database.
#FK to tbl_admin
alter table blindx_test1.tbl_admin add foreign key(employeeID)引用tbl_employee(employeeID);
#将FK添加到tbl_admin
alter table blindx_test1.tbl_employee add foreign key(adminID) references tbl_admin(adminID);
这部分工作正常,现在这里是将新数据插入表中的问题我得到错误代码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');
任何帮助或支持,都会非常感激,这么简单的事情现在变得很难:(我相信你们能够发现它显而易见的东西。
感谢您的支持。如果我发现有人会更新我的帖子,我会继续寻找解决方案。
答案 0 :(得分:3)
对于此查询:
insert tbl_admin values( 7,7,'Samia','Fazal','admin');
阅读整个错误:
无法添加或更新子行:外键约束失败 (
db_9_3137a
。tbl_admin
,CONSTRAINTtbl_admin_ibfk_1
FOREIGN KEY (employeeID
)参考tbl_employee
(employeeID
))
这很清楚。您正尝试在employeeID
中插入值7。没有这样的值,所以你得到错误。
Here是一个SQL小提琴,代码最后两个插入。