当前位置:网站首页>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 :
边栏推荐
- 理想中的接口自动化项目
- 100 important knowledge points that SQL must master: creating and manipulating tables
- Kubevela 1.4: make application delivery safer, easier to use, and more transparent
- The superficial understanding of the industrial Internet finally brought the development of the industrial Internet into the strange circle of the consumer Internet
- 企业出海数字化转型解决方案介绍
- 深入解析 Apache BookKeeper 系列:第四篇—背压
- As the public cloud market enters the deep water, can the calm Amazon cloud still sit still?
- [无线通信基础-13]:图解移动通信技术与应用发展-1-概述
- 零样本和少样本学习
- How to develop the exchange system? Mature technology case of digital currency exchange system development
猜你喜欢

Redis - 01 缓存:如何利用读缓存提高系统性能?

latex字母头顶两个点

Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?

2022中国国潮发展新动向

Esp8266 becomes client and server

Where can I find the computer device manager

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

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

Where can I find the computer version of wechat files

Introduction to digital transformation solutions for enterprises going to sea
随机推荐
微信支付WxPayPubHelper v3版 回调xml为空的原因
msf之ms17-010永恒之蓝漏洞
软件确认测试的内容和流程有哪些?确认测试报告需要多少钱?
电脑版微信文件存储在哪个文件夹可以找到
手机上怎么开股票账户?另外,手机开户安全么?
[golang] golang implements the string interception function substr
HP 惠普笔记本电脑 禁用触摸板 在插入鼠标后
理想中的接口自动化项目
[golang] golang实现截取字符串函数SubStr
Is it difficult to get a certified equipment supervisor? What is the relationship with the supervising engineer?
Web APIs comprehensive case -tab column switching - dark horse programmer
100 important knowledge points that SQL must master: creating and manipulating tables
公有云市场迈入深水区,冷静的亚马逊云还坐得住吗?
B_ QuRT_ User_ Guide(31)
B_ QuRT_ User_ Guide(35)
Apache服务器OpenSSL升级
Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction
严格次小生成树
Ideal interface automation project
d编译时计数