当前位置:网站首页>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 :
边栏推荐
- Jmeter跨线程参数关联无需脚本
- 电脑版微信文件存储在哪个文件夹可以找到
- Online customer service system code_ H5 customer service_ Docking with official account_ Support app_ Support for multiple languages
- 十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
- d编译时计数
- 图纸加密如何保障我们的核心图纸安全
- AtCoder Beginner Contest 257
- What does the software test report contain? How to obtain high quality software test reports?
- 唯一性索引与逻辑删除冲突问题解决思路
- Cas classique multithreadé
猜你喜欢

Nansen double disk encryption giant self rescue: how to prevent the collapse of billions of dominoes

电脑版微信文件存储在哪个文件夹可以找到

与AI结对编程式是什么体验 Copilot vs AlphaCode, Codex, GPT-3

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?

如何使用 DataAnt 监控 Apache APISIX

深入解析 Apache BookKeeper 系列:第四篇—背压

Where can I find the computer version of wechat files
![[career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises](/img/d3/68c9d40ae6e61efc10aca8bcc1f613.jpg)
[career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises

Kubevela 1.4: make application delivery safer, easier to use, and more transparent
随机推荐
分享十万级TPS的IM即时通讯综合消息系统的架构
Classic case of multithreading
hot-fix、cherry-pick怎么提
Redis的事务和锁机制
B_ QuRT_ User_ Guide(35)
深入解析 Apache BookKeeper 系列:第四篇—背压
与AI结对编程式是什么体验 Copilot vs AlphaCode, Codex, GPT-3
msf之ms17-010永恒之蓝漏洞
Using Obsidian with Hugo, markdown's local editing software is seamlessly connected with online
[450. delete nodes in binary search tree]
In depth analysis of Apache bookkeeper series: Part 4 - back pressure
将Nagios监控信息存入MySQL
CNN经典网络模型详解-LeNet-5(pytorch实现)
电脑版微信文件存储在哪个文件夹可以找到
What is the experience of pairing with AI? Pilot vs alphacode, Codex, gpt-3
Label Contrastive Coding based Graph Neural Network for Graph Classification
总结的一些内存问题
Technical principle of decentralized exchange system development - digital currency decentralized exchange system development (illustrative case)
10 airbags are equipped as standard, and Chery arizer 8 has no dead corner for safety protection
Ideal interface automation project