oracle 刪除和重建表空間指令碼

2021-09-02 15:59:06 字數 4348 閱讀 4902

除錯資料庫生成指令碼,需要頻繁重建表空間

/**清除原有表空間

重建表空間和使用者

**/declare

tbs varchar2(100):='ts_data'; --表空間名稱

tbs_tpm varchar2(100):='data_temp';--臨時表空間名稱

uname varchar2(100):='user';--使用者名稱 密碼為使用者名稱小寫

file_sp varchar2(100):='/';--檔案分隔附 自動判斷

tbs_exists integer;

filepath varchar2(100);

dyn_sql varchar2(1000);

begin

--check exist

select count(*) into tbs_exists from dba_data_files where tablespace_name=tbs;

dbms_output.put_line(tbs||' exists '|| tbs_exists);

/** **/

--drop old table space

if tbs_exists>0 then

dbms_output.put_line('drop exists old table space '||tbs);

dyn_sql:='drop user '||uname||' cascade';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='drop tablespace '||tbs||' including contents and datafiles cascade constraints ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='drop tablespace '||tbs_tpm||' including contents and datafiles ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

end if;

-- windows 系統的檔案分隔符

if(instr(dbms_utility.port_string,'win')>0) then

file_sp:='\';

end if;

-- init file path

select substr(file_name,0,instr(file_name,file_sp,-1,1)) into filepath from dba_data_files

where rownum=1;

dbms_output.put_line('filepath='||filepath);

--create new table space

dyn_sql:='create tablespace '||tbs||' logging datafile '''||filepath||tbs||'_data.dbf'' size 50m autoextend on next 10m maxsize unlimited ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='create temporary tablespace '||tbs_tpm||' tempfile '''||filepath||tbs_tpm||'.dbf'' size 500m autoextend on next 50m maxsize 2048m extent management local ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

-- user sql

dyn_sql:='create user '||uname||' identified by "'||lower(uname)||'" ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='alter user '||uname||' default tablespace "'||tbs||'" temporary tablespace "'||tbs_tpm||'" account unlock ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='alter user '||uname||' quota unlimited on "'||tbs||'"';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='grant connect,resource,dba to '||uname||' ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='alter user '||uname||' default role "dba"';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

dyn_sql:='grant connect,resource,dba to '||uname||' ';

dbms_output.put_line(dyn_sql);

execute immediate dyn_sql;

end;

ora-01940: 無法刪除當前已連線的使用者

sql>alter user *** account lock;

sql>select * from v$session where username='lgdb';

sql>alter system kill session 'xx,xx'

sql>drop user xx cascade

drop user and table space

drop user ***xcascade;

drop tablespace ts***xn including contents and datafiles cascade constraints ;

drop tablespace ***x_temp including contents and datafiles ;

create tablespace ts_*** logging datafile '/opt/oracle/oradata/orcl_pdm/ts_***x_data.dbf' size 50m autoextend on next 10m maxsize unlimited;

create temporary tablespace ***x_temp tempfile '/opt/oracle/oradata/orcl_pdm/***x_temp.dbf' size 500m autoextend on next 50m maxsize 2048m extent management local;

-- user sql

create user "***x" identified by "***x" ;

alter user "***x"

default tablespace "ts_***x"

temporary tablespace "***x_temp"

account unlock ;

-- quotas

alter user "***x" quota unlimited on ts_***x;

-- system privileges

grant connect,resource,dba to "***x" ;

-- roles

alter user "***x" default role "dba";

-- system privileges

grant connect,resource,dba to "***x" ;

select count(*) from all_tables t where t.owner='***x';

Oracle重建表空間

一為了開啟資料庫,首先想到的是將壞的日誌清除掉,但是 sql alter database clear unarchived logfile group 3 sql alter database clear unarchived logfile group 3 unrecoverable dataf...

重建oracle臨時表空間

1 首先查詢使用者的預設臨時表空間 sqlplus as sysdba select username,temporary tablespace from dba users select name from v tempfile 2 重建新的臨時表空間並進行切換 create temporary ...

oracle刪除使用者和表空間

oracle 使用時間長會有許多user 和tablespace,導致磁碟被佔滿 如果要刪除user和tablespace 來釋放硬碟空間,可以使用如下命令 step1 刪除user drop user 使用者名稱 cascade 說明 僅刪除了user,還沒有刪除對應的tablespace ste...