开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