首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >drop table是否也删除约束?

drop table是否也删除约束?
EN

Stack Overflow用户
提问于 2017-04-19 14:32:30
回答 1查看 9.5K关注 0票数 8

当我对一个表执行drop操作时,它是否也删除了约束?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-19 15:28:01

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

代码语言:javascript
复制
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> 

我们能不能不提了?是的,我们可以!

代码语言:javascript
复制
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。它由外键引用,并在视图中使用。

代码语言:javascript
复制
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> 

所以我们可以放弃这张表了吗?

代码语言:javascript
复制
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不支持它。没有必要这样做,我们不能删除强制关系完整性的表...除非我们坚持这样做:

代码语言:javascript
复制
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),但这些视图处于无效状态。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43488105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档