oracle常用命令

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:2415

[postbg]bg9.png[/postbg][font=Georgia]一、Oracle数据库实例、用户、目录及session会话查看:
[font=Georgia]1、ORACLE SID查看设置
[font=Georgia]查看SID、用户名
[font=Georgia]$ env|grep SID 、select * from v$instance、select instance_name,host_name from v$instance;
查看数据库所有用户及用户状态:
SQL> select usernames,account_status from dba_users;
[font=Georgia]设置SID
[font=Georgia]$ export ORACLE_SID=hisvhfs
[font=Georgia]查看数据库DBID:
SQL>select * from v$DBID
2、查询、设置Oracle数据库实例最大进程数及最大会话数
查看系统最大session:
SQL>show parameter session
查看当前连接数:
SQL>select count(*) from v$bgprocess
查看系统最大进程数:
SQL>show parameter process
查看当前连接到数据库的用户:
SQL>select count(*) from v$session
查看当前数据库建立的会话情况:
SQL> select sid,serial#,username,program,machine,status from v$session;
查询应用的连接数SQL:
SQL> SELECT b.MACHINE, b.PROGRAM, COUNT (*)
FROM v$process a, v$session b
WHERE a.ADDR = b.PADDR AND b.USERNAME IS NOT NULL
GROUP BY b.MACHINE, b.PROGRAM
ORDER BY COUNT (*) DESC;
查看当前数据库的并发连接数
SQL> select count(*) from v$session where status='ACTIVE';
查看当前有哪些用户正在使用数据:
select osuser,a.username,cpu_time/executions/1000000||'s',sql_fulltext,machine
from v$session a,v$sqlarea b
where a.sql_address = b.address
order by cpu_time/executions desc;
查看数据库指定用户的连接情况
SQL> select sid,serial# from v$session where username='XX'; XX为用户
例如:
SQL> select sid,serial# from v$session where username='BSPDEV';
SID SERIAL#
---------- ----------
204 4609
399 5841
590 6041
清除用户下连接进程
SQL> alter system kill session '204,4609';
SQL> alter system kill session '399,5841';
SQL> alter system kill session '590,6041';

修改processes和sessions值
  SQL> alter system set processes=300 scope=spfile;
  系统已更改。
  SQL> alter system set sessions=335 scope=spfile;
  系统已更改。
修改processes和sessions值必须重启oracle服务器才能生效
ORACLE的连接数(sessions)与其参数文件中的进程数(process)有关,它们的关系如下:
  sessions=(1.1*process+5)
[font=Georgia]3、查看数据库目录
[font=Georgia]SQL> select * from all_directories;
[font=Georgia]4、查看数据库现有模式、是否归档
[font=Georgia]SQl>select name,log_mode from v$database;
[font=Georgia]也可以用下面的语句
archive log list;(该方法需要as sysdba)
[font=Georgia]查看数据库的创建日期和归档方式
SQL> Select Created, Log_Mode, Log_Mode From V$Database;
[font=Georgia]5、配置用户密码过期时间
[font=Georgia]alter profile "default" limit password_life_time unlimited; 配置用户密码永不过期
alter profile "default" limit password_life_time 100; 配置用户密码100天过期
6、创建、配置新用户及查看用户属性
解锁新用户:
SQL> alter user scott account unlock;
SQL> alter user scott identified by tiger;
[font=Georgia]删除oracle用户:
SQL>drop user username cascade; (删除与用户相关的所有对象)
[font=Georgia]这样该用户包括该用户下的表,试图,同义词,过程,索引,及相关的一切就删除了。

[font=Georgia]创建用户并赋权限以及设置默认表空间。
[font=Georgia]以sysdba用户登陆进行以下设置:
[font=Georgia]-- Create the user
create user VHFSM
identified by vhnj1fsm
[font=Georgia]default tablespace MGRVHFSTBSDEF 此处是设置默认表空间。
temporary tablespace TEMP
profile DEFAULT
[font=Georgia]quota unlimited on mgrvhfstbs2010此处是设置可操作的其他表空间
quota unlimited on mgrvhfstbsdef;
-- Grant/Revoke role privileges
grant connect to VHFSM;
grant dba to VHFSM;
-- Grant/Revoke system privileges
grant unlimited tablespace to VHFSM;
[font=Georgia]查看用户及角色权限
[font=Georgia]--1.查看所有用户:
select * from dba_users;
select * from all_users;
select * from user_users;

