1、查看表空间的使用状况。
SELECT   upper(f.tablespace_name)   表空间名,   
               d.Tot_grootte_Mb   "表空间大小(M)",   
               d.Tot_grootte_Mb   -   f.total_bytes   "已使用空间(M)",   
               round((d.Tot_grootte_Mb   -   f.total_bytes)   /   d.Tot_grootte_Mb   *   100,2)   "使用比",   
                f.total_bytes   "空闲空间(M)",   
                f.max_bytes   "最大块(M)"   
    FROM               
          (SELECT   tablespace_name,   
                          round(SUM(bytes)/(1024*1024),2)   total_bytes,   
                          round(MAX(bytes)/(1024*1024),2)   max_bytes   
              FROM   sys.dba_free_space   
            GROUP   BY   tablespace_name)   f,   
          (SELECT   dd.tablespace_name,   round(SUM(dd.bytes)/(1024*1024),2)   Tot_grootte_Mb   
              FROM       sys.dba_data_files   dd   
              GROUP   BY   dd.tablespace_name)   d   
  WHERE   d.tablespace_name   =   f.tablespace_name           
  ORDER   BY   4   DESC;
---------------------------------------------------------------------------
SELECT   segment_name,segment_type,owner,a.tablespace_name "tablespacename",   
initial_extent/1024   "inital_extent(K)",next_extent/1024   "next_extent(K)",   
pct_increase,b.bytes/1024   "tablespace   max   free space(K)",   
b.sum_bytes/1024 "tablespace total free space(K)" FROM   dba_segments a,   
(SELECT   tablespace_name,MAX(bytes)  bytes,SUM(bytes) sum_bytes FROM dba_free_space GROUP BY tablespace_name)b   
WHERE   a.tablespace_name=b.tablespace_name  AND   next_extent>b.bytes  ORDER BY 4,3,1;
------------------------------------------------------------
用户如何有效地利用数据字典
    ORACLE的数据字典是数据库的重要组成部分之一,它随着数据库的产生而产生,
随着数据库的变化而变化,
体现为sys用户下的一些表和视图。数据字典名称是大写的英文字符。
     数据字典里存有用户信息、用户的权限信息、所有数据对象信息、表的约束条件、
统计分析数据库的视图等。
我们不能手工修改数据字典里的信息。
很多时候,一般的ORACLE用户不知道如何有效地利用它。
dictionary   全部数据字典表的名称和解释,它有一个同义词dict
     dict_column   全部数据字典表里字段名称和解释
     如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句:
     select * from dictionary where instr(comments,'index')>0;
     如果我们想知道user_indexes表各字段名称的详细含义,可以用下面这条SQL语句:
     select column_name,comments from dict_columns where table_name='USER_INDEXES';
     依此类推,就可以轻松知道数据字典的详细名称和解释,不用查看ORACLE的其它文档资料了。
     下面按类别列出一些ORACLE用户常用数据字典的查询使用方法。
     1、用户
            查看当前用户的缺省表空间
            select username,default_tablespace from user_users;
            查看当前用户的角色
            select * from user_role_privs;
            查看当前用户的系统权限和表级权限
            select * from user_sys_privs;
            select * from user_tab_privs;
     2、表
             查看用户下所有的表
             select * from user_tables;
             查看名称包含log字符的表
             select object_name,object_id from user_objects
                 where instr(object_name,'LOG')>0;
             查看某表的创建时间
             select object_name,created from user_objects where object_name=upper('&table_name');
             查看某表的大小
             select sum(bytes)/(1024*1024) as "size(M)" from user_segments
                 where segment_name=upper('&table_name');
             查看放在ORACLE的内存区里的表
             select table_name,cache from user_tables where instr(cache,'Y')>0;
     3、索引
             查看索引个数和类别
             select index_name,index_type,table_name from user_indexes order by table_name;
             查看索引被索引的字段
             select * from user_ind_columns where index_name=upper('&index_name');
             查看索引的大小
             select sum(bytes)/(1024*1024) as "size(M)" from user_segments
                 where segment_name=upper('&index_name');
     4、序列号
             查看序列号,last_number是当前值
             select * from user_sequences;
     5、视图
             查看视图的名称
             select view_name from user_views;
             查看创建视图的select语句
             set view_name,text_length from user_views;
             set long 2000;      说明:可以根据视图的text_length值设定set long 的大小
             select text from user_views where view_name=upper('&view_name');
     6、同义词
             查看同义词的名称
             select * from user_synonyms;
     7、约束条件
             查看某表的约束条件
             select constraint_name, constraint_type,search_condition, r_constraint_name
                 from user_constraints where table_name = upper('&table_name');
         select c.constraint_name,c.constraint_type,cc.column_name
             from user_constraints c,user_cons_columns cc
             where c.owner = upper('&table_owner') and c.table_name = upper('&table_name')
             and c.owner = cc.owner and c.constraint_name = cc.constraint_name
             order by cc.position;
     8、存储函数和过程
             查看函数和过程的状态
             select object_name,status from user_objects where object_type='FUNCTION';
             select object_name,status from user_objects where object_type='PROCEDURE';
             查看函数和过程的源代码
             select text from all_source where owner=user and name=upper('&plsql_name');

