本文主要介绍了数据库oracle常用sql查询语句,包括Oracle Server 相关信息查询操作命令和Oracle数据库表常用查询命令,有助于新手迅速熟悉ORACLE服务和基本查询语法。

Oracle Server 相关信息查询操作命令

Oracle查看用户状态

select username,account_status from dba_users;

查看所有角色

select * from dba_roles;

锁定用户

alter user XXX account lock;

Linux下新建用户

useradd -d /home/XXX -s /usr/bin/ksh -m XXX
passwd XXX

用户创建

useradd -d /home/XXX -g users -s /bin/bash -m XXX -G users
passwd XXX

删除用户

userdel -r XXX

Linux查看进程

ps -e|grep ***
kill -9 ***(PID)

linux查看版本信息

lsb_release -a

赋予debug权限给用户

grant debug any procedure, debug connect session to prodb;

Oracle查询用户

select username,password from dba_users;

Oracle删除用户

drop user XXX cascade;

如果遇到报错:Oracle cannot drop a user that is currently connected,则执行以下命令,以sysdba登陆

shutdown immediate;
startup restrict;
drop user *** cascade;
alter system disable restricted session;

Oracle监听

lsnrctl start

即可启动,如果出现以sysdba用户连接无法连接到数据库的现象,检查环境变量ORACLE_HOME,ORACLE_SID

sqlplus下的删除符号

ctrl+backspace

sqlplus远程连接

sqlplus 用户名/密码@10.10.10.10:1521/orcl

查询表空间和对应数据文件的关系

select t1.name, t2.name from v$tablespace t1, v$datafile t2 where t1.ts# = t2.ts#;

查看表空间的名称及大小

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;

查看表空间物理文件的名称及大小

select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;

查看回滚段名称及大小

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;

查看控制文件

select name from v$controlfile;

查看日志文件

select member from v$logfile;

查看表空间的使用情况

select sum(bytes)/(10241024) 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.BYTES100)/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;

查看数据库库对象

select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;

查看数据库的版本 

Select version FROM Product_component_version
Where SUBSTR(PRODUCT,1,6)=’Oracle’;

查看数据库的创建日期和归档方式

Select Created, Log_Mode, Log_Mode From V$Database;

捕捉运行很久的SQL

column username format a12
column opname format a16
column progress format a8
select username,sid,opname,
round(sofar*100 / totalwork,0) || ‘%’ as progress,
time_remaining,sql_text
from v$session_longops , v$sql
where time_remaining <> 0
and sql_address = address
and sql_hash_value = hash_value

删除表空间

drop tablespace **** including contents and datafiles;

取数据重复插入表

insert into table_A(X,X,X,X) select X,X,X,X from table_B where XXX

table_A和table_B可以是一张表,数据当然要经过更新之后再插入

为用户赋debug权限

GRANT debug any procedure, debug connect session TO USER XXX;

查看不同用户的连接数

select username,count(username) from v$session where username is not null group by username;

查看执行日志

select t.”SQL_TEXT”,t.”SQL_FULLTEXT”,t.”LAST_ACTIVE_TIME” from v$sqlarea t where t.PARSING_SCHEMA_NAME in (‘XXX’) and t.”SQL_TEXT” like ‘%XXX%’ and t.LAST_ACTIVE_TIME > to_date(‘201707130623’, ‘yyyymmddhhmi’) order by t.LAST_ACTIVE_TIME;

查看数据表的参数信息

SELECT partition_name, high_value, high_value_length, tablespace_name,
pct_free, pct_used, ini_trans, max_trans, initial_extent,
next_extent, min_extent, max_extent, pct_increase, FREELISTS,
freelist_groups, LOGGING, BUFFER_POOL, num_rows, blocks,
empty_blocks, avg_space, chain_cnt, avg_row_len, sample_size,
last_analyzed
FROM dba_tab_partitions
–WHERE table_name = :tname AND table_owner = :towner
ORDER BY partition_position;

查看还没提交的事务

select * from v$locked_object;
select * from v$transaction;

查找object为哪些进程所用

