Позвольте мне попытаться объяснить это.Как сказал Эрик, ограничение уникального ключа будет принимать (много) пустых значений для ограниченного столбца.
Сначала создайте таблицу с ограничением уникального ключа (того, что у вас есть сейчас):
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 к этому столбцу.
В качестве альтернативы,
- удалить все ограничения внешнего ключа
- удалить уникальный ключ
- создать первичный ключ
- воссоздать ограничения внешнего ключа