【Oracle】【13】特殊索引(如status=1时,id唯一)

前言:用户上传图片,表中数据为[{id:1, user_id:U001, pic:1.jpg, status:1}];用户重新上传图片,表中的数据变为[{id:1, user_id:U001, pic:1.jpg, status:0}; {id:2, user_id:U001, pic:2.jpg, status:1}]。一个用户只能有一条有效数据,设置唯一性约束的时候就很难设置了。之后发现了特殊索引的设置方法,可以满足这个要求

正文:

create unique index aname on table_name(decode(status, 1, id, null));

参考博客:

Oracle索引也可以这么建 - 关注系统性能调优 - CSDN博客
https://blog.csdn.net/stevendbaguo/article/details/54906555

原文地址:https://www.cnblogs.com/huashengweilong/p/10810716.html