user_tables/all_tables/dba_tables及Oracle常见SQL语句
<记录下Oracle常见的SQL语句做个学习笔记>
一、user_tables、all_tables、dba_tables关系:
user_tables :可查询当前用户的表;
all_tables :可查询所有用户的表;
dba_tables:可查询包括系统表在内的 所有表。
二、user_users、all_users、dba_users 关系:
user_users :描述当前用户;
all_users :列出数据库中对当前用户可见的所有用户;
dba_users :描述数据库中所有用户。
(系统权限递增)
三、常见SQL操作语句:(sql语句不唯一,仅供参考学习)
1、查看当前用户的缺省表空间:
select username, default_tablespace from user_users
2、查看所有用户的缺省表空间:
select username,default_tablespace from dba_users
3、查看当前用户下所有的表:
select * from user_tables
4、查看数据库中所有的表空间:
select tablespace_name from dba_data_files
或select tablespace_name from dba_tablespaces
5、查看指定用户所拥有的表空间(已经使用了哪些表空间),用户名需大写:
select distinct tablespace_name from dba_tables where owner='HR'
select distinct tablespace_name from dba_indexes where owner='HR'
6、查看指定用户在指定表空间下的表:
select table_name,tablespace_name,owner from dba_tables where tablespace_name='SYSTEM' and owner='SYSTEM' and table_name like 'TDDJ%'
7、查看表空间中数据文件的全路径:
select * from dba_data_files
8、删除表空间:
drop tablespace AHTBSPACETEST including contents and datafiles cascade constraints
9、查询当前索引的状态:
select distinct index_name,table_owner,table_name,status from user_indexes
10、创建用户四步曲:(其中文件位置、文件大小、自增量、命名之类可按需自定义)
(1) 创建临时表空间:
create temporary tablespace sean_temp tempfile 'C:\APP\ADMINISTRATOR\ORADATA\ORCL\SEAN_TEMP01.DBF'size 10m autoextend on next 10m maxsize 100m extent management local
(2) 创建数据表空间:
create tablespace sean_data logging datafile 'C:\APP\ADMINISTRATOR\ORADATA\ORCL\SEAN_DATA01.DBF' size 10m autoextend on next 10m maxsize 100m extent management local
(3) 创建用户并指定表空间:
create user sean identified by sean default tablespace sean_data temporary tablespace sean_temp
(4) 给用户授予权限:
grant connect,resource,dba to sean
11、给用户赋予DBA权限:
grant dba to sean
12、撤销用户对所有表空间都可使用的权限(相当于撤销用户在表空间上的DBA权限):
revoke unlimited tablespace from sean
13、将用户在SYSTEM表空间的配额置为0:
alter user sean quota 0 on SYSTEM
(有时为了测试需要临时给SYSTEM添加配额时只需把0改为其他配额即可,如改为10m或10都行。如果提示没有多余配额,可能是由于上面第12步所致,可再次执行第11步给它赋予权限后再配额)
14、设置用户在SEAN_DATA表空间上配额不受限(即第10步中创建的用户sean的默认表空间):
alter user sean quota unlimited on SEAN_DATA;
15、修改表空间:
alter table TD_USERS move tablespace TABLESPACE_A // 第一步:将表TD_USERS移至表空间tablesapce_A下
alter index TD_USERS_ID rebuild tablespace TABLESPACE_A // 第二步:修改该表的索引的表空间
(也可以利用Oracle SQL Developer,在表名上右键Edit,找到Storage Options进行修改,然后再对索引进行Rebuild,可参看下图:)