If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Error Handling in Foreign Keys

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-30-04, 12:13
rolandpish rolandpish is offline
Registered User
 
Join Date: Jun 2004
Posts: 21
Error Handling in Foreign Keys

Hi, I have a question related to error handling.

Look at the following:

Error Code : 1216
Cannot add or update a child row: a foreign key
constraint fails

Error Code : 1217
Cannot delete or update a parent row: a foreign key
constraint fails

I have a parent table which has more than one child
table. When I catch the error in the programming
language, I would like to know
which table is giving the error because I want to
display the Message Box with the corresponding
message to the user, something like: "Cannot delete
customer because invoices exists."

Is there something in MySQL that can be able to tell
me this?

If thereīs nothing to correct this, do you have an
idea to resolve this with something else?

Thanks for the time!
Roland
Reply With Quote
  #2 (permalink)  
Old 07-30-04, 14:24
yellowmarker yellowmarker is offline
Registered User
 
Join Date: Jul 2004
Location: Dundee, Scotland
Posts: 107
since the mysql error message is not specific enough, you should run validation queries first to check that the intended operation is valid before doing an update/delete. that way you'll be able to present accurate messages to the user.
Reply With Quote
  #3 (permalink)  
Old 07-30-04, 17:03
rolandpish rolandpish is offline
Registered User
 
Join Date: Jun 2004
Posts: 21
Ok, that sounds good. Iīll do it.

By the way, I donīt know if those validations
would bring a little overhead before doing the insert/update/delete.
What do you think about this If there are lots
of users connected to the database?

Thanks for the time
Roland
Reply With Quote
  #4 (permalink)  
Old 07-31-04, 14:30
yellowmarker yellowmarker is offline
Registered User
 
Join Date: Jul 2004
Location: Dundee, Scotland
Posts: 107
a select (read only) query should be no trouble. if the tables contain more that 200 records, starting thinking about using indexes if you have performance issues.

the thing is they are necessary from the application proint of view and should not be complex. to be safe, you could add a high level switch, using define(...), so that you can turn the extra validation off if the site has performance problems after introducing the change.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On