I need to create a unique constraint/index on 2 columns in a table. One of these 2 columns can be null
. My unique constraint/index needs to check if these 2 columns are not null then they cannot be duplicate.
Not sure how to tackle that, any starters?
You can use a primary key and function-based index for this. Here is an example:
create table t (
id int primary key,
x int,
y int
);
create unique index t_x_y on t(x, y, (case when x is null or y is null then id end));
Here is a db<>fiddle.
Try using the NVL function on the column in the index/constraint.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.