select
p.spid,
s.sid,
s.serial# serial_num,
s.username user_name,
a.type object_type,
s.osuser os_user_name,
a.owner,
a.object object_name,
decode(sign(48 – command),
1,
to_char(command), ‘Action Code #’ || to_char(command) ) action,
p.program oracle_process,
s.terminal terminal,
s.program program,
s.status session_status
from v$session s, v$access a, v$process p
where s.paddr = p.addr and
s.type = ‘USER’ and
a.sid = s.sid and
a.object=’SUBSCRIBER_ATTR’
order by s.username, s.osuser

回滚段查看

select rownum, sys.dba_rollback_segs.segment_name Name, v$rollstat.extents
Extents, v$rollstat.rssize Size_in_Bytes, v$rollstat.xacts XActs,
v$rollstat.gets Gets, v$rollstat.waits Waits, v$rollstat.writes Writes,
sys.dba_rollback_segs.status status from v$rollstat, sys.dba_rollback_segs,
v$rollname where v$rollname.name( ) = sys.dba_rollback_segs.segment_name and
v$rollstat.usn ( ) = v$rollname.usn order by rownum;

耗资源的进程(top session)

select s.schemaname schema_name, decode(sign(48 – command), 1,
to_char(command), ‘Action Code #’ || to_char(command) ) action, status
session_status, s.osuser os_user_name, s.sid, p.spid , s.serial# serial_num,
nvl(s.username, ‘[Oracle process]’) user_name, s.terminal terminal,
s.program program, st.value criteria_value from v$sesstat st, v$session s , v$process p
where st.sid = s.sid and st.statistic# = to_number(’38’) and (‘ALL’ = ‘ALL’
or s.status = ‘ALL’) and p.addr = s.paddr order by st.value desc, p.spid asc, s.username asc, s.osuser asc;

查看锁(lock)情况

select /* RULE */ ls.osuser os_user_name, ls.username user_name,
decode(ls.type, ‘RW’, ‘Row wait enqueue lock’, ‘TM’, ‘DML enqueue lock’, ‘TX’,
‘Transaction enqueue lock’, ‘UL’, ‘User supplied lock’) lock_type,
o.object_name object, decode(ls.lmode, 1, null, 2, ‘Row Share’, 3,
‘Row Exclusive’, 4, ‘Share’, 5, ‘Share Row Exclusive’, 6, ‘Exclusive’, null)
lock_mode, o.owner, ls.sid, ls.serial# serial_num, ls.id1, ls.id2
from sys.dba_objects o, ( select s.osuser, s.username, l.type,
l.lmode, s.sid, s.serial#, l.id1, l.id2 from v$session s,
v$lock l where s.sid = l.sid ) ls where o.object_id = ls.id1 and o.owner
<> ‘SYS’ order by o.owner, o.object_name;

查看等待(wait)情况

SELECT v$waitstat.class, v$waitstat.count count, SUM(v$sysstat.value) sum_value
FROM v$waitstat, v$sysstat WHERE v$sysstat.name IN (‘db block gets’,
‘consistent gets’) group by v$waitstat.class, v$waitstat.count;

查看sga情况

SELECT NAME, BYTES FROM SYS.V_$SGASTAT ORDER BY NAME ASC;

查看catched object

SELECT
owner,name,db_link,namespace,type,sharable_mem,loads,executions,locks, pins,kept FROM v$db_object_cache;

这一条执行以后会刷屏,就不了截图了。

查看V$SQLAREA

SELECT SQL_TEXT, SHARABLE_MEM, PERSISTENT_MEM, RUNTIME_MEM, SORTS,
VERSION_COUNT, LOADED_VERSIONS, OPEN_VERSIONS, USERS_OPENING, EXECUTIONS,
USERS_EXECUTING, LOADS, FIRST_LOAD_TIME, INVALIDATIONS, PARSE_CALLS, DISK_READS,
BUFFER_GETS, ROWS_PROCESSED FROM V$SQLAREA;

有关connection的相关信息

查看有哪些用户连接

select s.osuser os_user_name, decode(sign(48 – command), 1, to_char(command),
‘Action Code #’ || to_char(command) ) action, p.program oracle_process,
status session_status, s.terminal terminal, s.program program,
s.username user_name, s.fixed_table_sequence activity_meter, ” query,
0 memory, 0 max_memory, 0 cpu_usage, s.sid, s.serial# serial_num
from v$session s, v$process p where s.paddr=p.addr and s.type = ‘USER’
order by s.username, s.osuser;

根据v.sid查看对应连接的资源占用等情况

select n.name,
v.value,
n.class,
n.statistic#
from v$statname n,
v$sesstat v
where v.sid = 31 and
v.statistic# = n.statistic#
order by n.class, n.statistic#

根据sid查看对应连接正在运行的sql

select /* PUSH_SUBQ */
command_type,
sql_text,
sharable_mem,
persistent_mem,
runtime_mem,
sorts,
version_count,
loaded_versions,
open_versions,
users_opening,
executions,
users_executing,
loads,
first_load_time,
invalidations,
parse_calls,
disk_reads,
buffer_gets,
rows_processed,
sysdate start_time,
sysdate finish_time,
‘>’ || address sql_address,
‘N’ status
from v$sqlarea
where address = (select sql_address from v$session where sid = 1);

Oracle数据库表常用查询命令

为了方便测试,执行备份的sql,将sql复制到客户端中的编辑器界面使用f8执行,创建三个表emp,dept,salgrade

查询语句的书写有顺序遵循下面的顺序

select 字段列表
from 表名
where 查询条件
group by子句
order by 排序字段

投影查询(查询部分的列)

SQL> select eno,ename from emp;

算术运算

在查询列表中,可以对NUMBER型的字段使用算数原算符号(+,-,*,/)

注意:如果该列的值为null,要使用nvl()函数,进行处理才能进行运算

字符类型的连接 拼接运算符 ||: 表达字符(串)的拼接,可以将某几列或某列与字符串拼接在一起

SQL> select ‘员工编号为’||empno||’员工的姓名为’||ename||’员工的年薪为’||nvl(sal,0)*12 from emp;

‘员工编号为’||EMPNO||’员工的姓

员工编号为7369员工的姓名为SMITH员工的年薪为0
员工编号为7499员工的姓名为ALLEN员工的年薪为22440
员工编号为7521员工的姓名为WARD员工的年薪为17820
员工编号为7566员工的姓名为JONES员工的年薪为40590
员工编号为7654员工的姓名为MARTIN员工的年薪为17820
员工编号为7698员工的姓名为BLAKE员工的年薪为38940
员工编号为7782员工的姓名为CLARK员工的年薪为33660
员工编号为7788员工的姓名为SCOTT员工的年薪为40920
员工编号为7844员工的姓名为TURNER员工的年薪为21120
员工编号为7876员工的姓名为ADAMS员工的年薪为15840
员工编号为7900员工的姓名为JAMES员工的年薪为13860
员工编号为7902员工的姓名为FORD员工的年薪为40920
员工编号为7934员工的姓名为MILLER员工的年薪为18480
员工编号为7839员工的姓名为KING员工的年薪为67320
14 rows selected

查询的列给个别名

SQL> select empno as “员工编号” , ename as “员工姓名” from emp;

使用distinct实现对某个字段的值去掉重复

SQL> select distinct deptno from emp;

DEPTNO

30
20
10

条件查询Where子句

SQL> select * from emp where 1=1;

<>或者!=

SQL> select * from emp where empno != 7369;

<, >,>=,<=

SQL> select * from emp where empno > 7369;

between … and… 等同于>= and <=

SQL> select * from emp where empno >= 7369 and empno <=7566;

is null 或者是 is not null 判断该列的值为null

SQL> select * from emp where sal is null;

and并且 ,or或者 ,not不 is null,is not null, like,not like, in not in,NOT BETWEEN AND

以上就是数据库oracle常用sql查询语句介绍,适合新手学习练手。

更多文章,请持续关注《MySql教程网》https://mysql360.com