--2.查看用户或角色系统权限(直接赋值给用户或角色的系统权限):
select * from dba_sys_privs;
select * from user_sys_privs;

--3.查看角色(只能查看登陆用户拥有的角色)所包含的权限

sql>select * from role_sys_privs;

--4.查看用户对象权限:
select * from dba_tab_privs;
select * from all_tab_privs;
select * from user_tab_privs;

--5.查看所有角色:
select * from dba_roles;

--6.查看用户或角色所拥有的角色:
select * from dba_role_privs;
select * from user_role_privs;

--7.查看哪些用户有sysdba或sysoper系统权限(查询时需要相应权限)

select * from V$PWFILE_USERS

--注意:
--1、以下语句可以查看Oracle提供的系统权限

select name from sys.system_privilege_map

--2、查看一个用户的所有系统权限(包含角色的系统权限)

SELECT privilege
FROM dba_sys_privs
WHERE grantee = 'DATAUSER'
UNION
SELECT privilege
FROM dba_sys_privs
WHERE grantee IN (SELECT granted_role FROM dba_role_privs WHERE grantee = 'DATAUSER');

[font=Georgia]二、创建、管理Oracle表空间
[font=Georgia]1、先查询空闲空间
[font=Georgia]select tablespace_name,file_id,block_id,bytes,blocks from dba_free_space;
2、增加Oracle表空间
[font=Georgia]先查询数据文件名称、大小和路径的信息,语句如下:
[font=Georgia]select tablespace_name,file_id,bytes,file_name from dba_data_files;
3、修改文件大小语句如下
[font=Georgia]alter database datafile
'需要增加的数据文件路径,即上面查询出来的路径
'resize 800M;

[font=Georgia]4、创建Oracle表空间
[font=Georgia]create tablespace test
datafile '/home/app/oracle/oradata/oracle8i/test01.dbf' size 8M
autoextend on
next 5M
maxsize 10M;

create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize unlimited
maxsize unlimited 是大小不受限制

create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
extent management local uniform;
unform表示区的大小相同,默认为1M

create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
extent management local uniform size 500K;
unform size 500K表示区的大小相同,为500K

create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
extent management local autoallocate;
autoallocate表示区的大小由随表的大小自动动态改变,大表使用大区小表使用小区

create tablespace sales
datafile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
temporary;
temporary创建字典管理临时表空间 ,要创建本地管理临时表空间要加temporary tablespace关键字

create temporary tablespace sales
tempfile '/home/app/oracle/oradata/oracle8i/sales01.dbf' size 800M
autoextend on
next 50M
maxsize 1000M
创建本地管理临时表空间,如果是临时表空间,所有语句中的datafile都换为tempfile ,所有语句中的datafile都换为tempfile8i[font=Georgia]

为表空间增加数据文件:
alter tablespace sales add
datafile '/home/app/oracle/oradata/oracle8i/sales02.dbf' size 800M
autoextend on next 50M
maxsize 1000M;

[font=Georgia]5、查看表空间是否自动扩展
SQL> select file_id,file_name,tablespace_name,autoextensible,increment_by from dba_data_files order by file_id desc;
[font=Georgia]更改自动扩展属性:
[font=Georgia]alter database datafile
'/home/app/oracle/oradata/oracle8i/sales01.dbf',
'/home/app/oracle/oradata/oracle8i/sales02.dbf'
'/home/app/oracle/oradata/oracle8i/sales01.dbf
autoextend off;
[font=Georgia]6、表空间的查看与修改
[font=Georgia]查看用户默认表空间
[font=Georgia]SQL> select username,default_tablespace from dba_users;查看所有用户的默认表空间
[font=Georgia]SQL> select username,default_tablespace from user_users;查看某个用户的默认表空间,前提需要connect该用户。
SQL> select USERNAME,DEFAULT_TABLESPACE,TEMPORARY_TABLESPACE from dba_userswhere username = 'hr'; 查看用户对应的默认表空间

