当前位置:网站首页>Solution to the conflict between unique index and logical deletion
Solution to the conflict between unique index and logical deletion
2022-06-30 22:46:00 【YXXYX】
introduction
I encountered a problem in my project today : When logical deletion and unique index exist at the same time , The data that has been logically deleted cannot be added again due to the constraint of the unique index , What should I do ?
This problem has been encountered in other projects before , At that time, I didn't think of any good solution , I didn't check the data , I think of a violent method :
I directly removed the unique index , Then in the code logic implementation part, the uniqueness of the index field is constrained by code ;
This, of course, solves the problem , But it is wrong to do so , How can a designed database be modified at will ;
This time I encountered the same problem , Thinking of the last method is not only non-conforming , And it's very troublesome ( For a unique logical judgment, call the database query and then write the logical judgment ), This time I found a solution , Here is a brief introduction ;
Solutions
All the solutions are just one idea : Through logical deletion, fields and constraint fields are combined into an index ;
For example, there is a teacher surface , The unique index is name, The logical deletion field is is_delete:

Uniqueness index :

In order to solve the problem of conflict , First, you need to modify the index , Let the logically deleted field and the unique field act as the index together :

But one more thing to note : Generally, we are used to deleting fields logically is_delete Set to 0 There is 1 Delete ; But this is not the case here ;
For example, delete the above table test Field , The index can be seen as test and 1 The combination of ; Create again test No problem , Because the newly created index is test and 0 The combination of , But if you delete it again test Then again test and 1 The combination of , Index conflict ;
So the only thing we have to do is find a way to make The deletion ID of the logical deletion field is unique , Undeleted fields do not need to be changed ;
Here are three ways :
Set the deletion ID as the primary key id
Because the primary key id Is the only one. , Therefore, you only need to set the deletion ID of the logical deletion field as the primary key :

But this method I think if id Too long , Will it take up more space ?( Personal thoughts )
Like this teacher In the table, I only is_delete Field set to tinyint The length is 3, If the primary key is too long, it must be modified to bigint, Please don't say , Does it take up more space ;

The deletion ID is set to the current time
This method is similar to setting the deletion ID to a timestamp , Because every period of time is different , So naturally there will be no repetition :
Then you can use to get time mysql Of Now() function :

But it also takes up space , And the field calling function may affect sql efficiency ; It's not a special recommendation ;
The deletion ID is set to NULL
This method is to set the deletion flag to NULL, It can solve the problem perfectly :

I think this method is the best , I also use this method ;

It can be seen that there will be no conflict ;
Be careful : Delete fields logically is_delete To set the allowed to null;
summary
I've also seen other ways : For example, create a history table 、redis Weight determination 、 New fields and indexes are uniquely constrained …
But these methods are not convenient for direct modification , It's a bit superfluous ;
Of course, the above is only a personal point of view , If you have any questions, please contact us ;
Put it together :
边栏推荐
- 机器学习编译入门课程学习笔记第二讲 张量程序抽象
- win11更新后任务栏空白怎么办? win11更新后任务栏空白卡死的解决方法
- Online education program user login and registration
- KVM IO性能测试数据
- [Android, kotlin, tflite] mobile device integration deep learning light model tflite (object detection)
- Store Nagios monitoring information into MySQL
- 去中心化交易所系统开发技术原理丨数字货币去中心化交易所系统开发(说明案例)
- 基于kubernetes平台微服务的部署
- Nansen double disk encryption giant self rescue: how to prevent the collapse of billions of dominoes
- Swift 5.0 - creation and use of swift framework
猜你喜欢

电脑设备管理器在哪里可以找到

如何使用 DataAnt 监控 Apache APISIX

Label Contrastive Coding based Graph Neural Network for Graph Classification

项目管理到底管的是什么?

Jmeter跨线程参数关联无需脚本

唯一性索引与逻辑删除冲突问题解决思路

Where can I find the computer version of wechat files

2022-06-30:以下golang代码输出什么?A:0;B:2;C:运行错误。 package main import “fmt“ func main() { ints := make

分享十万级TPS的IM即时通讯综合消息系统的架构

The Sandbox 正在 Polygon 网络上进行部署
随机推荐
Some memory problems summarized
What does project management really manage?
十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言
部门新来了个阿里25K出来的,让我见识到了什么是天花板
腾讯3年,功能测试进阶自动化测试,送给在手工测试中迷茫的你
What does the software test report contain? How to obtain high quality software test reports?
Braces on the left of latex braces in latex multiline formula
总结的一些内存问题
Meet the StreamNative | 杨子棵:是什么让我放弃了大厂 Offer
Online education program user login and registration
leetcode:104. 二叉树的最大深度
During telecommuting, the project team punched in the wechat group | solicited papers from the community
严格次小生成树
Cas classique multithreadé
Neo4j load CSV configuration and use
How to ensure the security of our core drawings by drawing encryption
Redis的事务和锁机制
What are database OLAP and OLTP? Same and different? Applicable scenarios
Detailed explanation of conv2d of pytorch