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 > Constraint Problems

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-04-08, 11:44
mvreade mvreade is offline
Registered User
 
Join Date: Sep 2006
Posts: 25
Constraint Problems

I have a table RESERVATIONS, which has these Constraints:

CONSTRAINT `reservations_customerID` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`),

CONSTRAINT `reservations_eventID` FOREIGN KEY (`event_id`) REFERENCES `events` (`event_id`),

CONSTRAINT `reservations_hotelID` FOREIGN KEY (`hotel_id`) REFERENCES `hotels` (`hotel_id`)

For some reason, this INSERT SUCCEEDS:
INSERT INTO reservations (hotel_id, customer_id) VALUES (111,222)

While this INSERT FAILS:
INSERT INTO reservations (hotel_id, event_id, customer_id) VALUES (111,222,333)

With the following error message:
"Cannot add or update a child row: a foreign key constraint fails ('turon/reservations', CONSTRAINT 'reservations_eventID' FOREIGN KEY ('event_id') REFERENCES 'events' ('event_id'))

As far as I can tell all constraints are similar.

Any suggestions?

Thanks,
Michael
Reply With Quote
  #2 (permalink)  
Old 05-04-08, 14:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
the error message is quite clearly telling you that you do not have a 222 row in the events table

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-04-08, 14:43
mvreade mvreade is offline
Registered User
 
Join Date: Sep 2006
Posts: 25
Oh! So a Foreign Key Constraint also prevents you from entering a non-existent FK value?

Shoot! I thought FK Constraints we only to prevent deleting a related record...

Is that correct?

Thanks, Michael
Reply With Quote
  #4 (permalink)  
Old 05-04-08, 15:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by mvreade
Oh! So a Foreign Key Constraint also prevents you from entering a non-existent FK value?
that is correct
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 05-04-08, 21:13
mvreade mvreade is offline
Registered User
 
Join Date: Sep 2006
Posts: 25
Living and learning. Thanks for the help, r937.

All the best,

Michael
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