当前位置:网站首页>When a logical deletion encounters a unique index, what are the problems and solutions?
When a logical deletion encounters a unique index, what are the problems and solutions?
2022-06-11 02:25:00 【Bean craftsman coding】
Logical deletion of database
Logical deletion of database , It is relative to physical deletion .
Data is priceless , Once physically deleted , Do not consider passing binlog、 Other technical means , It's hard to get back .
The method of logical deletion is to set a deletion field , We call it is_del, The initial value is 0.
If the user deletes data , We put is_del Field set to 1, In the query , Ignore is_del Field is 1 The data of , Thus, the user perceives that the data has been deleted , But the data still has the effect of database , This is logical deletion .
Conflict between logical deletion and unique index
Imagine a scenario , Data sheet user Of username Fields are unique , That is, the name cannot be duplicated .
To ensure the uniqueness of this field , We are username Add a unique index to the field unique.
At this time, Zhang San registered an account ,username The field is Zhang San .
One day Zhang San cancelled his account , Let's Take Zhang San's data is_del Field set to 1.
But Zhang San came back half a month later , The user name is still registered as Zhang San , However, it prompts that the registration fails .
This is because the unique index retrieves username = Zhang San The record of exists , This obviously does not conform to our daily logic .
Solution
We need to ensure that the logically deleted data does not affect the effect of the unique index .is_del We no longer only save 0 and 1, Instead, it is modified to the timestamp at the time of deletion , Then create a federated unique index uk(username, is_del).
In this way, even if Zhang San cancels his account many times , Because the time of logical deletion is different , There will be no unique index conflict .
边栏推荐
- The largest kth element in the array
- [3.delphi common components] 5 List class component
- 环糊精金属有机骨架(β-CD-MOF)装载二巯丁二酸/大黄素/槲皮素/三氯蔗糖/二氟尼柳/奥美拉唑(OME)
- 2022 simulated 100 questions and answers for crane driver (limited to bridge crane) examination
- 接口自动化核心知识点浓缩,为面试加分
- SAP smartforms page feed printing automatic judgment
- Using an old mobile phone to build a server and achieve intranet penetration does not require root (I have personally tested the simplest one many times)
- Switch case uses enumeration classes to compare
- C language principle explanation and code implementation of scalable / reduced thread pool
- Tencent test development post interview programming questions
猜你喜欢

During SSH configuration key login, you need to pay attention to whether the private key is set with a password

软件测试面试复盘:技术面没有难倒我,hr面却是一把挂

Li Kou brushing questions - hash table

贵金属白银行情走势图缘何强势?

14:00面试,14:08就出来了 ,问的实在是太...

Unity serial port communication

【并行与分布式系统】Cache学习

Shader of double sided material

JS basic part hand exercises

The diligent is the laziest
随机推荐
【无标题】
SD3.0笔记
接口自动化核心知识点浓缩,为面试加分
Project records
Bingbing learning notes: find the greatest common divisor and the least common multiple. Complex version reverse string
Implementing queues with stacks
贵金属白银和现货白银之间是什么关系
10007. ISBN number
Do tween record
常见漏洞的防御措施整理
Knowledge competition of safety production month -- how much do you know about new safety law
心态不能崩......
SSH配置密钥登录时需要注意私钥是否设置了密码(passphrase)
Me11/me12 purchase information record and condition record creation and update bapi:me_ INFORECORD_ MAINTAIN_ MULTI
What do you know about the set class? Soul questions from Volume I
Ortele has obtained three rounds of financing nine months after its establishment, and hard discount stores have found new ways to grow?
Li Kou brushing questions - hash table
Shell learning tutorial (super detailed and complete)
【并行与分布式系统】Cache学习
Tencent test development post interview programming questions