查看
1、查看表空间的名称及大小
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、查看表空间物理文件的名称及大小
select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;
3、查看回滚段名称及大小
select segment_name, tablespace_name, r.status,(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent, max_extents, v.curext CurExtent from dba_rollback_segs r, v$rollstat v Where r.segment_id = v.usn(+) order by segment_name ;
4、查看控制文件
select name from v$controlfile;
5、查看日志文件
select member from v$logfile;
6、查看表空间的使用情况
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;
7、查看数据库库对象
select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;
8、查看数据库的版本
Select version FROM Product_component_version Where SUBSTR(PRODUCT,1,6)='Oracle';
9.查看某个表空间内所占空间大于某个值的段(表或索引):
Select segment_name,bytes FROM dba_segments Where bytes>10000000 AND tablespace_name='tablespace_name';
10.查看所有表空间的碎片程度(值在30以下表示碎片很多)
select tablespace_name,sum(bytes),sum(free),sum(free)*100/sum(bytes) from (select b.file_id file_ID, b.tablespace_name tablespace_name, b.bytes Bytes, (b.bytes-sum(nvl(a.bytes,0))) used, sum(nvl(a.bytes,0))
free, sum(nvl(a.bytes,0))/(b.bytes)*100 Percent from dba_free_space a,dba_data_files b where a.file_id=b.file_id group by b.tablespace_name,b.file_id,b.bytes order by b.file_id) group by tablespace_name order by sum(free)*100/sum(bytes);
11.查看自上次数据库启动以来所有数据文件的读写次数
select substr(DF.NAME,1,5) Drive, DF.NAME file_name, (fs.phyblkrd+fs.phyblkwrt) from v$filestat fs,v$datafile df where df.file#=fs.file#;
12.查看数据库中的所有用户:
select * from all_users;
//or
select * from dba_users
创建
1.创建临时表空间
create temporary tablespace mytest
tempfile 'E:/Program/oracle/product/10.1.0/oradata/orcl/mytest.dbf'
size 32m
autoextend on
next 32m maxsize 1024m extent management local;
//必须指定tempfile的参数
2.创建数据表空间
create tablespace mytest_data
logging
datafile'E:/Program/oracle/product/10.1.0/oradata/orcl/mytest_data.dbf'
size 32m
autoextend on
next 32m maxsize 1024m
extent management local;
//必须指定datafile 的参数
3.创建用户并指定表空间
create user testaccount identified by testpwd default tablespace mytest_data temporary tablespace mytest;
4.权限
//授予message 用户dba角色的所以权限
Grant dba to testaccount;
//给用户授予权限
grant connect,resource to testaccount; (db2:指定所有权限)
5.创建一个用户:
CREATE USER username IDENTIFIED BY password OR IDENTIFIED EXETERNALLYOR IDENTIFIED GLOBALLY AS ‘CN=user’[DEFAULT TABLESPACE tablespace][TEMPORARY TABLESPACE temptablespace][QUOTA [integer K[M] ] [UNLIMITED] ] ON tablespace[,QUOTA [integer K[M] ] [UNLIMITED] ] ON tablespace[PROFILES profile_name][PASSWORD EXPIRE][ACCOUNT LOCK or ACCOUNT UNLOCK];
删除
1.删除用户以及用户所有的对象
Drop user zfmi cascade;
//cascade 参数是级联删除该用户所有对象,经常遇到如用户有对象而未加此参数则用户删除不了的问题,所以习惯性加上次参数
2.删除表空间
Drop tablespace zfmi including contents and datafiles cascade onstraints;
// including contents删除表空间内容
// including datafiles 同时删除tablespace中表的外键参照
如果在删除表空间之前删除了表空间对应的数据文件,会造成数据库无法正常启动和关闭
Filename是已经被删除的数据文件,
Tablespace_name是对应的表空间名称
$sqlplus/nolog
Sql>conn/as sysdba;
如果数据库已经启动,先执行以下这行
Sql>shutdown abort
Sql>shutup mount
Sql>alter database datafile ‘filename’ offline drop;
Sql>alter database open;
Sql>drop tabelspace tablespace_name including contents;