sql删除相关记录?
记录删除的最佳实践是什么? 哪个选项最正确?
例如
有以下情况:
表
----用户----
int usr_id
varchar 名称
varchar 姓氏
布尔deleted_yn
<小时>---主题--
int subject_id
varchar 标题
布尔deleted_yn
<小时>---subject_to_user---
int subject_to_user_id
int usr_id
int subject_id
布尔deleted_yn
<小时>
如果我想删除用户或主题我需要删除关系 - subject_to_user 吗?
为什么我这么问,因为在工作中有时用户会删除一些信息,然后我们必须恢复该数据。 目前我们删除了相关数据,但是恢复起来非常困难;
所以我问删除的最佳实践是什么? 哪个选项最正确?
我认为删除相关数据是正确的,但要看具体情况。
通过删除那里我的意思是标记deleted_yn true/false
What is best practice on record deletion?
And which option is most correct?
For example
there is following situation:
tables
----users----
int usr_id
varchar name
varchar surname
bool deleted_yn
---subjects--
int subject_id
varchar title
bool deleted_yn
---subject_to_user---
int subject_to_user_id
int usr_id
int subject_id
bool deleted_yn
And if I want to delete user or subject do I need delete relation - subject_to_user?
Why I'm asking, because at work sometimes user deletes some information and then we must recover that data.
At the moment we delete related data, but recovery is very difficult;
So I'm asking what is best practice on deletion?
And which option is most correct?
I think that deletion related data is correct, but it depends on situation.
By deleting there I mean marking deleted_yn true/false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最佳做法是不要删除任何内容。只需将项目标记为已删除
添加列“IsDeleted”,并在大多数查询中添加“WHERE IsDeleted=False”
Best practice is not to delete anything. Just flag an item as deleted
Add column 'IsDeleted' and in most of the queries you add the 'WHERE IsDeleted=False'
subject_to_user中的相关数据是如何关联的?带着身份证。
如果您从 user 和 subject 中删除,但没有删除 subject_to_user,那么什么告诉您不会使用 subject_to_user 中已有的 ID 创建新用户?
这个想法是删除所有相关数据。如果需要恢复,您始终可以拥有另一组表,称为deleted_user、deleted_subject 和deleted_subject_to_user。当您从主表中删除时,请移至之前的表。
The related data in subject_to_user is related how? With ID's.
If you delete from user and subject but not subject_to_user, what tells you that you won't create a new user with an ID that is already in subject_to_user?
The idea is to remove all related data. If you need recovery, you can always have another set of tables called deleted_user, deleted_subject, and deleted_subject_to_user. When you delete from your main tables, move to those just before.
Udi Dahan(我毫不掩饰地受到影响)就这个主题写了一篇很棒的博客文章。您可以在此处阅读相关内容,但摘要是反映现实生活中发生的事情。
例如,在现实世界中,订单被“取消”、“退回”,但从未真正删除 - 上下文非常重要。
Udi Dahan (am man whom I am unashamedly influenced by) wrote an excellent blog post on this topic. You can read about it here but the summary is to mirror what happens in real life.
For example in the real world an order is "cancelled", "returned" but never really deleted - with context being extremely important.
在数据库中将数据标记为已删除固然很好,但在某些情况下,您可能需要真正从数据库中删除数据以符合数据法规。
使用postgresql时,可以通过“USING”关键字来删除相关数据,将删除查询结合到相关表中。例如,
将首先删除与某些文档相关的
observed_topics
,然后在第二条语句中删除实际文档。Marking data as removed in the database is great, but in some cases you might need to really delete data from your database to conform with data regulations.
When using postgresql you can delete related data with the "USING" keyword to combine the delete query to the related table. For example
will first delete
observed_topics
related to some documents and in the second statement we delete the actual documents.