日常开发过程中,在对Oracle的操作中,多少都会进行一些操作,小弟不才,整理了一些日常用到的函数及方法
--查询某个用户下面所有的索引
SELECT
user_ind_columns.index_name 索引名称,
user_ind_columns.table_name 表名,
user_ind_columns.column_name 字段名,
user_indexes.uniqueness 约束
FROM
user_ind_columns,
user_indexes
WHERE
user_ind_columns.index_name = user_indexes.index_name;
--查询某个用户下面表空间的使用情况
SELECT tbs 表空间名,
sum(totalM) 总共大小M,
sum(usedM) 已使用空间M,
sum(remainedM) 剩余空间M,
sum(usedM)/sum(totalM)*100 已使用百分比,
sum(remainedM)/sum(totalM)*100 剩余百分比
FROM(
SELECT b.file_id ID,
b.tablespace_name tbs,
b.file_name name,
b.bytes/1024/1024 totalM,
(b.bytes-sum(nvl(a.bytes,0)))/1024/1024 usedM,
sum(nvl(a.bytes,0)/1024/1024) remainedM,
sum(nvl(a.bytes,0)/(b.bytes)*100),
(100 - (sum(nvl(a.bytes,0))/(b.bytes)*100))
FROM dba_free_space a,dba_data_files b
WHERE a.file_id = b.file_id
GROUP BY b.tablespace_name,b.file_name,b.file_id,b.bytes
ORDER BY b.tablespace_name
)
GROUP BY tbs ;
SELECT UPPER(F.TABLESPACE_NAME) "表空间名",
D.TOT_GROOTTE_MB "表空间大小(M) ",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M) ",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,
2),
'990.99') "使用比",
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;
--扩展某一表空间的大小(单个dbf文件最大为32G)
ALTER TABLESPACE ANALYSIS ADD
DATAFILE '/data/oracle/oradata/orcl/analysis11.dbf' SIZE 30G
AUTOEXTEND ON NEXT 500M MAXSIZE UNLIMITED;
--设置表空间为自动增长
alter database datafile '/data/oracle/oradata/orcl/analysis.dbf' autoextend on next 1G maxsize 30G;
--查询某个用户下所有的dblink
select * from dba_db_links;
-- 查询所有用户的表,视图等
select * from all_tab_comments;
-- 查询本用户的表,视图等
select * from user_tab_comments;
--查询所有用户的表的列名和注释.
select * from all_col_comments;
--查询本用户的表的列名和注释
select * from user_col_comments;
--查询所有用户的表的列名等信息(详细但是没有备注).
select * from all_tab_columns;
--查询本用户的表的列名等信息(详细但是没有备注).
select * from user_tab_columns;
--查询当前用户下锁表
select sess.sid,
sess.serial#,
lo.oracle_username,
lo.os_user_name,
ao.object_name,
lo.locked_mode
from v$locked_object lo,
dba_objects ao,
v$session sess
where ao.object_id = lo.object_id and lo.session_id = sess.sid;
--删除dblink
drop public database link dg;
--创建dblink
create public database link DB2LINK connect to TXJBOS identified by TXJBOS using '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=172.16.9.197)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=172.16.9.198)(PORT=1521))(LOAD_BALANCE=yes)(FAILOVER=on))(CONNECT_DATA=(SERVICE_NAME=innetdb)))
';
--Oracle 表空间是否开启自动扩展
SELECT T.TABLESPACE_NAME,
D.FILE_NAME,
D.AUTOEXTENSIBLE,
D.BYTES,
D.MAXBYTES,
D.STATUS
FROM DBA_TABLESPACES T, DBA_DATA_FILES D
WHERE T.TABLESPACE_NAME = D.TABLESPACE_NAME
ORDER BY TABLESPACE_NAME, FILE_NAME;
--同一字段用逗号分隔(列转行):
SELECT LISTAGG(CASE
WHEN FUNC_CHECKDISABILITYCARD(S.AAD003) = 0 THEN
S.AAD003
END) WITHIN GROUP(ORDER BY S.AHH002) AS C022_DESC
FROM AA01_2014 T, AH02_2014 S
WHERE T.STATUS <> 0
AND S.STATUS <> 0
AND T.AAA001 = S.AAA001
AND T.AAH009 <> '-1'
AND T.BASENUM_FLAG = '2016'
AND T.AAH009 IS NOT NULL
GROUP BY S.AAA001;
--复制表结构及数据
create table_name as select * from Source_table where 1=1;
--复制表结构
create table_name as select * from Source_table where 1 <> 1;
--根据表名查询过程名
SELECT name,
type,
referenced_owner,
referenced_name,
referenced_type
FROM user_dependencies
WHERE referenced_name = upper('tb_poor_tpysdf')
AND type = 'PROCEDURE';
SELECT DISTINCT NAME
FROM USER_SOURCE
WHERE TYPE = 'PROCEDURE'
AND UPPER(TEXT) LIKE UPPER('%insert into TBL_DA_POOR_VILLAGE_INFO%');
--查询Oracle正在执行的sql语句及执行该语句的用户
SELECT b.sid oracleID,
b.username Oracle用户,
b.serial#,
spid 操作系统ID,
paddr,
sql_text 正在执行的SQL,
b.machine 计算机名
FROM v$process a, v$session b, v$sqlarea c
WHERE a.addr = b.paddr
AND b.sql_hash_value = c.hash_value;
--查看正在执行sql的发起者的发放程序
SELECT A.serial#,OSUSER 电脑登录身份,
PROGRAM 发起请求的程序,
USERNAME 登录系统的用户名,
SCHEMANAME,
B.Cpu_Time 花费cpu的时间,
STATUS,
B.SQL_TEXT 执行的sql
FROM V$SESSION A
LEFT JOIN V$SQL B ON A.SQL_ADDRESS = B.ADDRESS
AND A.SQL_HASH_VALUE = B.HASH_VALUE
ORDER BY b.cpu_time DESC;
--查出oracle当前的被锁对象
SELECT l.session_id sid,
s.serial#,
l.locked_mode 锁模式,
l.oracle_username 登录用户,
l.os_user_name 登录机器用户名,
s.machine 机器名,
s.terminal 终端用户名,
o.object_name 被锁对象名,
s.logon_time 登录数据库时间
FROM v$locked_object l, all_objects o, v$session s
WHERE l.object_id = o.object_id
AND l.session_id = s.sid
ORDER BY sid, s.serial#;
----查看临时表空间
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 ;
--查看临时表分区
SELECT *
FROM (SELECT USERNAME,
SESSION_ADDR,
SQL_ID,
CONTENTS,
SEGTYPE,
BLOCKS * 8 / 1024 / 1024 GB
FROM V$SORT_USAGE
ORDER BY BLOCKS DESC)
WHERE ROWNUM <= 200;
--字段用逗号隔开转成一行
SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP(ORDER BY COLUMN_ID) AS COLUMN_ORDER
FROM DBA_TAB_COLUMNS
WHERE OWNER = UPPER('hndajzfp')
AND TABLE_NAME = UPPER('tbl_fpxm_poor_jyfp')
ORDER BY COLUMN_ID;
--删除重复数据
UPDATE TBL_POOR_FAMILY_SIGN S
SET S.STATE = '0'
WHERE (S.USER_ID, SUBSTR(S.CREATE_TIME, 0, 10)) IN
(SELECT T.USER_ID, SUBSTR(T.CREATE_TIME, 0, 10)
FROM TBL_POOR_FAMILY_SIGN T
WHERE T.CREATE_TIME IS NOT NULL
AND T.STATE != '0'
GROUP BY SUBSTR(T.CREATE_TIME, 0, 10), T.USER_ID
HAVING COUNT(T.USER_ID) > 1)
AND ROWID NOT IN (SELECT MIN(ROWID)
FROM TBL_POOR_FAMILY_SIGN T
WHERE T.CREATE_TIME IS NOT NULL
AND T.STATE != '0'
GROUP BY SUBSTR(T.CREATE_TIME, 0, 10), T.USER_ID
HAVING COUNT(T.USER_ID) > 1);
--回车符换行符替换为空
SELECT translate(t.xjbfdw,chr(13)||chr(10),',') FROM tbl_hyfp_wsfp t
--分区、子分区信息
SELECT * FROM USER_TAB_PARTITIONS;
SELECT * FROM USER_TAB_SUBPARTITIONS;
--interval 用法
select sysdate - interval '10' day as "10天前",
sysdate - interval '10' hour as "10小时前",
sysdate - interval '10' minute as "10分钟前",
sysdate - interval '10' second as "10秒钟前",
sysdate - 10 as "10天前",
sysdate - 10 / 24 as "10小时前",
sysdate - 10 / (24 * 60) as "10分钟前",
sysdate - 10 / (24 * 3600) as "10秒钟前"
from dual;
-- 查看所有角色:
select * from dba_roles;
-- 查看哪些用户有sysdba或sysoper系统权限(查询时需要相应权限)
select * from V$PWFILE_USERS;
-- 查看当前有哪些用户正在使用数据
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;
--查看工作空间的路径
select * from dba_data_files;
--查看数据库连接
SELECT * FROM V$SESSION;
--查看数据库的连接数
select count(*) from v$session;
--查询oracle的并发连接数
select count(*) from v$session where status='ACTIVE';
--数据库允许的最大连接数
select value from v$parameter where name = 'processes' ;
--修改最大连接数:
alter system set processes = 300 scope = spfile;
--查看不同用户的连接数
select username,count(username) from v$session where username is not null group by username;
-- 创建临时表空间
CREATE temporary TABLESPACE HM_QA_TMP tempfile '$ORACLE_HOME/HM_QA_TMP.dbf' size 128m autoextend on next 32m maxsize 512m;
-- 创建表空间
CREATE TABLESPACE HM_QA DATAFILE '$ORACLE_HOME/HM_QA.dbf' size 256M autoextend on next 128m maxsize 512m;
-- 创建用户和授权
CREATE USER HM_QA IDENTIFIED BY 123456 DEFAULT TABLESPACE HM_QA TEMPORARY TABLESPACE HM_QA_TMP ;
grant all privileges to HM_QA ;
-- 尽量给用户DBA权限
grant sysdba to HM_QA ;
grant imp_full_database to HM_QA ;
---数据导出:
--1、 将数据库test完全导出,用户名system 密码manager 导出到d:/daochu.dmp中
exp system/manager@test file=/u01/app/daochu.dmp full=y
--2 、将数据库中system用户与sys用户的表导出
exp system/manager@test file=/u01/app/daochu.dmp owner=\(system,sys\);
--3 、将数据库中的表inner_notify、notify_staff_relat导出
exp aichannel/aichannel@testdb2 file=/u01/app/newsmgnt.dmp tables=\(inner_notify,notify_staff_relat\);
--4 、将数据库中的表table1中的字段filed1以"00"打头的数据导出
exp system/manager@test file=/u01/app/daochu.dmp tables=\(table1\) query=/" where filed1 like 00%/"
--上面是常用的导出,对于压缩,既用winzip把dmp文件可以很好的压缩。 也可以在上面命令后面 加上 compress=y 来实现。
--数据导入:
--1 、将/u01/app/daochu.dmp 中的数据导入 test数据库中。
imp aichannel/aichannel@hust full=y file=/u01/app/newsmgnt.dmp ignore=y
--上面可能有点问题,因为有的表已经存在,然后它就报错,对该表就不进行导入。 在后面加上 ignore=y 就可以了。
--2 、将/u01/app/aochu.dmp中的表table1 导入
imp system/manager@test file=/u01/appdaochu.dmp tables=(table1)
--基本上上面的导入导出够用了。不少情况要先是将表彻底删除,然后导入
--Oracle查询SQL语句执行的耗时
select a.sql_text SQL语句,
b.etime 执行耗时,
c.user_id 用户ID,
c.SAMPLE_TIME 执行时间,
c.INSTANCE_NUMBER 实例数,
u.username 用户名, a.sql_id SQL编号
from dba_hist_sqltext a,
(select sql_id, ELAPSED_TIME_DELTA / 1000000 as etime
from dba_hist_sqlstat
-- where ELAPSED_TIME_DELTA / 1000000 >= 1
) b,
dba_hist_active_sess_history c,
dba_users u
where a.sql_id = b.sql_id
and u.username = 'EIS_DEV'
and c.user_id = u.user_id
and b.sql_id = c.sql_id
and a.sql_text like '%SELECT%'
order by SAMPLE_TIME desc,
b.etime desc;
--读取磁盘次数最多的SQL
SELECT * FROM (
SELECT
SQL_ID,ADDRESS,HASH_VALUE,COMMAND_TYPE, PARSING_USER_ID, PARSING_SCHEMA_NAME, EXECUTIONS, SORTS, DISK_READS, BUFFER_GETS, CPU_TIME, SQL_FULLTEXT
FROM
V$SQLAREA
ORDER BY
DISK_READS DESC
)WHERE ROWNUM<10 ;
--消耗CPU时间最多的SQL
SELECT * FROM (
SELECT
SQL_ID,ADDRESS,HASH_VALUE,COMMAND_TYPE, PARSING_USER_ID, PARSING_SCHEMA_NAME, EXECUTIONS, SORTS, DISK_READS, BUFFER_GETS, CPU_TIME, SQL_FULLTEXT
FROM
V$SQLAREA
ORDER BY
CPU_TIME DESC
)WHERE ROWNUM<10 ;
--执行次数最多的SQL
SELECT * FROM (
SELECT
SQL_ID,ADDRESS,HASH_VALUE,COMMAND_TYPE, PARSING_USER_ID, PARSING_SCHEMA_NAME, EXECUTIONS, SORTS, DISK_READS, BUFFER_GETS, CPU_TIME, SQL_FULLTEXT
FROM
V$SQLAREA
ORDER BY
EXECUTIONS DESC
)WHERE ROWNUM<10 ;
-- 查询索引
select index_name from all_indexes where table_name = 'BELL_ENT_UPDATE';
-- 创建索引
create index bell_ent_update_subid_idx on BELL_ENT_UPDATE(SUB_ID);
-- 删除索引
drop index bell_ent_update_subid_idx;
--查看表的空间占用
select num_rows * avg_row_len,table_name from user_tables;
select Segment_Name, Sum(bytes) / 1024 / 1024 / 1024 "size(DB)"
From User_Extents
Group By Segment_Name
order by "size(DB)" desc, Segment_Name;
--Oracle 远程登陆
sqlplus kmsbos/kmsbos@ip:1521/innetdb
--with as
with
e as (select * from scott.emp),
d as (select * from scott.dept)
select * from e, d where e.deptno = d.deptno;
--merge into
MERGE INTO table_name alias1
USING (table | view | sub_query) alias2
ON (join condition)
WHEN MATCHED THEN
UPDATE table_name SET col1 = col_val1, col2 = col2_val
WHEN NOT MATCHED THEN
INSERT (column_list) VALUES (column_values);