2016-04-02 98 views
0

我對SqL的世界相當陌生,我有這個常見問題,大多數人似乎有修復,但我無法插入數據時,我得到錯誤1452,我有嘗試在這裏Mysql error 1452 - Cannot add or update a child row: a foreign key constraint failsMySQL錯誤1452插入新的數據

每個解決方案已經在這裏失敗是我的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到tbl_admin

ALTER TABLE blindx_test1.tbl_admin添加外鍵(僱員)參考tbl_employee(僱員);

#添加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

始終在'insert'語句中列出列。然後您可能會看到是什麼導致問題並能夠輕鬆解決問題。 –

回答

3

對於此查詢:

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

閱讀整個錯誤:

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))

這是很清楚的。您正試圖在employeeID中插入值7。沒有這樣的價值,所以你會得到錯誤。

Here是SQL撥弄代碼到最後的兩個插件。

+0

感謝您的反饋戈登,什麼我誤解是僱員擁有價值7等也各表這些聲明爲PK和FK鍵adminID。那麼如何在這些字段中插入一個值?有點失落,另外我沒有現場電話'empid'你是否指僱員ID? – Wazzie

+0

@WaheedRafiq。 。 。 「tbl_admin」或「tbl_employees」中沒有值「7」。參考表需要包含這些值。 –