PostgreSQL 逻辑删除保证数据唯一
PostgreSQL 大约 441 字场景
有用户表(username
,del_flag
),保证用户名唯一。
方案一
username
唯一索引,逻辑删除后不能再新建相同名称的用户。
方案二
username - del_flag
联合唯一索引(del_flag
是布尔类型),第二次删除数据库会抛出异常。
方案二
username - del_flag
联合唯一索引(del_flag
是时间类型或文本类型),未删除的用户del_flag
为固定值,删除时设置del_flag
为最新的时间戳。
方案四(推荐)
username
唯一索引,删除用户后插入到user_deleted
表备份。
实现
使用with
语句,delete
的同时insert
到备份表中。
with cte as
(
delete from sys_user where username = 'admin' returning *
)
insert into sys_user_deleted select * from cte;
阅读 67 · 发布于 2023-11-20
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
PHP 使用 json_encode 格式化下标不为 0 开始的数组会转为对象阅读 2923
-
设计模式之原型模式 - 深拷贝和浅拷贝阅读 2104
-
Android 根据包名启动 Activity阅读 3230
-
Linux 查找文本中重复的内容阅读 4077
-
Python UnicodeDecodeError: 'gbk' codec can't decode byte 0x80阅读 3798
-
OpenResty 中模板渲染引擎 lua-resty-template阅读 6033
-
Java LockSupport 几种唤醒机制阅读 20
-
VS Code 下载慢解决办法阅读 526
-
Maven 打包报错 Couldn't retrieve @Mapper annotation阅读 4126
-
Linux 使用 dig 查询 DNS阅读 1096