oracle初学
1. Oracle安装完成后的初始口令?
internal/oracle
sys/change_on_install
system/manager
scott/tiger
sysman/oem_temp
2. oracle中的裸设备指的是什么?
裸设备就是绕过文件系统直接访问的储存空间。
3. 请问如何分辨某个用户是从哪台机器登陆ORACLE的?
SELECT machine , terminal FROM V$SESSION;
4. 用什么语句查询字段呢?
desc table_name 可以查询表的结构
select field_name,... from ...可以查询字段的值
select * from all_tables where table_name like '%'
select * from all_tab_columns where table_name='??'
5. 怎样得到触发器、过程、函数的创建脚本?
desc user_source
user_triggers
6. 怎样计算一个表占用的空间的大小?
select owner,table_name,
NUM_ROWS,
BLOCKS*AAA/1024/1024 "Size M",
EMPTY_BLOCKS,
LAST_ANALYZED
from dba_tables
where table_name='XXX';
Here: AAA is the value of db_block_size ;
XXX is the table name you want to check
7. 如何查看最大会话数?
SELECT * FROM V$PARAMETER WHERE NAME LIKE 'proc%';
SQL>
SQL> show parameter processes
NAME TYPE VALUE
------------------------------------ ------- ------------------------------
aq_tm_processes integer 1
db_writer_processes integer 1
job_queue_processes integer 4
log_archive_max_processes integer 1
processes integer 200
这里为200个用户。
select * from v$license;
其中sessions_highwater纪录曾经到达的最大会话数。
8. 如何查看系统被锁的事务时间?
select * from v$locked_object ;
9. 如何以archivelog的方式运行oracle?
init.ora
log_archive_start = true
RESTART DATABASE
10. 怎么获取有哪些用户在使用数据库?
select username from v$session;
11. 数据表中的字段最大数是多少?
表或视图中的最大列数为 1000。
12. 怎样查得数据库的SID?
select name from v$database;
也可以直接查看 init.ora文件。
13. 如何在Oracle服务器上通过SQLPLUS查看本机IP地址?
select sys_context('userenv','ip_address') from dual;
如果是登陆本机数据库,只能返回127.0.0.1。
14. 在ORACLE TABLE中如何抓取MEMO类型栏位为空的资料记录?
select remark from oms_flowrec where trim(' ' from remark) is not null ;
15. 如何用BBB表的资料去更新AAA表的资料(有关联的字段)?
UPDATE AAA SET BNS_SNM=(SELECT BNS_SNM FROM BBB
WHERE AAA.DPT_NO=BBB.DPT_NO) W
HERE BBB.DPT_NO IS NOT NULL;
16. 何查询每个用户的权限?
SELECT * FROM DBA_SYS_PRIVS;
17. 如何将表移动表空间?
ALTER TABLE TABLE_NAME MOVE TABLESPACE_NAME;
18. 如何将索引移动表空间?
ALTER INDEX INDEX_NAME REBUILD TABLESPACE TABLESPACE_NAME;
19. 查询锁的状况的对象有?
V$LOCK, V$LOCKED_OBJECT, V$SESSION, V$SQLARE
A, V$PROCESS ;
20. 如何解锁?
ALTER SYSTEM KILL SESSION ‘SID,SERIR#’;
21. SQLPLUS下如何修改编辑器?
DEFINE _EDITOR="<编辑器的完整路经>" --必须加上双引号
来定义新的编辑器,也可以把这个写在$ORACLE_HOME/sqlplus/admin/glogin.sql里面使它永久有效。
22. ORACLE产生随机函数是?
DBMS_RANDOM.RANDOM
23. 查询当前用户对像?
SELECT * FROM USER_OBJECTS;
SELECT * FROM DBA_SEGMENTS;
24. 如何获取错误信息?
SELECT * FROM USER_ERRORS;
25. 如何获取链接状况?
SELECT * FROM DBA_DB_LINKS;
26. 查看数据库字符状况?
SELECT * FROM NLS_DATABASE_PARAMETERS;
SELECT * FROM V$NLS_PARAMETERS;
27.. 如何给表、列加注释?
SQL>comment on table表 is '表注释';
注释已创建。
SQL>comment on column表.列 is '列注释';
注释已创建。
SQL> select * from user_tab_comments where comments is not null;
28. 如何查看各个表空间占用磁盘情况?
SQL> col tablespace format a20
SQL> select
b.file_id 文件ID号,
b.tablespace_name 表空间名,
b.bytes 字节数,
(b.bytes-sum(nvl(a.bytes,0)))已使用,
sum(nvl(a.bytes,0)) 剩余空间,
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_id,b.bytes
order by b.file_id
29. 如何才能得知系统当前的SCN号?
select max(ktuxescnw * power(2, 32) + ktuxescnb) from x$ktuxe;
30. 如何在字符串里加回车?
select 'Welcome to visit'||chr(10)||'www.CSDN.NET' from dual ;
31. 如何改变WIN中SQL*Plus启动选项?
SQL*PLUS自身的选项设置,可以在$ORACLE_HOME/sqlplus/admin/glogin.sql中设置。
32. 怎样修改oracel数据库的默认日期?
alter session set nls_date_format='yyyymmddhh24miss';
OR
可以在init.ora中加上一行
nls_date_format='yyyymmddhh24miss'
33. 如何使select语句使查询结果自动生成序号?
select rownum,COL from table;
34. 如何知道数据裤中某个表所在的tablespace?
select tablespace_name from user_tables where table_name='TEST';
select * from user_tables中有个字段TABLESPACE_NAME,(oracle);
select * from dba_segments where …;
35. 怎么在sqlplus下修改procedure?
select line,trim(text) t from user_source where name =’A’ order by line;
36.. 请问如何修改一张表的主键?
alter table aaa
drop constraint aaa_key ;
alter table aaa
add constraint aaa_key primary key(a1,b1) ;
37. 改变数据文件的大小?
用 ALTER DATABASE .... DATAFILE .... ;
手工改变数据文件的大小,对于原来的数据文件有没有损害。
[NextPage]
38. 怎么可以看到数据库有多少个tablespace?
select * from dba_tablespaces;
39. 如何修改oracle数据库的用户连接数?
修改initSID.ora,将process加大,重启数据库。
40. 怎样把“&”放入一条记录中?
insert into a values (translate ('at{&}t','at{}','at'));
41. 怎样查看哪些用户拥有SYSDBA、SYSOPER权限?
SQL>conn sys/change_on_install
SQL>select * from V_$PWFILE_USERS;
42. 如何单独备份一个或多个表?
exp 用户/密码 tables=(表1,…,表2)
43. 如何单独备份一个或多个用户?
exp system/manager owner=(用户1,用户2,…,用户n) file=导出文件
44. 如何对CLOB字段进行全文检索?
SELECT * FROM A WHERE dbms_lob.instr(a.a,'K',1,1)>0;
45. 如何显示当前连接用户?
SHOW USER
46. 如何查看数据文件放置的路径?
col file_name format a50
SQL> select tablespace_name,file_id,bytes/1024/1024,file_name from dba_data_fi
les order by file_id;
47. 如何查看现有回滚段及其状态?
SQL> col segment format a30
SQL> SELECT SEGMENT_NAME,OWNER,TABLESPACE_NAME,SEGMENT_ID,FILE_ID,STATUS FROM
DBA_ROLLBACK_SEGS
48. 如何改变一个字段初始定义的Check范围?
SQL> alter table xxx drop constraint constraint_name;
之后再创建新约束:
SQL> alter table xxx add constraint constraint_name check();
49. Oracle常用系统文件有哪些?
通过以下视图显示这些文件信息:
v$database,v$datafile,v$logfile v$controlfile v$ parameter;
50. 什么是内连接INNER JOIN?
Select a.* from bsempms a,bsdptms b where a.dpt_no=b.dpt_no;
51. 如何外连接?
Select a.* from bsempms a,bsdptms b where a.dpt_no=b.dpt_no(+);
Select a.* from bsempms a,bsdptms b wherea.dpt_no(+)=b.dpt_no;
52. 如何执行脚本SQL文件?
SQL>@$PATH/filename.sql;
53. 如何快速清空一个大表?
SQL>truncate table table_name;
54. 如何查有多少个数据库实例?
SQL>SELECT * FROM V$INSTANCE;
55. 如何查询数据库有多少表?
SQL>select * from all_tables;
56. 如何测试SQL语句执行所用的时间?
SQL>set timing on ;
SQL>select * from tablename;
57. CHR()的反函数是?
ASCII()
SELECT CHAR(65) FROM DUAL;
SELECT ASCII('A') FROM DUAL;
58. 如何实现字符串的连接
SELECT CONCAT(COL1,COL2) FROM TABLE ;
SELECT COL1||COL2 FROM TABLE ;
59. 怎么把select出来的结果导到一个文本文件中?
SQL>SPOOL C:\ABCD.TXT;
SQL>select * from table;
SQL >spool off;
60. 怎样估算SQL执行的I/O数?
SQL>SET AUTOTRACE ON ;
SQL>SELECT * FROM TABLE;
OR
SQL>SELECT * FROM v$filestat ;
可以查看IO数。
61. 如何在sqlplus下改变字段大小?
alter table table_name modify (field_name varchar2(100));
改大行,改小不行(除非都是空的)。
62. 如何查询某天的数据?
select * from table_name where trunc(日期字段)=to_date('2003-05-02','yyyy-mm-
dd');
63. sql 语句如何插入全年日期?
create table BSYEAR (d date);
insert into BSYEAR
select to_date('20030101','yyyymmdd')+rownum-1
from all_objects
where rownum <= to_char(to_date('20031231','yyyymmdd'),'ddd');
65. 如果修改表名?
alter table old_table_name rename to new_table_name;
66. 如何取得命令的返回状态值?
sqlcode=0
67. 如何知道用户拥有的权限?
SELECT * FROM dba_sys_privs ;
68. 如何搜索出前N条记录?
Select * FROM empLOYEE Where ROWNUM < n
orDER BY empno;
69. 如何知道机器上的Oracle支持多少并发用户数?
SQL>conn internal ;
SQL>show parameter processes;
70. 如何统计两个表的记录总数?
select (select count(id) from aa)+(select count(id) from bb)总数 from dual;
71. 怎样用Sql语句实现查找一列中第N大值?
select * from
(select t.*,dense_rank() over (order by sal) rank from employee)
where rank = N;
71. 如何在给现有的日期加上2年?(
select add_months(sysdate,24) from dual;
72. 怎样扩大REDO LOG的大小?
建立一个临时的redolog组,然后切换日志,删除以前的日志,建立新的日志。
73. 如何不同用户间数据导入?
IMP SYSTEM/MANAGER FILE=AA.DMP
FROMUSER=USER_OLD TOUSER=USER_NEW ROWS=Y INDEXES=Y;
74. 如何找数据库表的主键字段的名称?
SQL>Select * FROM user_constraints
Where CONSTRAINT_TYPE='P' and table_name='TABLE_NAME';
75. 两个结果集互加的函数?
SQL>Select * FROM BSEMPMS_OLD INTERSECT Select * FROM BSEMPMS_NEW;
SQL>Select * FROM BSEMPMS_OLD UNION Select * FROM BSEMPMS_NEW
SQL>Select * FROM BSEMPMS_OLD UNION ALL Select * FROM BSEMPMS_NEW;
76. 两个结果集互减的函数?
SQL>Select * FROM BSEMPMS_OLD MINUS Select * FROM BSEMPMS_NEW;
77. 将N秒转换为时分秒格式?
set serverout on
declare
N number := 1000000;
ret varchar2(100);
begin
ret := trunc(n/3600) || '小时' || to_char(to_date(mod(n,3600),'sssss'),'fmmi"分 "ss"秒"') ;
dbms_output.put_line(ret);
end;
78 如何查询做比较大的排序的进程?
Select b.tablespace, b.segfile#, b.segblk#, b.blocks, a.sid, a.serial#, a.username, a.osuser,
a.status
FROM v$session a,v$sort_usage b
Where a.saddr = b.session_addr
orDER BY b.tablespace, b.segfile#, b.segblk#, b.blocks;
79. 如何查询做比较大的排序的进程的SQL语句?
select /*+ orDERED */ sql_text from v$sqltext a
where a.hash_value = (
select sql_hash_value from v$session b
where b.sid = &sid and b.serial# = &serial)
order by piece asc ;
80. 如何查找重复记录?
Select * FROM TABLE_NAME
Where ROWID!=(Select MAX(ROWID) FROM TABLE_NAME D
Where TABLE_NAME.COL1=D.COL1 AND TABLE_NAME.COL2=D.COL2);
81. 如何删除重复记录?
Delete FROM TABLE_NAME
Where ROWID!=(Select MAX(ROWID) FROM TABLE_NAME D
Where TABLE_NAME.COL1=D.COL1 AND TABLE_NAME.COL2=D.COL2);
82. 如何快速编译所有视图?
SQL >SPOOL VIEW1.SQL
SQL >Select ‘Alter VIEW ‘||TNAME||’
COMPILE;’ FROM TAB;
SQL >SPOOL OFF
然后执行VIEW1.SQL即可。
SQL >@VIEW1.SQL;
83. 如何加密ORACLE的存储过程?
下列存储过程内容放在AA.SQL文件中
create or replace procedure testCCB(i in number) as
begin
dbms_output.put_line('输入参数是'||to_char(i));
end;
SQL>wrap iname=a.sql;
PL/SQL Wrapper: Release 8.1.7.0.0 - Production on Tue Nov 27 22:26:48 2001
Copyright (c) oracle Corporation 1993, 2000. All Rights Reserved。
Processing AA.sql to AA.plb
运行AA.plb
SQL> @AA.plb ;
84. 如何监控事例的等待?
select event,sum(decode(wait_Time,0,0,1)) "Prev",
sum(decode(wait_Time,0,1,0)) "Curr",count(*) "Tot"
from v$session_Wait
group by event order by 4;
85. 如何回滚段的争用情况?
select name, waits, gets, waits/gets "Ratio"
from v$rollstat C, v$rollname D
where C.usn = D.usn;
86. 如何监控表空间的 I/O比例?
select B.tablespace_name name, B.file_name "file", A.phyrds pyr,
A.phyblkrd pbr, A.phywrts pyw, A.phyblkwrt pbw
from v$filestat A, dba_data_files B
where A.file# = B.file_id
order by B.tablespace_name;
87. 如何监控文件系统的 I/O比例?
select substr(C.file#,1,2) "#", substr(C.name,1,30) "Name", C.status, C.bytes, D.phyrds,
D.phywrts
from v$datafile C, v$filestat D
where C.file# = D.file#;
88. 如何在某个用户下找所有的索引?
select user_indexes.table_name, user_indexes.index_name,uniqueness, column_name
from user_ind_columns, user_indexes
where user_ind_columns.index_name = user_indexes.index_name
and user_ind_columns.table_name = user_indexes.table_name
order by user_indexes.table_type, user_indexes.table_name,
user_indexes.index_name, column_position;
89. 如何监控 SGA的命中率?
select a.value + b.value "logical_reads", c.value "phys_reads",
round(100 * ((a.value+b.value)-c.value) / (a.value+b.value)) "BUFFER HIT RATIO"
from v$sysstat a, v$sysstat b, v$sysstat c
where a.statistic# = 38 and b.statistic# = 39
and c.statistic# = 40;
90. 如何监控 SGA中字典缓冲区的命中率?
select parameter, gets,Getmisses , getmisses/(gets+getmisses)*100 "miss ratio",
(1-(sum(getmisses)/ (sum(gets)+sum(getmisses))))*100 "Hit ratio"
from v$rowcache
where gets+getmisses <>0
group by parameter, gets, getmisses;
91. 如何监控 SGA中共享缓存区的命中率,应该小于1%?
select sum(pins) "Total Pins", sum(reloads) "Total Reloads",
sum(reloads)/sum(pins) *100 libcache
from v$librarycache;
select sum(pinhits-reloads)/sum(pins) "hit radio",sum(reloads)/sum(pins) "reload percent"
from v$librarycache;
92. 如何显示所有数据库对象的类别和大小?
select count(name) num_instances ,type ,sum(source_size) source_size ,
sum(parsed_size) parsed_size ,sum(code_size) code_size ,sum(error_size) error_size,
sum(source_size) +sum(parsed_size) +sum(code_size) +sum(error_size) size_required
from dba_object_size
group by type order by 2;
93. 监控 SGA中重做日志缓存区的命中率,应该小于1%
Select name, gets, misses, immediate_gets, immediate_misses,
Decode(gets,0,0,misses/gets*100) ratio1,
Decode(immediate_gets+immediate_misses,0,0,
immediate_misses/(immediate_gets+immediate_misses)*100) ratio2
FROM v$latch Where name IN ('redo allocation', 'redo copy');
94. 监控内存和硬盘的排序比率,最好使它小于 .10,增加 sort_area_size
Select name, value FROM v$sysstat Where name IN ('sorts (memory)', 'sorts(disk)');
156. 如何监控当前数据库谁在运行什么SQL语句?
Select osuser, username, sql_text from v$session a, v$sqltext b
where a.sql_address =b.address order by address, piece;
95. 如何监控字典缓冲区?
Select (SUM(PINS - RELOADS)) / SUM(PINS) "LIB CACHE" FROM V$LIBRARYCACHE;
Select (SUM(GETS - GETMISSES - USAGE - FIXED)) / SUM(GETS) "ROW CACHE" FROM V$ROWCACHE;
Select SUM(PINS) "EXECUTIONS", SUM(RELOADS) "CACHE MISSES WHILE EXECUTING" FROM V$LIBRARYCACHE;
后者除以前者,此比率小于1%,接近0%为好。
Select SUM(GETS) "DICTIONARY GETS",SUM(GETMISSES) "DICTIONARY CACHE GET MISSES"
FROM V$ROWCACHE
96. 如何知道当前用户的ID号?
SQL>SHOW USER;
或 SQL>select user from dual;
97. 如何查看碎片程度高的表?
Select segment_name table_name , COUNT(*) extents
FROM dba_segments Where owner NOT IN ('SYS', 'SYSTEM') GROUP BY segment_name
HAVING COUNT(*) = (Select MAX( COUNT(*) ) FROM dba_segments GROUP BY segment_name);
98. 如何知道表在表空间中的存储情况?
select segment_name,sum(bytes),count(*) ext_quan from dba_extents where
tablespace_name='&tablespace_name' and segment_type='TABLE' group by tablespace_name,segment_name;
99. 如何知道索引在表空间中的存储情况?
select segment_name,count(*) from dba_extents
where segment_type='INDEX' and owner='&owner'
group by segment_name;
100. 如何获取时间点的秒的写法?
Select TO_CHAR(SYSDATE,'SS') FROM DUAL;