赞
踩
可选项,方案,这里可以理解为用户名, 缺省为当前用户下的表。
比如,要删除scott用户下的emp表,
drop table scott.emp
可选项,如果开启了回收站(oracle 10g以后,回收站默认是开启的)功能,
不带purge选项,表示删除的表放进回收站,空间不回收 。使用flashbask可以闪回该表。
带purge选项,则表示将表删除,释放空间。
可选项,如果有其它表关联到要删除的表的主键列或唯一键列(要删除的表是主表(父表),从表(子表)有外键关联到这张表),那么,直接用drop table会报错,这时候如果想要强制删除这张表,就需要加上cascade constraints选项
SQL> drop table t;
drop table t
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> drop table t1;
Table dropped.
SQL> create table t(id number,name varchar2(20));
Table created.
SQL> create table t1(id number,sal number);
Table created.
SQL>
SQL> alter table t add constraint t_pk primary key(id);
Table altered.
--在T表上添加主键
SQL> alter table t1 add constraint t_fk foreign key(id) references t(id);
Table altered.
--在t1表上添加外键,关联到t表的主键列
SQL> insert into t values (1,'Smith');
1 row created.
SQL> insert into t values (2,'John');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> insert into t1 values(1,3000);
1 row created.
SQL> insert into t1 values(2,4000);
1 row created.
SQL> commit;
Commit complete.
SQL> insert into t1 values(3,200);
insert into t1 values(3,200)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.T_FK) violated - parent key not found
SQL>
SQL> drop table t;
drop table t
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
-- 删除不成功
SQL> drop table t cascade constraints;
Table dropped.
--加上cascade constrants项,删除成功
SQL> select * from t1;
ID SAL
---------- ----------
1 3000
2 4000
SQL> select CONSTRAINT_NAME,TABLE_NAME from dba_constraints where owner='SOTT'and TABLE_NAME='T1';
no rows selected
SQL>

2.purge选项实验
--清空回收站
SQL> Purge recyclebin;
Recyclebin purged.
--建测试表
SQL> create table t(id int,name varchar2(10));
Table created.
--删除表不,加purge选项
SQL> drop table t;
Table dropped.
--删除的表已经放进回收站,只不过改了个表名
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
T BIN$Nl2YJRjLSyyJ45+XGN7pwA==$0 TABLE 2014-04-04:00:42:10
--闪回t表
SQL> flashback table t to before drop;
Flashback complete.
--t表已经回来了
SQL> select * from t;
no rows selected
--删除t表,加purge选项
SQL> drop table t purge;
Table dropped.
--没有放进回收站,回收站是空的
SQL> show recyclebin;
SQL>

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。