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;