[font=Georgia]修改用户默认表空间
SQL> alter user zhanghr default tablespace test;
[font=Georgia]设置数据库的默认临时表空间:
SQL>Alter database default temporary tablespace temp_tbs_name;
[font=Georgia]查看用户和默认表空间的关系:
SQL>select username,default_tablespace from dba_users;
[font=Georgia]查看临时表空间:
[font=Georgia]SQL> select file_name,file_id,blocks,user_blocks from dba_temp_files;
[font=Georgia]SQL> select tablespace_name,current_users,total_blocks,used_blocks,free_blocks from v$sort_segment;
[font=Georgia]查看undo表空间
[font=Georgia]SQL> show parameter undo;
[font=Georgia]NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
[font=Georgia]查看undo表空间大小
[font=Georgia]SQL> select sum(bytes)/1024/1024 "current undo size(M)" from dba_data_files where tablespace_name='UNDOTBS1';
[font=Georgia]通过增加数据文件来改变undo表空间大小
[font=Georgia]SQL> alter tablespace undotbs1
add datafile '/oracle/oradata/orc6/undo02.dbf' size 10M;
[font=Georgia]通过resize更改数据文件大小
[font=Georgia]SQL> alter database datafile '/oracle/oradata/orc6/undo02.dbf' resize 100M;
[font=Georgia]查看某个表空间的数据文件
[font=Georgia]SQL> select file_name,tablespace_name,bytes/1024/1024 "bytes MB",maxbytes/1024/1024 "maxbytes MB" from dba_data_files where tablespace_name='ORA1TBS';指定表空间名要大写
[font=Georgia]查看所有http://zhidao.baidu.com/search?word=%E8%A1%A8%E7%A9%BA%E9%97%B4&fr=qb_search_exp&ie=gbk][font=Georgia]表空间[font=Georgia]大小

SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_files
group by tablespace_name;

