Нет определения действия для таблицы Alter для добавления ограничения внешнего ключа в Oracle - PullRequest
0 голосов
/ 06 декабря 2018

Я изменяю ограничение таблицы, используя запрос ниже.

ALTER TABLE MY_Table ADD (CONSTRAINT MT_ID_FK FOREIGN KEY (ID) REFERENCES YOUR_TABLE (ID) ON DELETE NO ACTION)

Я обнаружил, используя URL ниже, что мы не можем использовать NO ACTION в скрипте оракула. ​​

https://www.haidongji.com/2006/07/24/defining-no-action-foreign-key-constraints-in-oracle/comment-page-1/

, но если мы используем приведенный ниже скрипт для изменения таблицы, то в представлении dba_constraints значение столбца delete_rule отображается как пустое.

ALTER TABLE MY_Table ADD (CONSTRAINT MT_ID_FK FOREIGN KEY (ID) REFERENCES YOUR_TABLE (ID))

Итак, теперь мой вопрос заключается в том, как использовать NO ACTION в скрипте оракула, чтобы мы могли получить NO ACTION в качестве значения в столбце delete_rule представления dba_constraints.

Заранее спасибо ...

Ответы [ 2 ]

0 голосов
/ 07 декабря 2018

Я удалил ограничение и пересоздал его, используя приведенные ниже запросы, и обнаружил, что теперь в столбце delete_rule представления dba_constraints есть NO ACTION в качестве значения.

Удаление запроса

ALTER TABLE M_SP3D_EXP_JOB_SEL_CLIS DROP CONSTRAINT 'MT_ID_FK';

Изменить запрос

ALTER TABLE MY_Table ADD (CONSTRAINT MT_ID_FK FOREIGN KEY (ID) REFERENCES YOUR_TABLE (ID))

Работает, как и ожидалось.

0 голосов
/ 06 декабря 2018

Это будет довольно длинный ответ, так как я показываю, что происходит в разных версиях базы данных.

Вы заметите, что все они ведут себя одинаково:

  • ограничение внешнего ключа может быть установлено на cascade и set null, и они оба отображаются в user_constraints.delete_ruleстолбец
  • no action является недопустимым параметром (поэтому вы не можете ожидать, что увидите что-либо в delete_rule)

Вот так:

12c

SQL> select * from v$version;

BANNER                                                                               CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production              0
PL/SQL Release 12.2.0.1.0 - Production                                                    0
CORE    12.2.0.1.0      Production
TNS for Linux: Version 12.2.0.1.0 - Production                                            0
NLSRTL Version 12.2.0.1.0 - Production                                                    0

SQL> create table your_table (id number primary key);

Table created.

SQL> create table my_table (id number);

Table created.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete cascade;

Table altered.

SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

DELETE_RU
---------
CASCADE

SQL> alter table my_table drop constraint fk_my_your;

Table altered.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete set null;

Table altered.

SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

DELETE_RU
---------
SET NULL

SQL> alter table my_table drop constraint fk_my_your;

Table altered.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete no action;
  references your_table (id) on delete no action
                                       *
ERROR at line 2:
ORA-00905: missing keyword


SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

no rows selected

11 г

SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL> create table your_table (id number primary key);

Table created.

SQL> create table my_table (id number);

Table created.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete cascade;

Table altered.

SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

DELETE_RU
---------
CASCADE

SQL> alter table my_table drop constraint fk_my_your;

Table altered.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete set null;

Table altered.

SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

DELETE_RU
---------
SET NULL

SQL> alter table my_table drop constraint fk_my_your;

Table altered.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete no action;
  references your_table (id) on delete no action
                                       *
ERROR at line 2:
ORA-00905: missing keyword


SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

no rows selected

10 г

SQL> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for 64-bit Windows: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

SQL> create table your_table (id number primary key);

Table created.

SQL> create table my_table (id number);

Table created.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete cascade;

Table altered.

SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

DELETE_RU
---------
CASCADE

SQL> alter table my_table drop constraint fk_my_your;

Table altered.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete set null;

Table altered.

SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

DELETE_RU
---------
SET NULL

SQL> alter table my_table drop constraint fk_my_your;

Table altered.

SQL> alter table my_table add constraint fk_my_your foreign key (id)
  2    references your_table (id) on delete no action;
  references your_table (id) on delete no action
                                       *
ERROR at line 2:
ORA-00905: missing keyword


SQL> select delete_rule from user_constraints where constraint_name = 'FK_MY_YOUR';

no rows selected
...