oracle常用(11g)
1.查询表字段注释:
select * from all_tab_columns a where a.TABLE_NAME='T_X27_USER';
2.查询oracle的历史操作记录:SELECT sql_text, last_load_time FROM v$sql
WHERE last_load_time IS NOT NULL ORDER BY last_load_time DESC;
2.查询当前用户所有的表以及表数据量大小、注释:
select a.table_name,b.comments,a.num_rows from user_tables a,
user_tab_comments b where a.table_name=b.table_name;
3.去除空格
update t1 set col1=trim(col1);
update xld a set a.xm=(select Trim(replace(b.xm,' ','')) from xld b where a.gh=b.gh);
4.cas when的用法:
select bm,
case
when bm = '06' then
'男孩'
else
case
when bm = '01' then
'女孩'
else
'其他'
end
end as sex
from xld;
4.merge into的用法:
merge into emp a
using (select 33 empno, 'kht' ename from dual) b
on (a.empno = b.empno)
when matched then
update set ename = 'khtt'
when not matched then
insert (empno, ename, job) values ('33', '33', '33');
3.查询当前用户所有的的删除指令:
SELECT 'drop table '|| table_name || ';' FROM USER_TABLES ORDER BY TABLE_NAME;
4.常用导入:imp zfsoft_teacher/zfsoft@172.20.13.100/orcl file='D:ack eacherjw_user20200603.dmp' full=y
1.切换用户命令:
conn system/kht ;conn sys/kht AS SYSDBA
2.查看当前登录的用户的表:
select table_name from user_tables;
3.复制emp表的数据和结构,复制后的表名为myemp:
create table myemp as select* from emp;
4.复制scott用户下的emp表的数据和结构到当前用户,复制后的表名为myemp:
create table myemp as select* from scott.emp;
5.复制scott用户下的emp表的结构到当前用户,复制后的表名为empp:
create table empp as select * from scott.emp where 1=2;
6.查询所有用户
select * from all_users;
7.查看所有用户及用户状态
select * from dba_users;
8.查看当前用户及状态
select * from user_users;
9.建用户名和密码:
create user kht identified by password;
10.删除用户:
drop user kht cascade;
11.强迫用户修改密码:
alter user kht password expire;
12.为用户授权:
grant connect,resource,dba to kht with admin option;
13.查看当前用户被授予的角色
select * from user_role_privs;
14.回收权限:
revoke connect from kht;
15.改变用户锁定状态:
alter user scott account unlock;
16.查看当前连接数
select count(*) from v$process;
17.查看数据库允许的最大连接数
select value from v$parameter where name='processes';
18.死锁的处理:SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,
l.ORACLE_USERNAME,l.OS_USER_NAME,l.PROCESS
FROM V$LOCKED_OBJECT l,V$SESSION S WHERE l.SESSION_ID=S.SID;
alter system kill session '91427,1549';
--------------------------------------------------------------------
select s.sid,s.serial#,object_name,machine,s.INST_ID
from gv$locked_object l,dba_objects o ,gv$session s
where l.object_id = o.object_id and l.session_id=s.sid;
alter system kill session '6446,10295,@1';
--------------------------------------------------------------------
18.从服务器上导出
exp zfsoft_jkcj/JKCJ_67A5POWHL6@172.20.13.100/orcl
file=d:akhtstudent.dmp log=d:akhtstudent.log full=y
exp zfsoft_jkcj_js/JKCJ_67A5POWHL6_JS@172.20.13.100/orcl
file=d:akht eacher.dmp log=d:akht eacher.log full=y
19.导入
imp 用户/密码 file=文件路径 full=y ignore=y;
full=y 是导入文件中全部内容
ignore=y相当于,如果没有的表,创建并倒入数据,如果已经有的表,忽略创建的,但不忽略倒入
buffer 缓冲器大小的配置
如: imp kht/kht file='C:UserskhtDesktop
ull eacher.dmp' full=y ignore=y
通过cmd导入到服务器上的数据库
imp zfsoft_student/zfsoft@172.20.13.100/orcl file='d:akhtstudent.dmp'
fromuser=zfsoft_jkcj touser=zfsoft_student
imp zfsoft_tea/zfsoft@172.20.13.100/orcl file='d:akht eacher.dmp'
fromuser=zfsoft_jkcj_js touser=zfsoft_tea
fromuser=原用户 touser=新用户
20.windows cmd登录服务器oracle
sqlplus zfsoft_jkcj_js/kht@172.20.13.100/orcl
21.删除已经连接的用户
select username,sid,serial# from v$session;
alter system kill session '575,3109';
drop user zfsoft_teacher cascade;
22.内容替换:update zfsoft_oa.zftal_zhbgpt_tzggb set nr=replace(nr,'http://zfoa.zjhu.edu.cn:81','') where lb='02'
and nr like '%http://zfoa.zjhu.edu.cn:81/%'
22.查询所有表名和表名注释
select
t.table_name tablename,
f.comments comments
from
user_tables t
inner join user_tab_comments f on t.table_name = f.table_name;
23.查询某一表的表段类型及其注释
select
t.table_name zftal_mobilehd_yhb,
t.column_name columnname,
t.data_type datatype,
a.comments
from
user_tab_columns t
left join user_col_comments a on t.table_name = a.table_name
and t.column_name = a.column_name;
24.--查询表空间使用情况
select df.tablespace_name,
count(*) datafile_count,
round(sum(df.bytes) / 1048576 / 1024, 2) size_gb,
round(sum(free.bytes) / 1048576 / 1024, 2) free_gb,
round(sum(df.bytes) / 1048576 / 1024 -
sum(free.bytes) / 1048576 / 1024,
2) used_gb,
round(max(free.maxbytes) / 1048576 / 1024, 2) maxfree,
100 - round(100.0 * sum(free.bytes) / sum(df.bytes), 2) pct_used,
round(100.0 * sum(free.bytes) / sum(df.bytes), 2) pct_free
from dba_data_files df,
(select tablespace_name,
file_id,
sum(bytes) bytes,
max(bytes) maxbytes
from dba_free_space
where bytes > 1024 * 1024
group by tablespace_name, file_id) free
where df.tablespace_name = free.tablespace_name(+)
and df.file_id = free.file_id(+)
group by df.tablespace_name
order by 8;