具有PK和具有UNIQUE约束的表是否有任何区别

时间:2019-09-24 19:13:47

标签: sql oracle oracle11g primary-key

在TOAD中,我可以看到我的桌子说没有PK。但是PK候选者有一个独特的约束条件。

enter image description here

我尝试将字段PK设置为PK,但是toad表示表上已经存在一个约束。

I

并且不能删除约束,因为说其他人依赖它。

所以我应该这样保留它。还是要加倍努力,禁用所有依赖关系,删除唯一约束并创建PK?

2 个答案:

答案 0 :(得分:2)

让我尝试解释一下。正如Eric所说,唯一键约束将为约束列接受(许多)空值。

首先,创建一个具有唯一键约束的表(您现在拥有的表):

SQL> create table test (id     number constraint uk_test unique,    --> unique key constraint
  2                     name   varchar2(20));

Table created.

SQL> -- This is the first record - no problem with it
SQL> insert into test (id, name) values (1, 'Little');

1 row created.

SQL> -- Uniqueness violated
SQL> insert into test (id, name) values (1, 'Foot');
insert into test (id, name) values (1, 'Foot')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_TEST) violated


SQL> -- Let's insert some NULL values into the constrained column
SQL> insert into test (id, name) values (null, 'Foot');

1 row created.

SQL> insert into test (id, name) values (null, 'Big');

1 row created.

SQL> -- The result: not too pretty, eh?
SQL> select * From test;

        ID NAME
---------- --------------------
         1 Little
           Foot
           Big

进一步:对唯一键列应用新的NOT NULL约束,以使其像主键一样“起作用”:

SQL> delete from test;

3 rows deleted.

SQL> -- add NOT NULL constraint
SQL> alter table test modify id not null;

Table altered.

SQL> -- The first record is OK
SQL> insert into test (id, name) values (1, 'Little');

1 row created.

SQL> -- Uniqueness violated
SQL> insert into test (id, name) values (1, 'Foot');
insert into test (id, name) values (1, 'Foot')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UK_TEST) violated


SQL> -- This worked previously, but won't any longer because of the NOT NULL constraint
SQL> insert into test (id, name) values (null, 'Foot');
insert into test (id, name) values (null, 'Foot')
                                    *
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."ID")

最后,为了说明这一点,它就像是一个主键约束一样:

SQL> delete from test;

1 row deleted.

SQL> -- Let's drop the unique key constraint
SQL> alter table test drop constraint uk_test;

Table altered.

SQL> -- Add the primary key constraint (no duplicates, no nulls)
SQL> alter table test add constraint pk_test primary key (id);

Table altered.

SQL> -- The first record is OK
SQL> insert into test (id, name) values (1, 'Little');

1 row created.

SQL> -- Uniqueness violated
SQL> insert into test (id, name) values (1, 'Foot');
insert into test (id, name) values (1, 'Foot')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.PK_TEST) violated


SQL> -- Not null violated
SQL> insert into test (id, name) values (null, 'Foot');
insert into test (id, name) values (null, 'Foot')
                                    *
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."ID")

基本上,现在您遇到了与以前相同的错误,即primary key = unique key + NOT NULL


如果列已被​​唯一键约束,则不能创建主键-您已经知道。

由于您不能删除唯一键约束(因为外键引用了它),因此将NOT NULL约束应用于该列。

或者,

  • 删除所有外键约束
  • 放置唯一键
  • 创建主键
  • 重新创建外键约束

答案 1 :(得分:0)

您可以通过三个 easy 步骤来替换PK的UNIQUE约束:

  • 只需创建具有相同列的PK。
  • 然后更改所有FK以使其指向PK约束而不是UNIQUE约束。
  • 最后,删除UNIQUE约束。

容易