Solved

Foreign Key Constraints fail dumping and restoring MySQL db

Posted on 2013-01-25
6
1,225 Views
Last Modified: 2013-03-11
Hi all,

When I dump a MySQL db data to a .sql file from phpmyadmin and try to restore to the loca version of the db on my machine i get the following error;

Error Code 1452. Cannot add or update child row. a foreign key constraint fails.


Now I assume this is because it is trying to insert the data out of sequence (for example trying to insert a relationship table such as userole before user table is populated??)

Is there a way of clmbatting this when we dump the data?
0
Comment
Question by:flynny
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38819411
Either you create the constraints after the data loading, or you make indeed shure you load in the correct order. You might find that you can disable the constraints during the loading.
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 38821750
Open the dump file with a text editor and put this somewhere at the top:

SET FOREIGN_KEY_CHECKS=0;

Remember to do a SET FOREIGN_KEY_CHECKS=1; after.
0
 

Author Comment

by:flynny
ID: 38821772
ok temporarily releasing the constraints sounds the most straight forward idea.

How is it possible to do this using mysql workbench please?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 24

Expert Comment

by:johanntagle
ID: 38821839
You can type in that line and run it like any SQL statement.

But I suggest you make it part of the dump file, which you can open as an SQL script in Workbench or using by any text editor.
0
 

Author Comment

by:flynny
ID: 38885476
ok could you provide the code to remove and then readd these constraints?

i.e. I assume I would use the code to remove constrainsts

Insert the data,

dump the constraints from the original db and re-add these back tot the copy db?
0
 
LVL 24

Expert Comment

by:johanntagle
ID: 38886604
Why remove and readd when you can just disable and re-enable them with the commands I gave above?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question