简体   繁体   English

在实际的mysql系统中处理DELETE的最佳方法

[英]Best approach to deal with DELETE in real mysql system

I am working on a web project using Spring Boot + mySql 我正在使用Spring Boot + mySql进行Web项目

and I am thinking about how to delete data from the database. 我正在考虑如何从数据库中删除数据。

I know the DELETE * FROM table statement, but I have questions, 我知道DELETE * FROM表语句,但是我有疑问,

  1. Is there any way to keep the history or operation logs using DELETE? 有什么方法可以使用DELETE保留历史记录或操作日志?
  2. If I delete the data using DELETE, then I add a new one, the new data will insert into the place that was deleted, so the id is mis-ordered right? 如果使用DELETE删除数据,则添加一个新数据,新数据将插入到已删除的位置,因此ID的排列顺序不正确吗?

My friend suggest me to add a BOOL column to each table, which is delete flag, in order to control the delete process, is it a nice way to do so? 我的朋友建议我在每个表中添加一个BOOL列,即delete标志,以控制删除过程,这是一个不错的方法吗?

Sorry, I am newbie to server development, so I want to make sure each step is robust. 抱歉,我是服务器开发的新手,所以我想确保每个步骤都很可靠。 Thank you in advance. 先感谢您。

Is there any way to keep the history or operation logs using DELETE? 有什么方法可以使用DELETE保留历史记录或操作日志?

You can do it or by tracing these operation in a log file or by putting a trigger on the DB so that each delete will be traced (for example a new record in a new table will be created). 您可以执行此操作,也可以通过在日志文件中跟踪这些操作,也可以通过在数据库上放置触发器来跟踪每个删除操作(例如,将在新表中创建新记录)。 Personally I'd prefer to use the log file so yuo can configure your logging framework in several types (or by saving on DB, or in a log file or by using syslog etc...) 就个人而言,我更喜欢使用日志文件,以便yuo可以以多种类型配置日志记录框架(或通过保存在DB上,或在日志文件中或通过使用syslog等)。

If I delete the data using DELETE, then I add a new one, the new data will insert into the place that was deleted, so the id is mis-ordered right? 如果使用DELETE删除数据,则添加一个新数据,新数据将插入到已删除的位置,因此ID的排列顺序不正确吗?

No; 没有; the new record will have its own ID 新记录将具有其自己的ID

My friend suggest me to add a BOOL column to each table, which is delete flag, in order to control the delete process, is it a nice way to do so? 我的朋友建议我在每个表中添加一个BOOL列,即delete标志,以控制删除过程,这是一个不错的方法吗?

In this case you are doing not a real delete but a logical delete; 在这种情况下,您不是在执行真正的删除,而是在进行逻辑删除。 the record is not phisically deleted from the DB but it is marked as deleted. 该记录不是从数据库中删除的,而是标记为已删除。 This impacts on all other queries (for example you have to choice all records with delete flag false because the ones with delete flag true are "deleted") This can be useful to store all changes but you have also to think about tables dimension... records will always be added and never really deleted... 这会影响所有其他查询(例如,您必须选择所有带有delete标志为false的记录,因为带有delete标志为true的记录将被“删除”)。这对于存储所有更改很有用,但您还必须考虑表维度。 。记录将始终被添加,而从未真正删除...

Personally I'd move by using the log file and deleting the record 我个人将使用日志文件并删除记录

I hope this can help 我希望这可以帮助

Angelo 安杰洛

Q1. Q1。 Is there any way to keep the history or operation logs using DELETE? 有什么方法可以使用DELETE保留历史记录或操作日志? A1. A1。 You are working on the server side so while deleting you can generate logs in your code using any logger of your choices like Log4j or SLF4j. 您正在服务器端工作,因此删除时可以使用您选择的任何记录器(例如Log4j或SLF4j)在代码中生成日志。

Q2. Q2。 If I delete the data using DELETE, then I add a new one, the new data will insert into the place that was deleted, so the id is misordered right? 如果使用DELETE删除数据,则添加一个新数据,新数据将插入到已删除的位置,因此ID的排列顺序不正确吗? A2. A2。 No MySQL will not use the same key. 没有MySQL不会使用相同的密钥。

What your friend suggest is the soft delete, means the data still available in the system. 您的朋友建议的是软删除,这意味着系统中仍然有可用的数据。 But it makes your table bigger everytime. 但这会使您的桌子每次都变大。 Actually, you can use multiple approaches here its totally depend on your requirements like you need that data you want to delete or not. 实际上,您可以在此处使用多种方法,这完全取决于您的要求,例如您是否需要删除或不删除数据。

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

相关问题 处理列表中具有单独依赖项的子类的最佳方法 - Best approach to deal with subclasses in a List with separate dependencies 删除地图中一组条目的最佳方法 - Best approach to delete a set of entries in a Map iOS-JAVA实现实时数据同步的最佳方法 - iOS - JAVA Best approach to Achieve Real-Time Data sync 在 spring-jpa 中使用 JPQL 删除 2000 条记录的最佳方法 - Best approach to delete 2000 records using JPQL in spring-jpa 获取上下文以建立图标以进行滑动删除的最佳方法是什么 - What is the best approach for getting context to estabish icon for swipe to delete 建立具有大量数据通信的系统的最佳方法是什么? - What is the best approach to build a system with high amount of data communication? 什么是大规模系统的最佳方法-DB过程或编程逻辑 - What is the best approach for high scaled system - DB procedure or Programming logic 实时使用,传递和保存来自应用程序A的审核数据的最佳方法 - Best approach to consume, messaging, and save audit data from Application A in Application B real time 当您具有虚拟和实际实现的接口时,单元测试的最佳方法是什么? - What is the best approach for Unit testing when you have interfaces with both dummy & real implementations? 在Java中实现彩票系统以从mysql实时获取记录 - Implementing lottery system in java fetching records from mysql in real time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM