当我对一个表执行drop操作时,它是否也删除了约束?
发布于 2017-04-19 15:28:01
这是一个简单的表格。它有一个索引,一些完整性约束和一个触发器:
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> 我们能不能不提了?是的,我们可以!
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> 什么都没有留下。当其他对象引用该表时,情况会有所不同。
还有一张表,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> 所以我们可以放弃这张表了吗?
SQL> drop table p23 ;
drop table p23
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
SQL> 不我们不能。顺便说一句,关于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> CASCADE CONSTRAINTS子句删除表和引用它的任何外键。子表在其他方面保持不变。将保留引用该表的视图(以及任何PL/SQL),但这些视图处于无效状态。
https://stackoverflow.com/questions/43488105
复制相似问题