MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2009, 04:56 PM
minestrone minestrone is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 14 minestrone User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 54 sec
Reputation Power: 0
Importing data into tables with referential integrity (causing errors)

Hi,

I wonder if anyone can point me in the direction of a solution for this, if there is one.

I have an import script for a number of tables in my database, the problem is that when I try to run them I get contraint errors. like so

Code:
Script line: 6	Cannot add or update a child row: a foreign key constraint fails (`football/jforum_groups`, CONSTRAINT `FK80BB241C6B459AD9` FOREIGN KEY (`parent_id`) REFERENCES `jforum_groups` (`group_id`))
Script line: 12	Cannot add or update a child row: a foreign key constraint fails (`football/jforum_roles`, CONSTRAINT `FK1D86DB75E0D60E4` FOREIGN KEY (`group_id`) REFERENCES `jforum_groups` (`group_id`))
Script line: 30	Duplicate entry '1' for key 1
Script line: 35	Cannot add or update a child row: a foreign key constraint fails (`football/jforum_user_groups`, CONSTRAINT `FKDF950380F824D10` FOREIGN KEY (`user_id`) REFERENCES `jforum_users` (`user_id`))


I thought that if i switched autocommit off, started a new transaction and then called commit at the end then this would solve the problem bus sadly not so.

Is there another way in which I can get the data into the tables before the errors are thrown?

Thanks for any help in advance.

Reply With Quote
  #2  
Old July 2nd, 2009, 09:00 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 20,691 r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level)r937 User rank is General 22nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 Week 3 Days 9 h 50 m 16 sec
Reputation Power: 2459
Quote:
Originally Posted by minestrone
I have an import script for a number of tables
i'll bet that the tables were scripted in alphabetic order

you have to define them in a particular order, and also load them in that particular order

that order is simply this: for every PK/FK relationship, the table with the FK must come after the table with the PK

alternatively, if you can re-script them, look for an option which generates the FK relationships as ALTER TABLE statements, and save them for the after all the tables have been created and loaded
Comments on this post
SKDevelopment agrees!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 3rd, 2009, 02:01 AM
SKDevelopment SKDevelopment is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 771 SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level)SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level)SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level)SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level)SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level)SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level)SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level)SKDevelopment User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 11 h 7 m 58 sec
Reputation Power: 186
I am not sure if it helps, but if you run the command
Code:
set foreign_key_checks=0;

before the import, the foreign key constraints will be simply ignored during the import.

Of course if you use it, you must be sure that the foreign key constraints would not be violated after the import is finished.

Personally I use it e.g. when I need to move the whole database to another server when the FILE privilege is absent (so I can not use LOAD DATA INFILE and need to fill in the database with a script).

Edit: Importing data in some particular order as r937 said is a more correct approach I think. I use
set foreign_key_checks=0;
only when I am absolutely sure the foreign key constraints could not get violated after the import is finished.
__________________
PHP Programmer

Last edited by SKDevelopment : July 3rd, 2009 at 02:10 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Importing data into tables with referential integrity (causing errors)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
Stay green...Green IT