简体   繁体   English

将记录标记为删除而不是从mysql表中删除记录

[英]flag record as delete instead of deleting the record from mysql table

i want to know how to proceed to flag records as deleted instead of physically deleting the record from the database. 我想知道如何继续将记录标记为已删除而不是从数据库中物理删除记录。 I never came across such a thing before. 我之前从未遇到过这样的事情。 Is there any tutorial i can follow? 我可以遵循任何教程吗?

Easiest way is to add a new BOOL column to the table like enabled with a default value of 1 . 最简单的方法是向表中添加一个新的BOOL列,如enabled默认值1

Then UPDATE table_name SET enabled = 0 when you would like to "delete" the row. 然后,当您要“删除”该行时, UPDATE table_name SET enabled = 0

And filter out "deleted" rows in your queries like: 并过滤掉查询中的“已删除”行,例如:

SELECT * FROM table_name WHERE ... AND enabled = 1

Use an extra boolean field in your table to denote if the entry is valid or not. 在表中使用额外的布尔字段来表示条目是否有效。

TableName(<Your Fields>,Valid)

Valid =1 , then it is present. 有效值= 1,然后它存在。

Valid =0, then it is deleted. 有效值= 0,然后删除。

There's nothing intrinsic in MySQL to flag a record as deleted - but you can add your your own column to describe the state - you just need need to remember to exclude it from your queries. MySQL中没有任何内在功能可以将记录标记为已删除 - 但您可以添加自己的列来描述状态 - 您只需要记住将其从查询中排除。 The complication arises when you try to insert a row with unique/primary keys matching your deleted record; 当您尝试插入与您的已删除记录匹配的唯一/主键的行时,会出现此复杂情况; you'll need to add a trigger to amend the state. 你需要添加一个触发器来修改状态。 Really a simpler option is to just delete the data in the first place (you don't say what you're trying to achieve by not deleting the row) 真的更简单的选择就是首先删除数据(你没有说明你想要通过不删除行来实现的目标)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM