Oracle Drop User使用说明

本文详细介绍了在Oracle数据库中如何使用DROP USER命令删除用户及其相关对象。包括仅删除用户本身,以及通过CASCADE选项删除用户的所有对象,如表、索引、触发器等,并解释了这些操作对数据库的影响。

在搭建测试DB时,常用到删除用户及其对象的命令,也即drop user

drop user xxx;  // 仅删掉用户xxx

drop user xxx cascade; // 会删除用户及用户对应的schema下所有对象

user
Specify the user to be dropped. Oracle Database does not drop users whose schemas 
contain objects unless you specify CASCADE or unless you first explicitly drop the 
user's objects.

CASCADE
 
Specify CASCADE to drop all objects in the user's schema before dropping the user. You 
must specify this clause to drop a user whose schema contains any objects. 

使用cascade参数可以删除该用户的全部objects。要说明的如下:

1 If the user's schema contains tables, then Oracle Database drops the tables and 
automatically drops any referential integrity constraints on tables in other schemas 
that refer to primary and unique keys on these tables. 

如果用户的schema中有表,则在删除表的时候自动删除与该表相关的主键和外键。

2 If this clause results in tables being dropped, then the database also drops all 
domain indexes created on columns of those tables and invokes appropriate drop
 
routines. 
如果用户的schema中有表,则在删除表的时候自动删除与该表相关的索引。

3 Oracle Database invalidates, but does not drop, the following objects in other 
schemas:
删除用户时,下列在其他用户中的objects不会被删除,只会被置为无效

1 Views or synonyms for objects in the dropped user's schema
视图,同义词

2 Stored procedures, functions, or packages that query objects in the dropped 
user's schema
存储过程,函数,包

4 Oracle Database does not drop materialized views in other schemas that are based 
on tables in the dropped user's schema. However, because the base tables no 
longer exist, the materialized views in the other schemas can no longer be 
refreshed.
其他用户建立的基于被删除用户的物化视图不会被删除,只是不能在刷新了。

5 Oracle Database drops all triggers in the user's schema.
用户模式下的所有触发器全部被删除

6 Oracle Database does not drop roles created by the user
被删除用户建立的其他用户不会被删除


以上文章转自(有部分修改):http://blog.youkuaiyun.com/hexiankang1984/article/details/6925180

### 如何在 Oracle 数据库中使用 DROP TABLE 命令删除表 #### DROP TABLE 的基本语法 在 Oracle 数据库中,`DROP TABLE` 是一种用于永久移除整个表及其所有关联对象的方法。此操作不仅会清除所有的记录,还会彻底销毁表结构以及任何依赖于该表的对象。 ```sql DROP TABLE table_name; ``` 这条语句执行后,指定名称的表将会从数据库模式中完全消失[^1]。 #### 安全地删除表并防止误操作 为了更加谨慎地处理删除动作,在实际应用中通常建议先确认目标表确实是要被删除的那个: ```sql BEGIN FOR rec IN (SELECT object_name FROM user_objects WHERE object_type='TABLE' AND object_name = 'YOUR_TABLE_NAME') LOOP EXECUTE IMMEDIATE 'DROP TABLE '||rec.object_name; END LOOP; END; / ``` 这段 PL/SQL 代码片段通过遍历 `USER_OBJECTS` 字典视图来验证是否存在名为 `'YOUR_TABLE_NAME'` 的表,并对其进行删除操作。这种方式有助于减少因拼写错误或其他原因造成的意外删除风险。 #### 结合 PURGE 关键字一次性完成清理工作 默认情况下,当发出 `DROP TABLE` 指令时,所涉及的数据会被移动到回收站而不是立即物理上消除;如果希望绕过这一机制,则可以在命令结尾附加 `PURGE` 参数实现更高效的资源释放过程。 ```sql DROP TABLE table_name PURGE; ``` 上述做法能够确保表一旦被丢弃就不会再出现在回收站里等待最终处置,从而节省存储空间和简化后续维护流程[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值