三、查看数据库的SQL
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 Created, Log_Mode, Log_Mode From V$Database;
四、ORACLE用户连接的管理
用系统管理员,查看当前数据库有几个用户连接:
select username,sid,serial# from v$session;
如果要停某个连接用
alter system kill session 'sid,serial#';
如果这命令不行,找它UNIX的进程数
select pro.spid from v$session ses,v$process pro where ses.sid=21 and ses.paddr=pro.addr;
说明:21是某个连接的sid数
然后用 kill 命令杀此进程号。

五、SQL*PLUS使用
a、近入SQL*Plus
$sqlplus 用户名/密码
    退出SQL*Plus
exit
b、在sqlplus下得到帮助信息
列出全部SQL命令和SQL*Plus命令
help
列出某个特定的命令的信息
help 命令名
c、显示表结构命令DESCRIBE
DESC 表名
d、SQL*Plus中的编辑命令
显示SQL缓冲区命令
L
修改SQL命令
首先要将待改正行变为当前行
n
用CHANGE命令修改内容
c/旧/新
重新确认是否已正确
L
使用INPUT命令可以在SQL缓冲区中增加一行或多行
i
输入内容
e、调用外部系统编辑器
edit 文件名
可以使用DEFINE命令设置系统变量EDITOR来改变文本编辑器的类型,在login.sql文件中定义如下一行
DEFINE_EDITOR=vi
f、运行命令文件
START test
@test
常用SQL*Plus语句
a、表的创建、修改、删除
创建表的命令格式如下:
create table 表名 (列说明列表);
为基表增加新列命令如下:
ALTER TABLE 表名 ADD (列说明列表)
例:为test表增加一列Age,用来存放年龄
     alter table test
         add (Age number(3));
修改基表列定义命令如下:
ALTER TABLE 表名
MODIFY (列名 数据类型)
例:将test表中的Count列宽度加长为10个字符
     alter atble test
         modify (County char(10));
b、将一张表删除语句的格式如下:
DORP TABLE 表名;
例:表删除将同时删除表的数据和表的定义
drop table test
c、表空间的创建、删除

六、ORACLE逻辑备份的SH文件
完全备份的SH文件:exp_comp.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=complete file=/oracle/export/db_comp$rq.dmp"
累计备份的SH文件:exp_cumu.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=cumulative file=/oracle/export/db_cumu$rq.dmp"
增量备份的SH文件: exp_incr.sh
rq=` date +"%m%d" `
su - oracle -c "exp system/manager full=y inctype=incremental file=/oracle/export/db_incr$rq.dmp"
root用户crontab文件
/var/spool/cron/crontabs/root增加以下内容
0 2 1 * * /oracle/exp_comp.sh
30 2 * * 0-5 /oracle/exp_incr.sh
45 2 * * 6 /oracle/exp_cumu.sh
当然这个时间表可以根据不同的需求来改变的,这只是一个例子。