oracle数据库的常见操作
env命令用于显示系统中已存在的环境变量,以及在定义的环境中执行指令。
[dsg@weizhu1 config]$ env |grep ORA
ORACLE_SID=orcls
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
1、查看数据文件的大小
SQL> select sum(bytes)/1024/1024/1024 from dba_data_files;
2、查看所有表空间及表空间大小:
select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_name;
TABLESPACE_NAME
SUM(BYTES)/1024/1024
------------------------------ --------------------
SYSAUX
600
UNDOTBS1
600
USERS
5
SYSTEM
700
SQL>
3、查看所有表空间对应的数据文件:
SQL> select tablespace_name,file_name from dba_data_files;
TABLESPACE_NAME
------------------------------
FILE_NAME
--------------------------------------------------------------------------------
SYSTEM
/u01/app/oracle/oradata/orcls/system01.dbf
SYSAUX
/u01/app/oracle/oradata/orcls/sysaux01.dbf
UNDOTBS1
/u01/app/oracle/oradata/orcls/undotbs01.dbf
TABLESPACE_NAME
------------------------------
FILE_NAME
--------------------------------------------------------------------------------
USERS
/u01/app/oracle/oradata/orcls/users01.dbf
SQL>
4、修改数据文件大小:
alter database datafile '/u01/app/oracle/oradata/orcls/users01.dbf' RESIZE 30M;
5、所有数据文件及大小
SQL> select name, bytes/1024/1024 as M from v$datafile;
NAME
--------------------------------------------------------------------------------
M
----------
/u01/app/oracle/oradata/orcls/system01.dbf
700
/u01/app/oracle/oradata/orcls/sysaux01.dbf
600
/u01/app/oracle/oradata/orcls/undotbs01.dbf
600
NAME
--------------------------------------------------------------------------------
M
----------
/u01/app/oracle/oradata/orcls/users01.dbf
5
SQL>
6、添加一个数据文件
如果是在已有的表空间中添加数据文件,则使用:
alter tablespace dsgt add datafile '/u01/app/oracle/oradata/orcl/dsgt04.dbf' size 30M;
alter tablespace dsgt add datafile '/u01/app/oracle/oradata/orcls/users04.dbf' size 30M;
如果是新建一个表空间则是:
create tablespace dsgt datafile '/u01/app/oracle/oradata/orcls/dsgt01.dbf' size 30M;
create tablespace dsgt datafile '/u01/app/oracle/oradata/orcl/dsgt01.dbf' size 30M;
create tablespace dsgt2 datafile '/u01/app/oracle/oradata/orcls/dsgt02.dbf' size 30M;
7、查到当前用户表的具体大小
select segment_name,segment_type,bytes/1024 from user_segments;
查看当前用户表的总大小
select sum(bytes)/1024/1024 from user_segments;
8、查询当前用户下所有的表
select table_name from user_tables;
统计当前用户下表的数量
select count(*) from user_tables;
在dba权限下查看
select table_name from dba_tables where owner='HR';
select count(*) from all_tables where owner='HR'; #注意用户名要大些
select table_name from all_tables where owner='HR';
9、修改用户默认表空间
alter user hr default tablespace dsgt;
解释:以上语句就是说给hr用户重新指定表空间为dsgt;
扩展:创建用户的时候指定表空间。
create user username identified by userpassword default tablespace userspace;
10、统计表的行数
select count(*) from table_name;