开发者

MySQL "ON DELETE CASCADE" is too powerful

开发者 https://www.devze.com 2022-12-18 15:05 出处:网络
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 i

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号