首页 > 解决方案 > Oracle中同一表中2列的唯一索引/约束

问题描述

我需要在表中的 2 列上创建唯一约束/索引。这 2 列之一可以是null. 我的唯一约束/索引需要检查这 2 列是否不为空,那么它们不能重复。

不知道如何解决这个问题,任何初学者?

标签: sqldatabaseoracleindexingconstraints

解决方案


您可以为此使用主键和基于函数的索引。这是一个例子:

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));

是一个 db<>fiddle。


推荐阅读