tags:

views:

41

answers:

2

MySQL doesn't seem to have deffering constraints until the end of a transaction implemented yet. If so, what's the best way to switch parents in two child records?

For instance if we have some UI slots that each user can place items in. How should be switch items in user's slots, given that slotid is FK in item table and it there's unique index on itemId and slotId columns in an Items table?

Is there any better way to do it than by using SET FOREIGN_KEY_CHECKS=0 and three updates in a transaction?

[EDIT] SlotId isn't nullable and shouldn't be given the item always has to be in some slot - so setting it as nullable would mean giving up semantic correctness and important safety check just to make some implementation detail easier.

+1  A: 

Null them out, if the column to be updated is nullable, and then update, or delete and insert.

tpdi
A: 

I described the similar issue in this post.

Seems that making the column NULL'able is the lesser evil, since you don't have to disable the foreign key constraint in this case.

Quassnoi