1. 如何查看表的列名及类型
SQL> select column_name,data_type,data_length from all_tab_columns where owner='SCOTT' and table_name='EMP'; COLUMN_NAME DATA_TYPE DATA_LENGTH
--------------- ---------- -----------
EMPNO NUMBER 22
ENAME VARCHAR2 10
JOB VARCHAR2 9
MGR NUMBER 22
HIREDATE DATE 7
SAL NUMBER 22
COMM NUMBER 22
DEPTNO NUMBER 22 8 rows selected.
2. 如何查看表的约束及建立在何列上
SQL> select * from user_cons_columns; OWNER CONSTRAINT TABLE COLUMN_NAME POSITION
----- ---------- ----- --------------- ----------
SCOTT FK_DEPTNO EMP DEPTNO 1
SCOTT PK_DEPT DEPT DEPTNO 1
SCOTT PK_EMP EMP EMPNO 1
SQL> select owner,constraint_name,constraint_type,table_name from user_constraints; OWNER CONSTRAINT C TABLE
----- ---------- - -----
SCOTT FK_DEPTNO R EMP
SCOTT PK_EMP P EMP
SCOTT PK_DEPT P DEPT
3. 如何查看哪个用户对具体目录的操作权限
SQL> select * from dba_tab_privs where table_name='TMP'; GRANT OWN TAB GRA PRIVILEGE GRA HIE
----- --- --- --- ------------------------ --- ---
SCOTT SYS TMP SYS WRITE NO NO
SCOTT SYS TMP SYS READ NO NO
4. 如何查看用户拥有的对象权限和系统权限
SQL> select * from user_tab_privs; GRANT OWN TAB GRA PRIVILEGE GRA HIE
----- --- --- --- ------------------------- --- ---
SCOTT SYS TMP SYS WRITE NO NO
SCOTT SYS TMP SYS READ NO NO SQL> select * from user_sys_privs; USERNAME PRIVILEGE ADM
------------------------------ ------------------------ ---
SCOTT UNLIMITED TABLESPACE NO
5. 如何判断数据库启动用的是pfile还是spfile
SQL> show parameter spfile
若有值,则是用spfile启动,若为空,则是用pfile启动
6. When SQL*Plus starts, and after CONNECT commands, the site profile (e.g. $ORACLE_HOME/sqlplus/admin/glogin.sql) and the user profile (e.g. login.sql in the working directory) are run. The files may contain SQL*Plus commands.
这两个文件可以自定义SQL*Plus命令
7. sqlplus -L
-L代表Attempts to log on just once, instead of reprompting on error.即只判断一次用户名和密码是否匹配,倘若不匹配,则立即退出,如果不加L,则有三次尝试机会
[oracle@node3 ~]$ sqlplus -L scott/
SQL*Plus: Release 11.2.0.1. Production on Thu Dec ::
Copyright (c) , , Oracle. All rights reserved.
ERROR:
ORA-: invalid username/password; logon denied
SP2-: Unable to connect to Oracle. Exiting SQL*Plus
8. 密码文件
密码文件的默认位置为:$ORACLE_HOME/dbs
密码文件的查找顺序:orapw<sid> --> orapw --> Failure
所以在创建密码文件时filename只能为orapw<sid>或者orapw
9. 查看谁被授予了SYSDBA或者SYSOPER权限
SQL> select * from v$pwfile_users;
10. 查看数据字典中有关权限的表
SQL> select * from dict where table_name like '%PRIVS%';
11. 批量插入
insert all
into test1
into test2
select * from dba_objects;
12. beq
[root@node2 oracle]# ipcs -m ------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 oracle
0x00000000 oracle
0x00000000 oracle
0x00000000 oracle
0xefc07fc8 oracle
bytes列均为4096,而在10g下,owner列只有一个oracle用户,且bytes是一个具体的数值。为什么11g下的bytes为4096且nattch为0,具体原因可参考:http://www.oracledatabase12g.com/archives/11g%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%96%B0%E7%89%B9%E6%80%A7%E7%9A%84internal%E8%A1%A8%E7%8E%B0.html
14. 如何查看当前会话的SID
SQL> select sid from v$mystat where rownum=1;
15. 如何查看连接到当前数据库的会话
SQL> select sid,username,status from v$session;
16. 如何kill掉会话
SQL> select sid,serial# from v$session where username='SCOTT'; SID SERIAL#
---------- ----------
19 109 SQL> alter system kill session '19,109'; System altered.
SQL> select sid,serial#,status from v$session where username='SCOTT'; SID SERIAL# STATUS
---------- ---------- --------
19 109 KILLED
关于kill会话,可参考 http://www.cnblogs.com/kerrycode/p/4034231.html
17. 如何查看oracle用户密码
SQL> select name,password from sys.user$;
18. 不知道某个用户的密码,又想用这个用户做一些操作,又不能去修改掉这个用户的密码
以test用户为例,原来test用户的密码为test
SQL> conn test/test
Connected.
SQL> conn /as sysdba
Connected.
SQL> select name,password from sys.user$ where name='TEST'; NAME PASSWORD
------------------------------ ------------------------------
TEST 7A0F2B316C212D67 1 row selected. SQL> alter user test identified by hello; User altered. SQL> conn test/hello
Connected.
SQL> select * from tab; TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
CHAINED_ROWS TABLE
EMPLOYEES TABLE
EMPLOYEES_TMP TABLE 3 rows selected. SQL> conn /as sysdba
Connected.
SQL> alter user test identified by values '7A0F2B316C212D67'; User altered. SQL> conn test/test
Connected.
19. 如何查看数据库属性
SQL> select property_name,property_value from database_properties;
20. 如何查看特定分区的值
SQL> select count(*) from test partition(p1);
21. 查看tablespace的数据文件
SQL> select tablespace_name,file_name from dba_data_files;
22. 查看用户被赋予的角色
SQL> select * from dba_role_privs where grantee='SCOTT';
23. 在PL/SQL Developer中如何查看表结构
CTRL+表名
24. 如何原样输出字符串
SQL> select q'([@#$%^&*~?/" '])' from dual; Q'([@#$%^&*~?/"
---------------
[@#$%^&*~?/" '] SQL> select q'2I'm Victor2' from dual; Q'2I'MVICT
----------
I'm Victor
注意: 1> Q'后跟起始分隔符,起始分隔符后的字符串原样输出,起始分隔符必须有配对的结束分隔符。
2> 分隔符可以为数字、字母、特殊字符。但'&'不能作为分隔符,因为'&'意思是传入参数。
3> '['、'('、'{'作为分隔符,必须以']'、')'、'}'结束。
25. TWO_TASK
TWO_TASK lets you silently support a tns network connect string, that is, instead of: sqlplus scott/tiger@tnsname
you can: export TWO_TASK=tnsname
sqlplus scott/tiger <<<== that has an implied @tnsname
oracle_sid is used for a local connection - direct to the server - without using a listener the environment variable name is LOCAL on windows NT. set LOCAL=service_name
TWO_TASK是环境变量,通过将其设置为网络字符串,用户通过网络登录时,可不用指定网络字符串,譬如,sqlplus scott/tiger@orcl,通过将TWO_TASK设置为orcl,用sqlplus scott/tiger可直接登录。
注意:Linux和Windows下的设置方式不同,Linux下为export TWO_TASK=orcl,Windows下为set LOCAL=orcl
26. wmsys.wm_concat
该函数可以把列值以","号分隔起来,并显示成一行
SQL> select wmsys.wm_concat(dname) from dept; WMSYS.WM_CONCAT(DNAME)
--------------------------------------------------------------------------------
ACCOUNTING,RESEARCH,SALES,OPERATIONS,IT --将逗号替换为'|'号
SQL> select replace(wmsys.wm_concat(dname),',','|') from dept; REPLACE(WMSYS.WM_CONCAT(DNAME),',','|')
--------------------------------------------------------------------------------
ACCOUNTING|RESEARCH|SALES|OPERATIONS|IT
27. 如何将一行的值转换为列
原始输出如下:
SQL> select * from dept where deptno=10; DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
那么如何将这一行的值转换为列呢?
SQL> with t as
2 ( select deptno,dname,loc from dept where deptno=10)
3 select to_char(deptno) from t
4 union all
5 select dname from t
6 union all
7 select loc from t; TO_CHAR(DEPTNO)
----------------------------------------
10
ACCOUNTING
NEW YORK
28. dual
SQL> create table dual as (select * from dual union all select * from dual); Table created. SQL> select * from dual; D
-
X
X SQL> select sysdate from dual; SYSDATE
---------
10-JUN-15
10-JUN-15
29. 生成AWR报告的三个条件,以test用户为例
grant create session to test;
grant select any dictionary to test;
grant execute on dbms_workload_repository to test;
30. 单引号对应的是chr(39)
39其实是单引号的ASCII码
31. 如何查找堵塞会话并杀掉它
SQL> select sid,serial#,blocking_session from v$session where username='SCOTT'; SID SERIAL# BLOCKING_SESSION
---------- ---------- ----------------
125 7 142 SQL> select sid,serial#,username from V$session where sid=142; SID SERIAL# USERNAME
---------- ---------- ------------------------------
142 27 SYS SQL> alter system kill session '142,27'; System altered.
32. 如何设置sqlprompt
首先利用define命令显示预定义的SQL*Plus变量列表
SQL>define
DEFINE _DATE = "16-JUL-15" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "test" (CHAR)
DEFINE _USER = "SCOTT" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR)
DEFINE _EDITOR = "vim" (CHAR)
DEFINE _O_VERSION = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE = "1102000400" (CHAR)
设置sqlprompt
SQL>set sqlprompt &_USER@&_CONNECT_IDENTIFIER>
SCOTT@test>
33. 如何查看后台进程的作用
SQL> select name,description from v$bgprocess;
34. 如何查看Oracle的版本号
SQL> select * from v$version;
35. Database Sample Schemas
http://docs.oracle.com/cd/E11882_01/server.112/e10831/toc.htm
36. 如何找出给定数之间的最大值和最小值
SQL> select least(1,2,3) from dual; LEAST(1,2,3)
------------
1 SQL> SELECT greatest (1,2,3) from dual ; GREATEST(1,2,3)
---------------
3
37. 用shell脚本获取用户连接数
刚开始是这样:
#!/usr/bin/sh
export ORACLE_HOME=/opt/app/oracle/product/11.2./
export TNS_ADMIN=/home/monitor
sqlplus='/opt/app/oracle/product/11.2.0/bin/sqlplus'
output=`$sqlplus -s test/test@orcl << EOF
set feedback off;
set heading off;
select count(*) from v$session where status='ACTIVE';
EOF`
echo $output
但是执行的时候会报如下错误:
select count(*) from v where status='ACTIVE'
ERROR at line : ORA-: procedure, function, package, or type is not allowed here
原因在于$session当做变量了,当然为空了。
根据网上的方案,$前面加个转义符,即v$session修改为v\$session,但还是没有效果。
解决方法,在前面定义了一个session变量
#!/usr/bin/sh
export ORACLE_HOME=/opt/app/oracle/product/11.2./
export TNS_ADMIN=/home/monitor
session='$session'
sqlplus='/opt/app/oracle/product/11.2.0/bin/sqlplus'
output=`$sqlplus -s audit_vces/audit_vces@orcl << EOF
set feedback off;
set heading off;
select count(*) from v$session where status='ACTIVE';
EOF`
echo $output