[font=Georgia]查看已使用的http://zhidao.baidu.com/search?word=%E8%A1%A8%E7%A9%BA%E9%97%B4&fr=qb_search_exp&ie=gbk][font=Georgia]表空间[font=Georgia]大小
SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_free_space
group by tablespace_name;
[font=Georgia]oracle查看表空间大小及使用率: [font=Georgia]方法一:
[font=Georgia]SELECT UPPER(F.TABLESPACE_NAME),D.TOT_GROOTTE_MB,D.TOT_GROOTTE_MB,F.TOTAL_BYTES,TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100, 2),'990.99'), F.TOTAL_BYTES, F.MAX_BYTES 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;
[font=Georgia]方法二:
[font=Georgia]SELECT D.TABLESPACE_NAME,
SPACE || 'M' "SUM_SPACE(M)",
BLOCKS "SUM_BLOCKS",
SPACE - NVL (FREE_SPACE, 0) || 'M' "USED_SPACE(M)",
ROUND ( (1 - NVL (FREE_SPACE, 0) / SPACE) * 100, 2) || '%'
"USED_RATE(%)",
FREE_SPACE || 'M' "FREE_SPACE(M)"
FROM ( SELECT TABLESPACE_NAME,
ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,
SUM (BLOCKS) BLOCKS
FROM DBA_DATA_FILES
GROUP BY TABLESPACE_NAME) D,
( SELECT TABLESPACE_NAME,
ROUND (SUM (BYTES) / (1024 * 1024), 2) FREE_SPACE
FROM DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
UNION ALL
SELECT D.TABLESPACE_NAME,
SPACE || 'M' "SUM_SPACE(M)",
BLOCKS SUM_BLOCKS,
USED_SPACE || 'M' "USED_SPACE(M)",
ROUND (NVL (USED_SPACE, 0) / SPACE * 100, 2) || '%' "USED_RATE(%)",
NVL (FREE_SPACE, 0) || 'M' "FREE_SPACE(M)"
FROM ( SELECT TABLESPACE_NAME,
ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,
SUM (BLOCKS) BLOCKS
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) D,
( SELECT TABLESPACE_NAME,
ROUND (SUM (BYTES_USED) / (1024 * 1024), 2) USED_SPACE,
ROUND (SUM (BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE
FROM V$TEMP_SPACE_HEADER
GROUP BY TABLESPACE_NAME) F
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)
ORDER BY 1;
[font=Georgia]方法三:
[font=Georgia]SELECT *
FROM (SELECT a.tablespace_name,
to_char(a.bytes / 1024 / 1024, '99,999.999') total_bytes,
to_char(b.bytes / 1024 / 1024, '99,999.999') free_bytes,
to_char(a.bytes / 1024 / 1024 - b.bytes / 1024 / 1024,
'99,999.999') use_bytes,
to_char((1 - b.bytes / a.bytes) * 100, '99.99') || '%' USE
FROM (SELECT tablespace_name, SUM(bytes) bytes
FROM dba_data_files
GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM(bytes) bytes
FROM dba_free_space
GROUP BY tablespace_name) b
WHERE a.tablespace_name = b.tablespace_name
UNION ALL
SELECT c.tablespace_name,
to_char(c.bytes / 1024 / 1024, '99,999.999') total_bytes,
to_char((c.bytes - d.bytes_used) / 1024 / 1024, '99,999.999') free_bytes,
to_char(d.bytes_used / 1024 / 1024, '99,999.999') use_bytes,
to_char(d.bytes_used * 100 / c.bytes, '99.99') || '%' USE
FROM (SELECT tablespace_name, SUM(bytes) bytes
FROM dba_temp_files
GROUP BY tablespace_name) c,
(SELECT tablespace_name, SUM(bytes_cached) bytes_used
FROM v$temp_extent_pool
GROUP BY tablespace_name) d
WHERE c.tablespace_name = d.tablespace_name)
ORDER BY tablespace_name;
[font=Georgia]查看表空间使用大小情况一
[font=Georgia]SQL> select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/1024 "used MB",
b.bytes/1024/1024 "free MB",round(((a.bytes-b.bytes)/a.bytes)*100,2) "percent_used"
from
(select tablespace_name,sum(bytes) bytes from dba_data_files group by tablespace_name) a,
(select tablespace_name,sum(bytes) bytes,max(bytes) largest from dba_free_space group by tablespace_name ) b
where a.tablespace_name=b.tablespace_name
order by ((a.bytes-b.bytes)/a.bytes) desc
;
[font=Georgia]TABLESPACE_NAME Sum MB used MB free MB percent_used
------------------------------ ---------- ---------- ---------- ------------
SYSAUX 490 461.0625 28.9375 94.09
UNDOTBS1 75 66.8125 8.1875 89.08
USERS 5 1.3125 3.6875 26.25
ORA1TBS 400 2 398 .5
ORA2TBS 400 2 398 .5
[font=Georgia]查看表空间使用大小情况二[font=Georgia]

SQL> select a.tablespace_name,total,free,total-free used from
2 ( select tablespace_name,sum(bytes)/1024/1024 total from dba_data_files
group by tablespace_name) a,
( select tablespace_name,sum(bytes)/1024/1024 free from dba_free_space
group by tablespace_name) b
where a.tablespace_name=b.tablespace_name; 3 4 5 6
[font=Georgia]TABLESPACE_NAME TOTAL FREE USED
------------------------------ ---------- ---------- ----------
USERS 5 .9375 4.0625
TESTTB 500 499 1
SYSAUX 550 30 520
EXAMPLE 100 20.75 79.25
UNDOTBS1 110 96.625 13.375

[font=Georgia]在SQL命令行情况下将结果输出到指定文件中。
SQL> spool out.txt
SQL> select * from v$database;
SQL> spool off
[font=Georgia][size=14px]三、查看、管理ORACLE数据文件
[font=Georgia]查看数据文件:
SQL> select name from v$datafile;
[font=Georgia]更改数据文件大小:
[font=Georgia]SQL> alter database datafile '/oradata2/hisvhfs/undotbs01.dbf' resize 30G;
[font=Georgia]查看用户所有表空间及对应的数据文件和数据文件大小
[font=Georgia]SQL> select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;
[font=Georgia]查看数据文件状态及大小
[font=Georgia]SQL> select name,BYTES,STATUS ,FILE# from v$datafile;
[font=Georgia]查看所有数据文件
[font=Georgia]SQL> select name from v$datafile;
[font=Georgia]查看所有表空间对应的数据文件
SQL> select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;
[font=Georgia][size=14px]四、查看、管理ORACLE表
[font=Georgia]查看所有segment的大小。
Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name
[font=Georgia]查询表的大小和表空间的大小
[font=Georgia]有两种含义的表大小。一种是分配给一个表的物理空间数量,而不管空间是否被使用。可以这样查询获得字节数:
当前实例以scott用户登录,并创建dept表的副本dept_copy2为例。
SQL> select segment_name, bytes
2 from user_segments
3 where segment_type = 'TABLE';

SEGMENT_NAME BYTES
-------------------------------------------------------------------------------- ----------
DEPT_COPY2 65536
DEPT_COPY 65536
BIN$7Sa/taXJEKHgQ2kFqMCxMQ==$0 65536
ITEMS 65536
SALGRADE 65536
EMP 65536
DEPT 65536

7 rows selected
[font=Georgia]或者
SQL> Select Segment_Name,Sum(bytes)/1024/1024 From User_Extents Group By Segment_Name;

SEGMENT_NAME SUM(BYTES)/1024/1024
-------------------------------------------------------------------------------- --------------------
DEPT 0.0625
PK_DEPT 0.0625
EMP 0.0625
DEPT_COPY 0.0625
DEPT_COPY2 0.0625
ITEMS 0.0625
PK_EMP 0.0625
SALGRADE 0.0625

8 rows selected
[font=Georgia]另一种表实际使用的空间。这样查询:
SQL> select table_name from user_tables;

TABLE_NAME
------------------------------
DEPT
EMP
BONUS
SALGRADEanalyze table emp compute statistics;
ITEMS
DEPT_COPY
DEPT_COPY2
[font=Georgia]SQL> analyze table DEPT_COPY2 compute statistics;
[font=Georgia]查看某个表属于哪个表空间:
SQL> select tablespace_name from all_tables where table_name='SYS_EXPORT_FULL_01';
[font=Georgia]创建表时指定表空间:
SQL>create table a (name varchar(10)) tablespace test;
[font=Georgia]查看数据库表结构:
[font=Georgia]SQL> desc dept_copy2;
Name Type Nullable Default Comments
------ ------------ -------- ------- --------
DEPTNO NUMBER(2) Y
DNAME VARCHAR2(14) Y
LOC VARCHAR2(13) Y
[font=Georgia]修改表的列名:
SQL> alter table users rename column ID to PID;
[font=Georgia]修改表的列的字符大小
[font=Georgia]SQL> alter table student modify class varchar2(10);
[font=Georgia]创建主键:
SQL> alter table users add constraint pk_users primary key(name);
[font=Georgia]修改表的列数据类型:
SQL> select * from users;
[font=Georgia]NAME AGE PID
---------- ---------- ----------
赵霞 3.2012E+18
李莉 3.2012E+18
[font=Georgia]SQL> alter table users add(newColumn varchar2(20));
SQL> update users set newColumn = PID;
SQL> commit;
SQL> alter table users drop column PID cascade constraints;
SQL> alter table users rename column newColumn to PID;
SQL> commit;
[font=Georgia]SQL> desc users;
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
NAME NOT NULL VARCHAR2(10)
AGE NUMBER
PID VARCHAR2(20)
[font=Georgia]SQL> select * from users;
[font=Georgia]NAME AGE PID
---------- ---------- --------------------
赵霞 3201231988001010101
李莉 3201231988001010102
[font=Georgia]创建序列:
SQL> create sequence seq_PID
start with 3201231988001010101
maxvalue 3201231999999999999
minvalue 3201231988001010101
nocycle
cache 100;
[font=Georgia]创建触发器:
SQL> create or replace trigger trigger_users
before insert on users
for each row
begin
select seq_PID.nextval into :new.PID from dual;
end;
/
[font=Georgia]查看用户序列
[font=Georgia]SQL> select sequence_name from USER_SEQUENCES;
[font=Georgia]SEQUENCE_NAME
------------------------------
SEQ_ID

[font=Georgia]查看用户触发器
[font=Georgia]SQL> select * from user_triggers where table_name = upper('job');
[font=Georgia]通过group by 语句使用rollup运算符汇总ID列
[font=Georgia]SQL> select ID,avg(age),sum(age),max(age),count(*)
2 from job group by rollup(ID);
[font=Georgia]ID AVG(AGE) SUM(AGE) MAX(AGE) COUNT(*)
---------- ---------- ---------- ---------- ----------
1583 25 25 25 1
1584 24 24 24 1
1585 25 25 25 1
1586 25 25 25 1
1587 23 23 23 1
1683 22 22 22 1
1684 24 24 24 1
1685 25 25 25 1
1686 24 24 24 1
1687 22 22 22 1
1688 28 28 28 1
[font=Georgia]ID AVG(AGE) SUM(AGE) MAX(AGE) COUNT(*)
---------- ---------- ---------- ---------- ----------
1689 26 26 26 1
1690 26 26 26 1
1783 35 35 35 1
1784 1
1883 24 24 24 1
1884 1
25.2 378 35 17
[font=Georgia]having子句与group by 子句一般一起使用,可以在汇总相关数据后进一步筛选汇总的数据。
[font=Georgia]SQL> select ID,avg(age),sum(age),max(age),count(*)
2 from job group by id having avg(age)>25;
[font=Georgia]ID AVG(AGE) SUM(AGE) MAX(AGE) COUNT(*)
---------- ---------- ---------- ---------- ----------
1690 26 26 26 1
1783 35 35 35 1
1689 26 26 26 1
1688 28 28 28 1
[font=Georgia]多表连接查询
[font=Georgia]SQL> select job.name,job.id,job.job,job.age,users.name,users.age from job,users
2 where job.name=users.name;
[font=Georgia]NAME ID JOB AGE NAME AGE
-------- ---------- ---------- ---------- ---------- ----------
陈灵灵 1783 KTV服务员 35 陈灵灵 38
李莉 1587 IT程序员 23 李莉
赵霞 1683 空姐 22 赵霞
[font=Georgia]desc 在oracle数据库里有两种用法,一是查询表的结构 格式:desc table_name
二是在用排序时(order by)是降序的关键字 格式 :order by column_name desc;
[font=Georgia]查看表的segment大小,即表实际占用的物理大小,无论是否使用。
[font=Georgia]SQL> select segment_name,partition_name,bytes,blocks,extents from user_segments where segment_name='JOBS'; (segment名,即表明)
SQL> select segment_name,partition_name,count(extent_id),sum(bytes),sum(blocks) from user_extents where segment_name='JOBS' group by segment_name,partition_name;
[font=Georgia][size=14px]五、查看、管理ORACLE常用的参数、配置等
[font=Georgia]查看控制文件:
SQL> select name from v$controlfile;
[font=Georgia]查看重做日志文件,简称日志文件:
SQL> select * from v$logfile;
[font=Georgia]查看表的索引:
SQL>select index_name,index_type,table_name from user_indexes where table_name='表名';
[font=Georgia]修改oracle时间格式:
SQL> alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS';
[font=Georgia]指定开启某个监听:
SQL>lsnrctl start orc5_lisenter(此处是当初建监听时创建的监听名)
[font=Georgia]数据库SCN及时间查询。
[font=Georgia]SQL> select sysdate from dual; 查看数据库时间
[font=Georgia]SYSDATE
-----------------
20131216 23:52:55
[font=Georgia]SQL> select dbms_flashback.get_system_change_number from dual; 查看当前数据库的SCN号
[font=Georgia]GET_SYSTEM_CHANGE_NUMBER
------------------------
1583042
[font=Georgia]SQL> select to_char(scn_to_timestamp(1583042),'yyyy-mm-dd hh24:mi:ss') from dual; 根据数据库的SCN号查找对应的数据库时间
[font=Georgia]TO_CHAR(SCN_TO_TIME
-------------------
2013-12-16 23:45:38
[font=Georgia]SQL> select timestamp_to_scn(to_date('2013-12-16 15:30:19','yyyy-mm-dd hh24:mi:ss')) from dual; 根据数据库的时间查找对应的数据库SCN号
[font=Georgia]TIMESTAMP_TO_SCN(TO_DATE('2013-12-1615:30:19','YYYY-MM-DDHH24:MI:SS'))
----------------------------------------------------------------------
1519388
[font=Georgia]SQL> select * from (select time_dp,scn from smon_scn_time order by time_dp desc)where rownum<10; 条件查找,查找rownum小于10的SCN以对应的时间。 [font=Georgia]TIME_DP SCN ----------------- ---------- 20131216 15:53:02 1584574 20131216 15:46:45 1583098 20131216 15:45:20 1583022 20131216 15:39:08 1582399 20131216 15:37:01 1581998 20131216 15:31:08 1579915 20131216 14:46:02 1577210 20131216 14:40:16 1576196 20131216 14:35:12 1575597 [font=Georgia]9 rows selected. [font=Georgia][size=14px]数据库游标设置 [font=Georgia]SQL> show parameter open_cursors; 查看游标参数
SQL> select count(*) from v$open_cursor; 查看打开的游标
SQL> select name,value from v$sysstat where name ='opened cursors current';查看当前打开的游标
SQL> alter system set open_cursors=3000 scope=both; 修改默认游标数
[font=Georgia][size=14px]六、查看、配置ORACLE重做日志
[font=Georgia]创建日志文件组:
SQL> alter database add logfile group 4
(
'/oracle/oradata/orc6/redo04.log'
)
size 10M;
[font=Georgia]查看日志文件组
[font=Georgia]SQL> select group#,status,member from v$logfile;
[font=Georgia]为日志文件组增加日志文件:
[font=Georgia]SQL> alter database add logfile member '/oracle/oradata/orc6/redo05.log' to group 3;
[font=Georgia]查看日志文件大小[font=Georgia]SQL> select GROUP#,BYTES,BLOCKSIZE,MEMBERS from v$log;

[font=Georgia][size=14px]七、desc的常用查询

[font=Georgia]SQL> desc dba_temp_files; 查询临时表空间[font=Georgia]SQL> desc v$database; 查看数据库
[font=Georgia]SQL> desc dba_data_files; 查看数据文件
[font=Georgia]SQL> desc user_segments; 查看oracle segment(段)
[font=Georgia]SQL> desc dba_segments; 查看ORACLE segment
[font=Georgia]SQL> desc dba_tables; 查看表
SQL> desc dba_objects 查看对象
SQL> desc dba_users; 查看用户
SQL> desc dba_tablespaces; 查看表空间
[font=Georgia]SQL> desc user_segments; 查看数据段
[font=Georgia]SQL> desc dba_jobs; 查看job
[font=Georgia]SQL> desc dba_role_privs; 查看角色权限
[font=Georgia]SQL> desc dba_constraints 查看约束
[font=Georgia]SQL> desc dba_cons_columns 查看列约束
[font=Georgia]SQL> show parameter log_archive_dest; 查看archive log所在位置
[font=Georgia]SQL> archive log list; 查看归档目录以及log sequence
[font=Georgia]SQL> select * from V$FLASH_RECOVERY_AREA_USAGE; 查看flash_recovery_area 使用情况。
[font=Georgia]SQL> select sum(percent_space_used)*3/100 from v$flash_recovery_area_usage; 计算flash_recovery_area已经占用的空间。
[font=Georgia]SQL> show parameter recover; 查找recovery目录
[font=Georgia]SQL> desc v$process 通过v$process视图,我们获的是当前系统中所有进程信息,包括“后台进程”,也包括“服务器进程”。
SQL> select addr, program from v$process;
[font=Georgia]
SQL> desc v$bgprocess 通过v$bgprocess视图,我们获得当前系统中启动的Oracle“后台进程”信息
SQL> select paddr, name from v$bgprocess where paddr<>'00';
[font=Georgia]通过v$bgprocess.paddr与v$process.addr关联起来的,关联后查询结果显而易见只会有Oracle“后台进程”的信息
SQL> select a.paddr, a.name, a.description from v$bgprocess a, v$process b where a.paddr=b.addr;
[font=Georgia][size=14px]八、查看ASM磁盘组信息
[font=Georgia]SQL> desc v$asm_disk;
[font=Georgia]SQL> desc v$asm_diskgroup;
[font=Georgia][size=14px]九、常用SQL命令行设置
1、set
set wrap on/off 查询返回的纪录每行超过默认宽度时,可选择换行(on )或不换行(off),默认为换行;
set linesize N 设置查询返回的纪录每行的宽度,超过这个宽度则截掉,不过这个宽度则补空格。

本文标签:
本文标题:oracle常用命令
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】