【问题标题】:Unique constraint on multiple columns对多列的唯一约束
【发布时间】:2010-12-22 22:04:47
【问题描述】:

我正在使用一个 oracle 表,并在四列上创建了一个唯一约束。约束中的这些列可以包含 NULL 吗?

【问题讨论】:

  • 在我看来,通过测试找出答案是非常微不足道的。这样做可能需要一分钟以上的时间吗?
  • 是的 - 你是对的。但我从 Vincent、Amber 和 Shoover 发布的答案中了解到了其他信息。

标签: oracle unique-constraint


【解决方案1】:

是的,Oracle 允许 UNIQUE 约束包含具有 NULL 内容的列,但 PRIMARY KEY 约束不能包含包含 NULL 值的列。 (已编辑:是“...可为空的列...”,但我下面的示例表明这不是真的。PK 中的列可以定义为可为空,但不能包含 NULL。)

UNIQUE 约束和 PRIMARY KEY 约束不能具有相同的列。

SQL> create table stest (col1 integer not null, col2 integer null);

Table created.

SQL> alter table stest add constraint stest_uq unique (col1, col2);

Table altered.

SQL> insert into stest values (1, 3);

1 row created.

SQL> insert into stest values (1, null);

1 row created.

SQL> insert into stest values (1, null);
insert into stest values (1, null)
*
ERROR at line 1:
ORA-00001: unique constraint (SUSAN_INT.STEST_UQ) violated

SQL> insert into stest values (2, null);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from stest;

      COL1       COL2
---------- ----------
         1          3
         1
         2

SQL> alter table stest add constraint stest_pk PRIMARY KEY (col1, col2);
alter table stest add constraint stest_pk PRIMARY KEY (col1, col2)
                                                             *
ERROR at line 1:
ORA-01449: column contains NULL values; cannot alter to NOT NULL

SQL> truncate table stest;

Table truncated.

SQL> alter table stest add constraint stest_pk PRIMARY KEY (col1, col2);
alter table stest add constraint stest_pk PRIMARY KEY (col1, col2)
                                          *
ERROR at line 1:
ORA-02261: such unique or primary key already exists in the table

SQL> alter table stest drop constraint stest_uq;

Table altered.

SQL> alter table stest add constraint stest_pk PRIMARY KEY (col1, col2);

Table altered.

【讨论】:

    【解决方案2】:

    你的列中可以有 NULL,除非列被指定为 NOT NULL。但是,您将只能存储一个 NULL 实例(除非所有列都是 NULL,否则不允许两组相同的列):

    SQL> CREATE TABLE t (id1 NUMBER, id2 NUMBER);
    
    Table created
    SQL> ALTER TABLE t ADD CONSTRAINT u_t UNIQUE (id1, id2);
    
    Table altered
    SQL> INSERT INTO t VALUES (1, NULL);
    
    1 row inserted
    SQL> INSERT INTO t VALUES (1, NULL);
    
    INSERT INTO t VALUES (1, NULL)
    
    ORA-00001: unique constraint (VNZ.U_T) violated
    
    SQL> /* you can insert two sets of NULL, NULL however */
    SQL> INSERT INTO t VALUES (NULL, NULL);
    
    1 row inserted
    SQL> INSERT INTO t VALUES (NULL, NULL);
    
    1 row inserted
    

    【讨论】:

      【解决方案3】:

      Oracle 中认为两个空值不相等,因此这些列中可以包含空值。

      【讨论】:

      • 在我的情况下(Oracle 数据库 11g 快捷版 11.2.0.2.0 - 64 位生产),这不是事实。我通过两列(可为空的 VARCHAR2 和 NUMBER)使用多个约束,并且我不能插入两行具有空和相同数字的行。
      • 我认为在这些情况下最好用一个例子作为其他(和接受的)答案来回答。这样,它就可以解决问题并表明您的陈述是有效的。在这种情况下,我认为该陈述不正确,并且接受的答案也确切说明了为什么它不正确。
      猜你喜欢
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 2016-08-22
      • 2013-08-23
      • 2011-09-10
      • 2013-05-25
      • 1970-01-01
      相关资源
      最近更新 更多