Error in copying records - mysql

I have a parent table, and child. The child is newly created table. I connected the child with the parent using foreign key.
The parent already contains data. The child is new empty one. I need to copy data from previous child table (that was previously connected to the parent but I removed the connection because of duplications in values in a column that is supposed to be unique (note: this column that I discovered that it should be unique is not the foreign key of course). However, I type the following statement in order to copy records from the previous child that was connected to the parent:
insert into databasename.newchild select distinct * from databasename.previouschild group by uniquename;
The difference between the new child & the old one, is that I specified a column as unique while it was not unique in the previous child which cause duplicates that shoud not be.
Mysql gives me error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (databsename.newchildtable, CONSTRAINT newchildforeignkeyname FOREIGN KEY (newchildforeignkeyname) REFERENCES parenttable (parentuniquecolumn) ON DELETE CASCADE ON UPDATE CASCADE)
What could be the problem ?

Related

PhpMyAdmin error #1452 - Cannot add or update a child row: a foreign key constraint fails

What I want to do is put a Foreign Key from users_table column id into users_order table in the column user_id but when I try to do it says this. what did I do wrong or is there any other way to add foreign key to table in PhpMyAdmin ?
#1452 - Cannot add or update a child row: a foreign key constraint fails (`users`.`#sql-4830_792`, CONSTRAINT `#sql-4830_792_ibfk_1` FOREIGN KEY (`id`) REFERENCES `user_order` (`user_id`))
Users
According to the docs,
For storage engines supporting foreign keys, MySQL rejects any INSERT
or UPDATE operation that attempts to create a foreign key value in a
child table if there is no a matching candidate key value in the
parent table.
The error you see indicates that you are trying to add a new row to a child table, for which mo matching row is present in your parent table. To fix it, you can either add the row in your parent table before inserting a row in your child table, or remove NOT NULL constraints (if any) and insert a NULL value in the corresponding column. Once you do it, you will be able to add the foreign key constraint.
Your error said that
the value that you are inserting into the foreign key does not exists in the parent table.
so before insertion foreign value into child table make sure your value is in parent table
The value should be the same in the primary and index key columns.

MySQL delete with inner join but foreign key constraint fails

I have a MySQL database. I would like to delete all matches where places.match_no > 26 and matches.chart_id = 106.
DELETE matches
FROM kk_matches AS matches
INNER JOIN places ON places.id = matches.place_id
AND places.match_no > 26
WHERE matches.chart_id = 106
This will cause an error:
#1451 - Cannot delete or update a parent row: a foreign key constraint fails...
What to do?
What to do?
Delete the child table dependent row first and then go for deleting parent table row. What you should have actually done is defining CASCADE option while defining your FOREIGN KEY saying ON DELETE CASCADE.
Another option is to have a BEFORE DELETE trigger and in there do DELETE the child table rows
There is another table in your database which has a foreign key to the kk_matches table. Before you can delete a record in the kk_matches table, you need to delete any records in other tables that point to that record in kk_matches. Otherise you would be "orphaning" those other records.
Foreign Keys exist as a means of avoiding these sort of operations that will cause your data to become irrelevant.
You can't delete a row that has a field referenced by a foreign key constraint. You must delete the referencing row first, then delete the row you want (or drop the constraint, if it wasn't intended).
Check this tutorial for more info:
https://www.w3schools.com/sql/sql_foreignkey.asp

Getting complicated about foreign key

