问题描述
相对于实际或物理删除记录,对记录进行逻辑/软删除(即设置一个标志,指出记录已删除)有什么好处?
What is the advantage of doing a logical/soft delete of a record (i.e. setting a flag stating that the record is deleted) as opposed to actually or physically deleting the record?
这是惯例吗?
这安全吗?
推荐答案
优点是您保留了历史记录(便于审核),而不必担心通过引用数据库的各种其他表来级联删除操作您要删除的行.缺点是您必须对任何报告/显示方法进行编码才能考虑该标记.
Advantages are that you keep the history (good for auditing) and you don't have to worry about cascading a delete through various other tables in the database that reference the row you are deleting. Disadvantage is that you have to code any reporting/display methods to take the flag into account.
就通常的做法而言-我会说是的,但是对于其他任何用途,都取决于您的业务需求.
As far as if it is a common practice - I would say yes, but as with anything whether you use it depends on your business needs.
想到另一个缺点-如果表上有唯一索引,则删除的记录仍将占据一个"记录,因此您也必须围绕这种可能性进行编码(例如,一个用户表具有一个用户名的唯一索引;已删除的记录仍会阻止已删除的用户的新记录的用户名;要解决此问题,您可以将GUID附加到已删除的用户名列,但这是我不推荐的非常棘手的解决方法.在这种情况下,最好有一条规则,即一旦使用了用户名,就永远不能替换它.)
Thought of another disadvantange - If you have unique indexes on the table, deleted records will still take up the "one" record, so you have to code around that possibility too (for example, a User table that has a unique index on username; A deleted record would still block the deleted users username for new records. Working around this you could tack on a GUID to the deleted username column, but it's a very hacky workaround that I wouldn't recommend. Probably in that circumstance it would be better to just have a rule that once a username is used, it can never be replaced.)
这篇关于物理记录还是逻辑记录(硬对软)删除数据库记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!