MySQL "ON DELETE CASCADE" is too powerful
hey, following problem here: i have table with persons that have one location, if i delete the location for the location table, i don't wont to lose the assi开发者_开发百科gned persons. ive a entry in the location with the id=1 and the name is: "No location". and that what I'm trying to do, but i fail!
ALTER TABLE `persons`
ADD CONSTRAINT `persons_ibfk_1` FOREIGN KEY (`location_id`)
REFERENCES `locations` (`id`) ON DELETE SET `location_id` = 1;
thanks!
Use ON DELETE SET DEFAULT
(and make 1 the default value), or ON DELETE SET NULL
.
A third option is to manually set all ids to 1 before deleting.
精彩评论