1452 Cannot add or update a child row a foreign key constraint fails mysql phpmyadmin 2022

1452 Cannot add or update a child row a foreign key constraint fails mysql phpmyadmin 2022


1452 Cannot add or update a child row a foreign key constraint fails mysql phpmyadmin 2022

1452 Cannot add or update a child row a foreign key constraint fails mysql phpmyadmin 2022

The MySQL ERROR 1452 happens when you try to execute a data manipulation query into a table that has one or more failing foreign key constraints. The cause of this error is the values you’re trying to put into the table are not available in the referencing (parent) table.

How do I fix foreign key constraint failure?
The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.

Why Cannot add or update a child row?
The error comes when you are trying to add a row for which no matching row in in the other table. “Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table.


Content

10.8 -> hello everybody welcome to our new video uh today  we want to fix this error which is a error number  
18.08 -> 1452 is my mysql error so what i did i tried  to put i was trying to put a foreign key  
30.08 -> on this table so i've created a subject id  which is 11 inch but now you can see here  
36.88 -> when i go and i say i want to to  put a foreign key what it does uh
46 -> let me say subject id this is  something crazy sub object id  
56.96 -> which is that get it there so we go here  and we take it and we say subject id
67.84 -> um what is that okay let me see yes then  we go here and we try to get the database  
78.08 -> this database we're using then we come  and we look for the table we want to  
85.44 -> to get its primary key to be our foreign key in  this table so we're trying to get that tbr subject  
95.68 -> this one we got it so it gets it  automatically if i say on cascade  
102.88 -> i want on delete if that it deleted that site  it can be deleted this side so if i want to  
110.72 -> also edit that site to be edited that's this side  so when we try to save this we get into problems  
120.8 -> if i say okay you can see i can see we get  into problems let's go and analyze our data
130 -> that we're going to see that it's a  0 0 0 0 here and in the table here  
140.48 -> we want to make a foreign key let me  get you to that table appearance um
150.4 -> please come no the table is called subject  table um can't see it here but i'm gonna get it
162.56 -> subject subject i can't seem  to see it but i'm gonna see it
172.88 -> subject subject student subject subject  subject subjects tbo subject student well  
182.24 -> i don't know if my eyes are not working well  but okay that shouldn't be a problem to us  
189.36 -> what we're gonna do we're going to try to go back  yeah and we'll try to say subject we say to say go  
208.08 -> if there is no table like that is fine  um we're going to get tbl subject subject  
222.16 -> uh we see if we can get that yes we got that  so after getting that subject you can see  
229.44 -> in the subject id there is no id called  zero that could cause a little problem  
236.96 -> also but in the foreign key there we can see  there's a subject called zero so now here  
243.36 -> uh what i could do let me try to go also see the  structure of this table and you can see the phones  
249.92 -> be nine nine or two okay everything's fine here  but we might be facing a problem that they were  
255.44 -> trying to create a foreign key but there is no id  like this id so that id0 is not here that's why  
263.2 -> uh we might be have facing that problem so i'm  gonna look for that table again then i try to  
269.12 -> do something simple and see if we can can be fixed  like that uh tbl lessons we go here now we see all  
277.68 -> these guys we have we're gonna put one we're gonna  give it one and uh we give it a one also here  
285.92 -> give it a one wow what's going on here it's  giving me trouble here okay we're gonna give  
293.12 -> it one one one one uh lessons okay we edit  this also we put a one we edit the second
306.88 -> another one we call it a one and we  edit another one we give it a one
314.72 -> and we edit another one we give it a one and uh we  save okay no we we we have one also we have to do  
328 -> okay one and we saved so now let's try to  see if we can give a foreign key to this
365.6 -> yes we got it okay let's save and see
371.2 -> yes it worked so the issue here is one you cannot  create a primary foreign key in a child table  
379.2 -> when there is no data which is going to  be similar to the data in the first table  
386.64 -> there simply means whatever
391.04 -> happens here whatever i do we have in the child  table must happen at what in the secondary table  
396.48 -> which means the mother table so that's the reason  why we could not find but if you want to do  
402.24 -> that it simply means this table should be new or  you should delete everything here all the data  
408 -> here if we say let me browse you should delete  everything here that's we can be able to create  
414.72 -> so that's why we had a zero it could not work  only we would it would have worked if the zeros  
420.4 -> we could go and put a zero into that and also  you saw the cascading stuff whereby if we go to  
431.36 -> our our table and change if we change  the id this id that side it can change  
437.36 -> also that if we change it that side it could  not be changing also this side automatically  
441.6 -> that's why i can see we put it on cascade  or delete so whenever we delete in there  
446.88 -> in the parent table it's going  to disappear also from here
452.8 -> yeah guys that's the way we fix  this uh thanks for watching guys  
457.2 -> uh see i hope i see you in the next video  please don't forget to subscribe don't forget to  
463.36 -> to also give this uh uh send uh give this  to friends and share it today with them  
470.32 -> post it on facebook on twitter and  everywhere make us popular guys  
474.24 -> thanks for watching if we did help you on that  thanks for watching see you in the next video

Source: https://www.youtube.com/watch?v=edAfbueS77Q