且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在DB表中标记已删除的记录

更新时间:2023-12-04 22:16:28

这是关于它 - 一个布尔字段来表示记录被删除。我使用过几次,我打电话给这个字段 IsDeleted



这通常被称为逻辑删除



您需要尊重报告中的该字段 - 这意味着不包含所有使用的记录IsDeleted = true 。如果您有很多表和关系,那么这些查询可能会变得复杂一些。



另外,如果表上有唯一约束,可能会遇到一些问题。例如,如果在用户表中用户具有 IsDeleted = true ,并且电子邮件列是唯一的,则我无法添加具有相同电子邮件地址的新用户。 / p>

有些ORM将考虑这些字段 - 例如,如果有一个名为Deleted或IsDeleted的列,SubSonic 2.2将不会删除记录



一些相关资源:





除此之外,您还可以添加审核表。


Sometimes you want to mark a DB table record as deleted instead of deleting it permanently, right?

How do you do that?

So far I've been using a boolean "deleted" field but I'm not sure if it's a good apprach.

That's about it - a boolean field to indicate that the record is deleted. The few times I used that, I called that field IsDeleted.

This is often referred to as Logical Delete.

It's up to you to respect that field in your reports - which means excluding all the records with IsDeleted = true. Those queries can get a little complicated if you have a lot of tables and relations.

Also, you may encounter some problems if you have unique constraints on a table. For example, if in a user table a user has IsDeleted = true and the email column is unique, i would not be possible to add a new user with same email address.

There are some ORM which take those fields into consideration - for example, SubSonic 2.2 will not delete a record if there is a column named 'Deleted' or 'IsDeleted', instead it will set this field to true.

Some related resources:

As an alternative to this you could add auditing tables.