梳理Oracle不常用但很實用的Sql(表空間,用戶)

1.用戶相關的Sql

1.1 查看當前用戶的信息

1.1.1 查詢當前用戶的詳細信息。

select * from user_users;

1.1.2 查詢當前用戶的角色信息

select * from user_role_privs;

1.1.3 查詢當前用戶角色的權限信息

select * from user_role_privs;

1.2 查詢其他用戶信息

1.2.1 查看所有用戶(system或者sys用戶登錄)

--以system用戶登錄即可
select * from all_users;
--以sys用戶登錄(有DBA權限的用戶)
select * from dba_users;

1.2.1 查詢用戶系統權限

--以system或者sys用戶登錄
select * from dba_sys_privs;
--以正常用戶登錄
select * from user_sys_privs;

1.2.2 查看用戶對象權限

select * from dba_tab_privs;

select * from all_tab_privs;

select * from user_tab_privs;

1.2.3 查看所有角色(以system或者sys權限登錄)

select * from dba_roles;

1.2.4 Oracle查看當前用戶連接(system或者sys權限登錄)

select * from v$Session

1.2.5 增刪用戶(System或者sys用戶)

--普通增加用戶
create user username identified by password;

--爲用戶指定表空間
create user atf_ygj  identified by password
default tablespace  ygj_data
temporary tablespace ygj_temp;

1.2.6 刪除用戶(system或者sys用戶)

drop user username cascade;

1.2.6 爲用戶授權和釋放權限(擁有dba權限的用戶可以)

--授權
grant connect, resource to username;
--撤銷權限:
revoke connect, resource from username;

oracle提供三種標準角色(role):connect/resource和dba.

connect(連接角色):臨時用戶,特指不需要建表的用戶,通常只賦予他們connect role.

resource role(資源角色):提供給用戶另外的權限以創建他們自己的表、序列、過程(procedure)、觸發器(trigger)、索引(index)和簇(cluster)。

dba(數據庫管理員角色):dba role擁有所有的系統權限。包括無限制的空間限額和給其他用戶授予各種權限的能力。

2.表空間相關Sql

2.1 表空間查詢

2.1.1 查詢當前用戶的表空間

select username, default_tablespace, temporary_tablespace from user_users;

2.2.2  查看錶空間的名稱及大小

select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size  
from dba_tablespaces t, dba_data_files d  
where t.tablespace_name = d.tablespace_name  
group by t.tablespace_name; 

2.2.3 查詢當前數據庫中的所有的臨時表空間(system或者更高權限用戶登錄)

select distinct tablespace_name from dba_temp_files;

2.2.4 查看錶空間物理文件的名稱及大小(system或者更高權限用戶登錄)

select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space  
from dba_data_files  order by tablespace_name;

2.2.5 查看錶空間的使用情況(system或者更高權限用戶登錄)

select sum(bytes)/(1024*1024) as free_space,tablespace_name  
from dba_free_space  
group by tablespace_name;  
SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,
(B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"  
FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C  
WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME; 

2.2 表空間創建

2.2.1 創建臨時表空間

create temporary tablespace ygj_temp
tempfile '/opt/oracle10g/oradata/orcl/ygj_temp.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;

2.2.2 創建數據表空間

create tablespace ygj_data
logging
datafile '/opt/oracle10g/oradata/orcl/ygj_data1.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;

2.3表空間刪除(不區分數據表空間和臨時表空間)

--正常刪除
drop tablespace tablespacename;
--刪除包括本地文件
drop tablespace tablespacename including contents and datafiles cascade constraint;

2.4 表空間中表格遷移

2.4.1 查詢需要移動的表所在的表空間

 select tt.table_name,tt.tablespace_name  from user_all_tables tt where tt.tablespace_name like ***

2.4.2 移動表到指定表空間

alter table employees move tablespace newtablespace;

2.4.3 查詢要移動的索引所在的表空間

select ii.index_name,ii.table_name,ii.tablespace_name,ii.temporary from user_indexes ii where index_name like  '***'

2.4.4 移動(重建)索引到指定表空間

alter index EMP_PK rebuild tablespace tablespacename;

2.5 表空間重命名

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章