当前位置:网站首页>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 :
边栏推荐
- 总结的一些内存问题
- Architecture of IM integrated messaging system sharing 100000 TPS
- 在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
- Store Nagios monitoring information into MySQL
- 在线客服系统代码_h5客服_对接公众号_支持APP_支持多语言
- MIT博士论文 | 优化理论与机器学习实践
- In depth analysis of Apache bookkeeper series: Part 4 - back pressure
- The sandbox is being deployed on the polygon network
- Online customer service system code_ H5 customer service_ Docking with official account_ Support app_ Support for multiple languages
- [career planning for Digital IC graduates] Chap.1 overview of IC industry chain and summary of representative enterprises
猜你喜欢
![[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

Two dots on the top of the latex letter
![[450. delete nodes in binary search tree]](/img/fd/bab2f92edeadd16263f15de6cc4420.png)
[450. delete nodes in binary search tree]

【Android,Kotlin,TFLite】移动设备集成深度学习轻模型TFlite(物体检测篇)

dba

The sandbox is being deployed on the polygon network

Redis - 01 cache: how to use read cache to improve system performance?

Fastjson V2 简单使用手册

As the public cloud market enters the deep water, can the calm Amazon cloud still sit still?

Redis的缓存穿透、缓存击穿和缓存雪崩
随机推荐
Tencent has been conducting advanced automated functional testing for 3 years. It is a gift to you who are confused in manual testing
手机上怎么开股票账户?另外,手机开户安全么?
Doker的容器数据卷
JVM Part 21 of interview with big companies Q & A
Where can I find the computer device manager
CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
Cas classique multithreadé
hot-fix、cherry-pick怎么提
Swift 5.0 - creation and use of swift framework
「团队训练赛」ShanDong Multi-University Training #3
D compile time count
严格次小生成树
What is flush software? In addition, is it safe to open an account online now?
The Sandbox 正在 Polygon 网络上进行部署
Two way data binding in wechat applet
B_ QuRT_ User_ Guide(33)
Store Nagios monitoring information into MySQL
Esp8266 becomes client and server
如何使用 DataAnt 监控 Apache APISIX
[micro service ~nacos] configuration center of Nacos