频道栏目
首页 > 资讯 > SQL Server > 正文

SQLon_delete的删除选项操作分析

18-06-19        来源:[db:作者]  
收藏   我要投稿

There are 6 possible actions to take when such event occurs:

CASCADE: When the referenced object is deleted, also delete the objects that have references to it (When you remove a blog post for instance, you might want to delete comments as well). SQL equivalent:CASCADE.

PROTECT: Forbid the deletion of the referenced object. To delete it you will have to delete all objects that reference it manually. SQL equivalent:RESTRICT.

SET_NULL: Set the reference to NULL (requires the field to be nullable). For instance, when you delete a User, you might want to keep the comments he posted on blog posts, but say it was posted by an anonymous (or deleted) user. SQL equivalent:SET NULL.

SET_DEFAULT: Set the default value. SQL equivalent:SET DEFAULT.

SET(...): Set a given value. This one is not part of the SQL standard and is entirely handled by Django.

DO_NOTHING: Probably a very bad idea since this would create integrity issues in your database (referencing an object that actually doesn't exist). SQL equivalent:NO ACTION.

Source:Django documentation

See alsothe documentation of PostGreSQLfor instance.

In most cases,CASCADEis the expected behaviour, but for every ForeignKey, you should always ask yourself what is the expected behaviour in this situation.PROTECTandSET_NULLare often useful. SettingCASCADEwhere it should not, can potentially delete all your database in cascade, by simply deleting a single user.

相关TAG标签
上一篇:MySQL 5.6 my.cnf的配置优化教程
下一篇:在JDBC中DAO模式的使用实例
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站