日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Oracle 刪除用戶和表空間

系統(tǒng) 2320 0

Oracle 使用時間長了, 新增了許多user 和tablespace. 需要清理一下

對于單個user和tablespace 來說, 可以使用如下命令來完成。

? 步驟一:? 刪除user

drop user ×× cascade

說明: 刪除了user,只是刪除了該user下的schema objects,是不會刪除相應的tablespace的。

步驟二: 刪除tablespace

DROP TABLESPACE tablespace_name INCLUDING CONTENTS AND DATAFILES;



但是,因為是供開發(fā)環(huán)境來使用的db, 需要清理的user 和 table space 很多。

思路

?Export出DB中所有的user和tablespace, 篩選出系統(tǒng)的和有用的tablespace,把有用的信息load到一張表中去。

然后寫例程循環(huán),把不在有用表的tablespace刪掉

1. select username,default_tablespace from dba_users;

2.??

create table MTUSEFULSPACE
(
?? ID Number(4) NOT NULL PRIMARY KEY,
?? USERNAME varchar2(30),
?? TABLESPACENAME varchar2(60),
?? OWNERNAME varchar2(30)
);

3.

declare icount number(2);
??????? tempspace varchar2(60);
begin
? for curTable in (select username as allusr,default_tablespace as alltblspace from dba_users)
? loop
? tempspace :=curTable.alltblspace;
? dbms_output.put_line(tempspace);
? select count(TABLESPACENAME) into icount from MTUSEFULSPACE where TABLESPACENAME = tempspace;
? if icount=0 then
??? DROP TABLESPACE tempspace INCLUDING CONTENTS AND DATAFILES;
? end if;
? commit;
? end loop;
end;


執(zhí)行后會報如下錯誤

ORA-06550: 第 10 行, 第 5 列:?
PLS-00103: 出現(xiàn)符號 "DROP"在需要下列之一時:
?begin case declare exit
?? for goto if loop mod null pragma raise return select update
?? while with <an identifier>
?? <a double-quoted delimited-identifier> <a bind variable> <<
?? close current delete fetch lock insert open rollback
?? savepoint set sql execute commit forall merge pipe
06550. 00000 -? "line %s, column %s:\n%s"
*Cause:??? Usually a PL/SQL compilation error.
*Action:


好像是被鎖了。。



沒辦法,例程不能寫,就只能組出語句執(zhí)行了。

把需要刪除的user, tablespace 導出到Excel. 使用CONCATENATE 組出SQL.

貼到SQLdevelop 批量執(zhí)行。


整個刪除會比較耗時間, 100多個user.? 用了12個小時左右。



如要找datafile的具體位置,可以使用

select t1.name,t2.name from v$tablespace t1, v$datafile t2 where t1.ts# = t2.ts#;


SQL code
--刪除空的表空間,但是不包含物理文件
drop tablespace tablespace_name;
--刪除非空表空間,但是不包含物理文件
drop tablespace tablespace_name including contents;
--刪除空表空間,包含物理文件
drop tablespace tablespace_name including datafiles;
--刪除非空表空間,包含物理文件
drop tablespace tablespace_name including contents and datafiles;
--如果其他表空間中的表有外鍵等約束關聯(lián)到了本表空間中的表的字段,就要加上CASCADE CONSTRAINTS
drop tablespace tablespace_name including contents and datafiles CASCADE CONSTRAINTS;

?

Oracle 刪除用戶和表空間


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 通化市| 九台市| 惠州市| 宜阳县| 崇信县| 阿拉善左旗| 托克逊县| 梅河口市| 凤山市| 班玛县| 荔波县| 丰城市| 和田县| 苏尼特右旗| 房山区| 从化市| 平罗县| 芦溪县| 越西县| 湾仔区| 岱山县| 班玛县| 延庆县| 永宁县| 新闻| 永吉县| 宽城| 长治市| 闸北区| 东乌珠穆沁旗| 开远市| 汶川县| 西畴县| 东源县| 黔江区| 嵊州市| 周宁县| 宜兰县| 华宁县| 湖州市| 正阳县|