******************************
日期/时间 相关查询 *****************************
select
trunc (sysdate,
'month'
)
"First
day of current month"
from
dual;
select
trunc (last_day (sysdate))
"Last
day of current month"
from
dual;
select
trunc (sysdate,
'YEAR'
)
"Year
First Day"
from
dual;
select
add_months (trunc (sysdate,
'YEAR'
),
12) - 1
"Year
Last Day"
from
dual
select
cast
(to_char (last_day (sysdate),
'dd'
)
as
int
)
number_of_days
from
dual;
select
sysdate, last_day (sysdate)
"Last"
,
last_day (sysdate) - sysdate
"Days
left"
from
dual;
select
round((months_between(to_date(
'2015-05-12'
,
'yyyy-mm-dd'
),
sysdate) * 30), 0) num_of_days
from
dual;
select
trunc(sysdate) - trunc(to_date(
'2015-05-9'
,
'yyyy-mm-dd'
))
from
dual;
select
add_months(trunc(sysdate,
'MONTH'
),
i) start_date,
trunc(last_day(add_months(sysdate,
i))) end_date
from
xmltable(
'for
$i in 0 to xs:int(D) return $i'
passing
xmlelement(d,floor(months_between(add_months(trunc(sysdate,
'YEAR'
)-1,12),sysdate)))
columns i
integer
path
'.'
);
select
(sysdate - trunc (sysdate)) * 24 * 60 * 60 num_of_sec_since_morning
from
dual;
select
(trunc (sysdate+1) - sysdate) * 24 * 60 * 60 num_of_sec_left
from
dual;
******************************
数据字典 相关查询 *****************************
select
table_name
from
user_tables
where
table_name =
'TABLE_NAME'
;
select
column_name
as
found
from
user_tab_cols
where
table_name =
'TABLE_NAME'
and
column_name =
'COLUMN_NAME'
;
select
dbms_metadata.get_ddl(
'TABLE'
,
'ACCT'
)
from
dual;
select
t.table_name,t.column_name,t.data_type,t.data_length,t.nullable,t.column_id,c.comments,
(
select
case
when
t.column_name=m.column_name
then
1
else
0
end
from
dual) iskey
from
user_tab_cols t, user_col_comments c, (
select
m.column_name
from
user_constraints s, user_cons_columns m
where
lower
(m.table_name)=
'表名'
and
m.table_name=s.table_name
and
m.constraint_name=s.constraint_name
and
s.constraint_type=
'P'
)
m
where
lower
(t.table_name)=
'表名'
and
c.table_name=t.table_name
and
c.column_name=t.column_name
and
t.hidden_column=
'NO'
order
by
t.column_id
desc
表名
select
sys_context (
'userenv'
,
'current_schema'
)
from
dual;
alter
session
set
current_schema = new_schema;
******************************
数据库管理 相关查询 *****************************
select
*
from
v$version;
select
username, profile, default_tablespace, temporary_tablespace
from
dba_users;
select
*
from
nls_database_parameters;
select
value
from
v$system_parameter
where
name
=
'compatible'
;
create
table
tab (col1 varchar2 (10));
create
index
idx1
on
tab (
upper
(col1));
analyze
table
a compute
statistics
;
alter
database
datafile
'/work/oradata/STARTST/STAR02D.dbf'
resize 2000m;
select
substr (file_name, 1, 50), autoextensible
from
dba_data_files;
select
tablespace_name, autoextensible
from
dba_data_files;
alter
tablespace data01
add
datafile
'/work/oradata/STARTST/data01.dbf'
size
1000m autoextend
off
;
alter
database
datafile
'/u01/app/Test_data_01.dbf'
resize 2g;
select
sum
(bytes) / 1024 / 1024 / 1024
as
gb
from
dba_data_files;
select
sum
(bytes) / 1024 / 1024 / 1024
as
gb
from
dba_segments;
select
sum
(bytes / 1024 / 1024)
"size"
from
dba_segments
where
owner =
'&owner'
;
select
s.username ||
'('
|| s.sid ||
')-'
|| s.osuser uname,
s.program
||
'-'
|| s.terminal ||
'('
|| s.machine ||
')'
prog,
s.sid
||
'/'
|| s.serial# sid,
s.status
"Status"
,
p.spid,
sql_text
sqltext
from
v$sqltext_with_newlines t, v$session s, v$process p
where
t.address = s.sql_address
and
p.addr = s.paddr(+)
and
t.hash_value = s.sql_hash_value
order
by
s.sid, t.piece;
******************************
性能 相关查询 *****************************
select
ss.username, se.sid, value / 100 cpu_usage_seconds
from
v$session ss, v$sesstat se, v$statname sn
where
se.statistic# = sn.statistic#
and
name
like
'%CPU used by this session%'
and
se.sid = ss.sid
and
ss.status =
'ACTIVE'
and
ss.username
is
not
null
order
by
value
desc
;
select
a.sid,
a.serial#,
b.username,
opname
operation,
target
object,
trunc
(elapsed_seconds, 5)
"ET
(s)"
,
to_char
(start_time,
'HH24:MI:SS'
)
start_time,
round
( (sofar / totalwork) * 100, 2)
"COMPLETE
(%)"
from
v$session_longops a, v$session b
where
a.sid = b.sid
and
b.username
not
in
(
'SYS'
,
'SYSTEM'
)
and
totalwork > 0
order
by
elapsed_seconds;
select
b.sid, b.serial#, a.spid processid, b.process clientpid
from
v$process a, v$session b
where
a.addr = b.paddr
and
b.audsid = userenv (
'sessionid'
);
V$SESSION.SID
AND
V$SESSION.SERIAL# 是数据库进程 ID
V$PROCESS.SPID
是数据库服务器后台进程 ID
V$SESSION.PROCESS
是客户端 PROCESS ID,
ON
windows it
IS
: separated THE
FIRST
#
IS
THE PROCESS ID
ON
THE client
AND
2nd one
IS
THE THREAD id.
select
created,
timestamp
,
last_ddl_time
from
all_objects
where
owner =
'MYSCHEMA'
and
object_type =
'TABLE'
and
object_name =
'EMPLOYEE_TABLE'
;
select
*
from
(
select
rownum,
substr
(a.sql_text, 1, 200) sql_text,
trunc
(
a.disk_reads
/ decode (a.executions, 0, 1, a.executions))
reads_per_execution,
a.buffer_gets,
a.disk_reads,
a.executions,
a.sorts,
a.address
from
v$sqlarea a
order
by
3
desc
)
where
rownum < 10;
select
osuser, username, machine, program
from
v$session
order
by
osuser;
select
program application,
count
(program) numero_sesiones
from
v$session
group
by
program
order
by
numero_sesiones
desc
;
select
username usuario_oracle,
count
(username) numero_sesiones
from
v$session
group
by
username
order
by
numero_sesiones
desc
;
select
owner,
count
(owner) number_of_objects
from
dba_objects
group
by
owner
order
by
number_of_objects
desc
;
******************************
实用/数学 相关的查询 *****************************
select
to_char (to_date (1526,
'j'
),
'jsp'
)
from
dual;
select
*
from
dba_source
where
upper
(text)
like
'%FOO_SOMETHING%'
and
owner =
'USER_NAME'
;
with
csv
as
(
select
'AA,BB,CC,DD,EE,FF'
as
csvdata
from
dual)
select
regexp_substr (csv.csvdata,
'[^,]+'
,
1,
level
)
pivot_char
from
dual, csv
connect
by
regexp_substr (csv.csvdata,
'[^,]+'
,
1,
level
)
is
not
null
;
select
*
from
dept
where
rowid
in
(
select
max
(rowid)
from
表名);
select
*
from
dept
minus
select
*
from
dept
where
rownum < (
select
count
(*)
from
表名);
with
tbl
as
(
select
-2 num
from
dual
union
select
-3 num
from
dual
union
select
-4 num
from
dual),
sign_val
as
(
select
case
mod (
count
(*), 2)
when
0
then
1
else
-1
end
val
from
tbl
where
num < 0)
select
exp (
sum
(ln (
abs
(num)))) * val
from
tbl, sign_val
group
by
val;
select
level
empl_id,
mod
(rownum, 50000) dept_id,
trunc
(dbms_random.value (1000, 500000), 2) salary,
decode
(round (dbms_random.value (1, 2)), 1,
'M'
,
2,
'F'
)
gender,
to_date
(
round
(dbms_random.value (1, 28))
||
'-'
||
round (dbms_random.value (1, 12))
||
'-'
||
round (dbms_random.value (1900, 2010)),
'DD-MM-YYYY'
)
dob,
dbms_random.string
(
'x'
,
dbms_random.value (20, 50)) address
from
dual
connect
by
level
< 10000;
select
round (dbms_random.value () * 100) + 1
as
random_num
from
dual;
select
1
from
表名
where
rownum = 1;