I am a beginner in innoDB and not very good in database. I m getting confused about foreign keys. I just want to know is:
If I delete a PK( fk on another table)
is the FK on child table also deleted?
So how about if I delete FK on child table?
Record in parent table also deleted?
Also, if I ADD a new record to parent table
child table also added?
How about i add to child table?
How about if i update data?
Please help me, I am very confused about foreign key reference in innoDB database . Guide me to the easiest way. Thanks.
Assuming that the foreign key constraints are defined, supported by the storage engine and are enabled, (i.e. MySQL variable foreign_key_checks = 1, Oracle foreign key constraints enabled and not deferred, etc.)
Q: If I delete a PK( fk on another table) , is the FK on child table also deleted ?
A: If the DELETE rule is specified as CASCADE, then a delete on the parent table will also perform delete operation on the child table. If the DELETE rule is specified as RESTRICT, and there's rows in the child table that reference the parent key, then the DELETE operation will raise an error.
(EDIT: For the sake of completeness, we note that "SET NULL" is another option for an UPDATE or DELETE rule.)
Q: If I delete a row from the child table with a reference to a row in the parent table, is the row in the parent table also deleted ?
A: No. Deleting rows in the child table will have no affect on row in the parent table.
Q: If I INSERT a new row to parent table, will a row also be added to the child table>
A: No. Rows are not automatically inserted to child tables. It's valid (relational database-wise) to have a parent row that does not have any child rows referencing it.
Q: If I INSERT a row to the child table ?
A: No. An insert to the child table does not automatically insert a row to the parent table. (Any non-null value of the foreign key column being inserted to the child table will be validated; the database will verify that there already exists a row in the parent table with a matching value. If there's not, then the INSERT will fail with an error.)
Q: What if I UPDATE values of the foreign key column in the child table.
A: Same as with an insert, the new value will be validated before the update operation proceeds.
Q: What if I UPDATE the value of the primary key in the parent table?
A: If the UPDATE rule on the foreign key is specified as CASCADE, then the related rows in the child table will also be updated, to preserve the relationship between the rows. If the UPDATE rule is RESTRICT, and there are related rows in the child table, the update operation will raise an error.
Foreign keys are designed for enforcing referential integrity. They basically disallow inserts/updates in the child table when the new non-null values of the foreign key column doesn't reference a row in the parent.
The UPDATE and CASCADE rules defined on the foreign key constraint determine the behavior of UPDATE and DELETE operations on the parent table.
What your'e asking about is cascading the update/deletion to the child element.
The docs : http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
Example :
CONSTRAINT FOREIGN KEY (jobAbbr) REFERENCESffxi_jobType(jobAbbr) ON DELETE CASCADE ON UPDATE CASCADE
the "ON DELETE CASCADE" means that when the key is deleted, all the FK's are deleted aswell, this is very useful for data integrity. It's however not automatic.
Deleting a foreign key will not delete the key it's based upon either way.

1452 - Cannot add or update a child row: a foreign key constraint fails

I'm getting this error:
1452 - Cannot add or update a child row: a foreign key constraint
fails.
I've located them and try to get rid of the references with
alter table tillhör drop foreign key kat_id;
But getting this error instead:
#1025 - Error on rename of '.\recept\tillh#1ir' to
'.\recept#sql2-1570-3cb' (errno: 152).
What do I do wrong?
Before you query run
SET FOREIGN_KEY_CHECKS=0
then set it to 1 after you`re done.
I face same problem. I solve this issue by clearing, i.e. deleting all data from child table and successfully done.
This is occur if child table contain some data with the foreign key that that are not in parent table i.e, if there are two table called Person (with column id, name, address) and order(with column id, person_id, order_name); order.person_id is foreign key of person.id and order table contain person_id that is not present in person table.
You can solve this using the following query
Delete from order where person_id NOT IN (select id from person where person.id = order.person_id)
When I had this problem, it was due to the fact that I forgot to specify NULLS allowed when creating the foreign key id fields. I changed it after the fact, but 0's were already in the value. It could not find 0 in the matching table, and then gave this error. The fix: update the zero values to nulls.
You have to set nulls to your from the parent to child tables
Or set the same values eg
Child table first nam=Derick
Parent table name=Derick

Parent table cannot be updated when foreign key is present

There are two tables one is a parent i.e., groups table which has foreign key to a child table i.e., users. I am not able to edit foreign key column in parent table where as I have given it to cascade to child table. It gives a error as follows:
Error Code : 1452
Cannot add or update a child row: a foreign key constraint fails (`tms`.`groups`, CONSTRAINT `FK_groups` FOREIGN KEY (`GroupName`) REFERENCES `users` (`groupname`) ON DELETE CASCADE ON UPDATE CASCADE)
Thanks,
-Jeevan
I assume a group contains many users, and a users belongs to one group.
Then you have declared the foreign key in the wrong direction. Actually users.groupname must reference tms.groups. Drop the current foreign key and rebuild it the other way around (in the users table).
This happens if you try to reference a non existing entry in database. In short, you inserted into groups and tried to reference an user entry which doesn't exist yet.