drop table是否也会删除约束?

Sim*_*n B 3 sql oracle ddl

当我drop一张桌子的时候它也放弃了约束?

APC*_*APC 8

这是一张简单的表格.它有一个索引,一些完整性约束和一个触发器:

SQL> desc t69
 Name               Null?    Type
 ------------------ -------- ----------------------------
 ID                 NOT NULL NUMBER

SQL> select index_name from user_indexes  where table_name = 'T69';

INDEX_NAME
------------------------------
SYS_C0034158

SQL> select constraint_name, constraint_type from user_constraints where table_name = 'T69';

CONSTRAINT_NAME                C
------------------------------ -
SYS_C0034157                   C
SYS_C0034158                   P

SQL> select trigger_name from user_triggers where table_name = 'T69';

TRIGGER_NAME
------------------------------
TRG69

SQL> 
Run Code Online (Sandbox Code Playgroud)

我们可以放弃吗?我们可以!

SQL> drop table t69;

Table dropped.

SQL> select constraint_name, constraint_type from user_constraints where table_name = 'T69';

no rows selected

SQL> select trigger_name from user_triggers where table_name = 'T69';

no rows selected

SQL> 
SQL> select index_name from user_indexes  where table_name = 'T69';

no rows selected

SQL> 
Run Code Online (Sandbox Code Playgroud)

什么都没有.当其他对象引用该表时,它会有所不同.

还有另一张桌子,P23.它由外键引用并在视图中使用.

SQL> create table c23 (id number, p_id number);

Table created.

SQL> alter table c23 add foreign key (p_id) references p23;

Table altered.

SQL> create view v23 as select * from p23;

View created.

SQL> 
Run Code Online (Sandbox Code Playgroud)

那么我们可以放弃这张桌子吗?

SQL> drop table p23 ;
drop table p23
           *
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys


SQL> 
Run Code Online (Sandbox Code Playgroud)

不,我们不可以.顺便提一下,关于RESTRICT语法,Oracle不支持.没有必要,我们不能删除强制关系完整性的表格......除非我们坚持这样做:

SQL> drop table p23 cascade constraints;

Table dropped.

SQL> desc t23
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 COLA                                               NUMBER
 COLB                                               NUMBER
 COLC                                               NUMBER
 GENDER                                             VARCHAR2(1)
 ID                                                 NUMBER

SQL> select * from v23
  2  /
select * from v23
              *
ERROR at line 1:
ORA-04063: view "FOX.V23" has errors


SQL> 
Run Code Online (Sandbox Code Playgroud)

CASCADE CONSTRAINTS子句删除表和引用它的任何外键.子表保持不变.引用该表的视图(以及任何PL/SQL)仍处于无效状态.