【问题标题】:Unique index/constraint on 2 columns in same table in OracleOracle中同一表中2列的唯一索引/约束
【发布时间】:2020-05-10 00:31:06
【问题描述】:

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

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

【问题讨论】:

标签: sql database oracle indexing constraints


【解决方案1】:

尝试在索引/约束中的列上使用 NVL 函数。

【讨论】:

    【解决方案2】:

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

    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 是一个 dbfiddle。

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多