1 登陆sqlplus
sqlplus / as sysdba
2 查找用户
select * from dba_users;
-- 查看所有用户:
select * from all_users;
-- 查看用户或角色系统权限(直接赋值给用户或角色的系统权限):
select * from dba_sys_privs;
select * from user_sys_privs;
-- 查看角色(只能查看登陆用户拥有的角色)所包含的权限
select * from role_sys_privs;
-- 查看用户对象权限:
select * from dba_tab_privs;
select * from all_tab_privs;
select * from user_tab_privs;
-- 查看所有角色:
select * from dba_roles;
-- 查看用户或角色所拥有的角色:
select * from dba_role_privs;
select * from user_role_privs;
-- 查看哪些用户有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;
3 查找工作空间的路径
select * from dba_data_files;
4 删除当前用户下的所有表
SQL> show parameter deferred_segment_creation
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
deferred_segment_creation boolean TRUE
SQL> alter system set deferred_segment_creation=false;
系统已更改。
SQL> show parameter deferred_segment_creation
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
deferred_segment_creation boolean FALSE
SQL> select 'drop table '||table_name||';' from cat where table_type='TABLE';
5 删除用户及表空间
-- 删除用户
drop user 用户名称 cascade;
-- 删除表空间
drop tablespace 表空间名称 including contents and datafiles cascade constraint;
--例如:删除用户名成为LYK,表空间名称为LYK
--删除用户,及级联关系也删除掉
drop user LYK cascade;
--删除表空间,及对应的表空间文件也删除掉
drop tablespace LYK including contents and datafiles cascade constraint;
drop tablespace LYK_TMP including contents and datafiles cascade constraint;
6 查询数据库连接SID
SELECT sid,serial#,username FROM v$session WHERE username = 'QSCG_UAT';
ALTER SYSTEM KILL SESSION '148, 2329';
7 查看数据库连接
--查看数据库连接
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;
8 查询oracle那个表被锁
-- 查询oracle那个表被锁
select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;
9 查询正在执行的sql
-- 查询正在执行的sql
select a.program, b.spid, c.sql_text, c.SQL_ID from v$session a, v$process b, v$sqlarea c
where a.paddr = b.addr and a.sql_hash_value = c.hash_value and a.username is not null;
10 查询某PID占用资源
--查询那个PID占用资源
SELECT sql_text FROM v$sqltext a WHERE (a.hash_value, a.address) IN(SELECT DECODE(sql_hash_value, 0, prev_hash_value, sql_hash_value),DECODE(sql_hash_value, 0, prev_sql_addr, sql_address) FROM v$session b WHERE b.paddr =(SELECT addr FROM v$process c WHERE c.spid = '26025'))ORDER BY piece ASC;
11 创建表空间及用户
-- 创建临时表空间
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 ;
12 数据库导入导出
-- 导出数据库时可能操作系统编码和数据库不一致会导致EXP-00091错误,
>SQL select * from nls_database_parameters t where t.parameter='NLS_CHARACTERSET';
>SQL select * from v$nls_parameters where parameter='NLS_CHARACTERSET';
-- 根据⑴查出的NLS_CHARACTERSET(AL32UTF8)来设定exp的环境变量:
set NLS_LANG=AMERICAN_AMERICA.AL32UTF8
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
echo $NLS_LANG
-- 导出数据库
exp HM_DEV/123456 owner=HM_DEV file=HM_DEV_2019_01_17.dmp;
exp green/light@MSSPS file=e:\ren.dmp tables=(tab_mobilephone_recharge,trade_detail,terminals,shops,locations,citys) query=\"where rownum<=5000\"
-- 导入数据库
imp HM_DEV/123456@orcl file=HM_DEV_2019_01_17.dmp ignore=y FULL=Y;
数据导出:
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)
基本上上面的导入导出够用了。不少情况要先是将表彻底删除,然后导入
13 解决oracl
e11g 空表不能exp导出的问题
show parameter deferred_segment_creation
NAME TYPE VALUE
------------------------------------ ----------- -------
deferred_segment_creation boolean TRUE
-- 数据库参数deferred_segment_creation=true,将导致dmp备份无法导出空表结构;
alter system set deferred_segment_creation=false;
-- 此方法只对以后的表有效,之前的表还是没分配空间。
-- 1、先查询一下当前用户下的所有空表 ,优先使用segment_created = 'NO'
select * from user_tables where segment_created = 'NO';
--select table_name from user_tables where NUM_ROWS=0;
-- 2、批量生成修改语句
select 'alter table ' || table_name || ' allocate extent;' from user_tables where segment_created = 'NO';
--select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0;
-- 3、执行修改语句
alter table 表名 allocate extent;
14 ORACLE删除表
Oracle如果开启了 flash
功能, 当我们删除表drop table HM_TABLE;
后会出现类似BIN$dJ4cxvVNZVHgQAB/AQBx3w==$0
表,
这个表还BIN$dJ4cxvVNZVHgQAB/AQBx3w==$0
存在,可以使用命令:来恢复至删除前
-- 来恢复至删除前
flashback table "BIN$dJ4hg1pva6/gQAB/AQByAw==$0" to before drop;
commit;
如果想彻底删除一个表处理方式
-- 如果想彻底删除一个表
drop table HM_TABLE purge;
commit;
如果想清空flash存储已经删除的表,用命令:
purge recyclebin;
commit;
如果想清除flash中指定的表,可以使用命令:
purge table HM_TABLE;
commit;
15 查询用户下的所有表名
select * from user_tab_comments;
16 查询用户下所有字段名
select * from user_col_comments;
17 导出当前用户的所有备注(包含表和字段)
SELECt 'COMMENT ON TABLE '|| TABLE_NAME || ' IS ' || '''' || nvl(COMMENTS, '') || ''';' from user_tab_comments
UNION all
SELECt 'COMMENT ON COLUMN '|| TABLE_NAME || '.' || COLUMN_NAME || ' IS ' || '''' || nvl(COMMENTS, '') || ''';'
from user_col_comments;
--相应的还有dba_col_comments,all_col_comments,这两个比user_col_comments多了ower列
18 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;
19 执行过的SQL
SELECT
SQL_ID, HASH_VALUE, ADDRESS, SQL_FULLTEXT,LAST_LOAD_TIME
FROM
V$SQL
ORDER BY
LAST_LOAD_TIME DESC;
-------------------------------------------------------------------------------------------------
SELECT
SQL_ID, HASH_VALUE, ADDRESS, B.SQL_FULLTEXT, B.FIRST_LOAD_TIME
FROM
V$SQLAREA B
ORDER BY
B.FIRST_LOAD_TIME DESC;
20 正在执行的SQL
SELECT
SSN.USERNAME, SSN.SID, SSN.SQL_ID,SAA.ADDRESS, SAA.HASH_VALUE, SAA.SQL_FULLTEXT
FROM
V$SESSION SSN, V$SQLAREA SAA
WHERE
SSN.SQL_ADDRESS = SAA.ADDRESS AND SSN.SQL_HASH_VALUE = SAA.HASH_VALUE;
21 读取磁盘次数最多的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 ;
22 消耗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 ;
23 执行次数最多的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 ;
24 用户表空间及使用率
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;
25 查看表空间是否为online
select * from v$tempfile;
26 查看普通数据文件是否扩展
select d.file_name,d.tablespace_name,d.autoextensible from dba_data_files d;
27 查看临时表空间是否可以扩展
select d.file_name,d.tablespace_name,d.autoextensible from dba_temp_files d;
28 临时表空间和数据表空间
select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space from dba_temp_files
union all
select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_space from dba_data_files;
29 修改数据表空间大小
alter database datafile '/u01/app/oracle/product/11.2.0/db_1/EIS_QA.dbf' resize 2048m;
30 删除表空间
alter database datafile '/u01/app/oracle/product/11.2.0/db_1/EIS.dbf' drop;
31 修改临时表空间大小
alter database tempfile '/u01/app/oracle/product/11.2.0/db_1/EIS_QA_TMP.dbf' resize 2048m;
32 删除临时表空间
alter database tempfile '/u01/app/oracle/product/11.2.0/db_1/EIS_TEMP_01.dbf' drop;
33 临时表空间添加新的临时数据文件
alter tablespace EIS add tempfile '/u01/app/oracle/product/11.2.0/db_1/EIS_ADD_01.dbf' size 400m;
34 临时文件自动扩展
alter database tempfile '/u01/app/oracle/product/11.2.0/db_1/EIMS_TMP.dbf' autoextend on next 5m maxsize unlimited;
35 关闭(启动)临时文件的自动增长
alter database tempfile 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\TEMP01.DBF' autoextend off(on);
36 oracle中用户删除不了,ORA-01940提示 “无法删除当前已连接用户”
先锁定用户、然后查询进程号,最后删除对应的进程、在删除对应的用户
SQL>alter user EIS account lock;
SQL>SELECT * FROM V$SESSION WHERE USERNAME='EIS';
SQL>alter system kill session 'xx, xx'
SQL>drop user xx cascade
37 Oracle 表添加索引
-- 查询索引
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;
说明
1)索引是数据库对象之一,用于加快数据的检索,类似于书籍的索引。在数据库中索引可以减少数据库程序查询结果时需要读取的数据量,类似于在书籍中我们利用索引可以不用翻阅整本书即可找到想要的信息。
2)索引是建立在表上的可选对象;索引的关键在于通过一组排序后的索引键来取代默认的全表扫描检索方式,从而提高检索效率
3)索引在逻辑上和物理上都与相关的表和数据无关,当创建或者删除一个索引时,不会影响基本的表;
4)索引一旦建立,在表上进行DML操作时(例如在执行插入、修改或者删除相关操作时),oracle会自动管理索引,索引删除,不会对表产生影响
5)索引对用户是透明的,无论表上是否有索引,sql语句的用法不变
6)oracle创建主键时会自动在该列上创建索引