在Oracle中没有用于添加外键约束的Alter表的操作定义

时间:2018-12-06 12:22:34

标签: oracle oracle11g oracle10g oracle12c

我正在使用以下查询更改表约束。

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

我发现使用下面的URL不能在oracle脚本中使用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))

因此,现在我的问题是如何在oracle脚本中使用NO ACTION,以便在dba_constraints视图的delete_rule列中获得NO ACTION作为值。

预先感谢...

2 个答案:

答案 0 :(得分:0)

这将是一个很长的答案,因为我正在显示不同数据库版本中的情况。

您会注意到它们的行为都相同:

  • 外键约束可以设置为cascadeset 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

11g

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

10g

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

答案 1 :(得分:0)

我删除了约束,并使用以下查询重新创建了约束,发现现在dba_constraints视图的delete_rule列中的值为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))

它按预期工作。