当前位置:网站首页>唯一性索引与逻辑删除冲突问题解决思路
唯一性索引与逻辑删除冲突问题解决思路
2022-06-30 22:30:00 【YXXYX】
引言
今天做项目中遇到了一个问题:逻辑删除和唯一性索引同时存在的情况下,已经逻辑删除的数据由于唯一性索引的约束无法再次添加,该怎么办?
这个问题在之前做其他项目中也遇到过,当时并没有想到什么好的解决方法,也没有查资料,自己想了一个暴力方法:
我直接把唯一性索引去掉了,然后在代码逻辑实现部分用代码对唯一性索引字段进行了唯一性约束;
这当然也解决了问题,但是这样做本来就是不对的,设计好的数据库怎么能随意修改;
这次又遇到了一样的问题,想到上次的那种方法不仅不合规,而且非常麻烦(为了一个唯一性逻辑判断再调用数据库查询然后再写逻辑判断),这次就找了一下解决方法,下面简单介绍一下;
解决思路
所有解决方法就一个思路:通过逻辑删除字段和约束字段组合为一个索引;
比如有一张teacher表,唯一性索引为name,逻辑删除字段为is_delete:

唯一性索引:

为了解决冲突问题,首先需要修改一下索引,让逻辑删除字段和唯一字段一起作为索引:

但是还需要注意一点:一般我们习惯把逻辑删除字段is_delete设为0存在1删除;但是这里就不能这样了;
比如删除上表中test字段,索引可以看为test和1的组合;再次创建test并不会出现问题,因为新创建的索引为test和0的组合,但是如果再次删除test那么就又出现test和1的组合,索引冲突;
所以我们唯一要做的就是想办法让逻辑删除字段的删除标识唯一,未删除字段不需要改变;
下面有三种方法:
删除标识设置为主键id
因为主键id是唯一的,所以只需要将逻辑删除字段的删除标识设置为主键即可:

但是这种方法我认为如果id过长,会不会占用更多的空间?(个人想法)
就比如这个teacher表中我只把is_delete字段设置为tinyint长度为3,如果主键过长肯定需要修改为bigint,麻烦不说,是不是还占用了更多的空间;

删除标识设置为当前时间
这个方法就类似与将删除标识设置为一个时间戳,因为每一段时间都是不一样的,所以自然也不会有重复一说:
那么获取时间可以使用mysql的Now()函数:

但是这样同样也占用空间,并且该字段调用函数可能会影响sql效率;并不是特别推荐;
删除标识设置为NULL
这个方法就是把删除标识设置为NULL,可以完美解决该问题:

我认为这个方法是最好的,我也是使用的这个方法;

可以看到并不会产生冲突;
注意:逻辑删除字段is_delete要设置允许为null;
总结
我也见了其他方法:比如创建历史表、redis判重、新增字段与索引进行唯一约束…
但是这些方法感觉都没有直接修改来的方便,有点多此一举;
当然以上仅是个人观点,如果有问题欢迎交流;
汇总一下:
边栏推荐
- ESP8266 成为客户端和服务器
- Nansen复盘加密巨头自救:如何阻止百亿多米诺倾塌
- Uniapp routing uni simple router
- 多線程經典案例
- What is the experience of pairing with AI? Pilot vs alphacode, Codex, gpt-3
- Is the stock account opened in qiniu safe? How to apply for a low commission stock account?
- Femas:云原生多运行时微服务框架
- leetcode:104. Maximum depth of binary tree
- 云游戏| 云计算推动游戏行业进入“新纪元”
- [Android, kotlin, tflite] mobile device integration depth learning light model tflite (image classification)
猜你喜欢

How to change the win11 computer name? Win11 method of changing computer name
![[micro service ~nacos] configuration center of Nacos](/img/c3/9d8fb0fd49a0ebab43ed604f9bd1cc.png)
[micro service ~nacos] configuration center of Nacos

Fastjson V2 简单使用手册

What does the &?

Label Contrastive Coding based Graph Neural Network for Graph Classification

A new one from Ali 25K came to the Department, which showed me what the ceiling is

Redis的事务和锁机制

Alibaba Kube eventer MySQL sink simple usage record

How to upload binary pictures in uniapp

Spark - understand partitioner in one article
随机推荐
Anfulai embedded weekly report no. 271: June 20, 2022 to June 26, 2022
2022-06-30:以下golang代码输出什么?A:0;B:2;C:运行错误。 package main import “fmt“ func main() { ints := make
Meet the StreamNative | 杨子棵:是什么让我放弃了大厂 Offer
Smart streetlights | cloud computing lights up the "spark" of smart cities
10 airbags are equipped as standard, and Chery arizer 8 has no dead corner for safety protection
[450. delete nodes in binary search tree]
CNN classic network model details -lenet-5 (pytorch Implementation)
微信小程序中的数据双向绑定
Win11如何优化服务?Win11优化服务的方法
部门新来了个阿里25K出来的,让我见识到了什么是天花板
Tencent has been conducting advanced automated functional testing for 3 years. It is a gift to you who are confused in manual testing
Cloud games | cloud computing drives the game industry into a "new era"
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Mysql:sql overview and database system introduction | dark horse programmer
在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程...
2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main() { ints := make
What are database OLAP and OLTP? Same and different? Applicable scenarios
KVM IO performance test data
B_ QuRT_ User_ Guide(31)
How cloud computing can protect online education in the post epidemic Era