1、连接(connect可简写为CONN)
CONN[ECT] {username | /}[@connect_identifier] [edition={edition_name | DATABASE_DEFAULT}] [AS {SYSOPER | SYSDBA}]
SQLPLUS下操作系统验证登录DBA角色:
connect / AS sysdba
用户名/密码登录格式:
connect AS a USER CONN <logon> / <password> @ <instance>
@后为配置好的监听连接标识符,如:
connect joesmith/password@orcl
注意:密码中含有@、/等特殊字符的需要用双引号把密码引起来
直接使用IP/SID登录方式:
--查看函数 describe可简写为DESC 16:23:22 SCOTT@orcl> describe fn_getname FUNCTION fn_getname RETURNS VARCHAR2 --查看存储过程 16:36:48 SCOTT@orcl> DESC TEST_001 PROCEDURE TEST_001 --查看表 16:36:59 SCOTT@orcl> DESC emp 名称 是否为空? 类型 ----------------------------------------------------- -------- --------------- EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) --查看同义词 16:40:15 SCOTT@orcl> CREATE SYNONYM syn_emp for emp; 同义词已创建。 已用时间: 00: 00: 00.03 16:40:53 SCOTT@orcl> desc syn_emp 名称 是否为空? 类型 ----------------------------------------------------- -------- ------------ EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2)
--设置列分隔符 (SET colsep 字符或字符串) SET colsep ',' 16:45:19 SCOTT@orcl> SELECT * FROM EMP WHERE ROWNUM<2; EMPNO,ENAME ,JOB , MGR,HIREDATE , SAL, COMM, DEPTNO ----------,----------,---------,----------,--------------,----------,----------,---------- 1214,<null> ,<null> ,<null> ,<null> , 1.21E+03,<null> ,<null> 已选择 1 行。 --设置表头是否显示(set head ON/OFF) 16:48:31 SCOTT@orcl> show head heading ON 16:48:36 SCOTT@orcl> SELECT * FROM EMP WHERE ROWNUM<2; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- -------------- ---------- ---------- ---------- 1214 <null> <null> <null> <null> 1.21E+03 <null> <null> 已选择 1 行。 已用时间: 00: 00: 00.00 16:49:06 SCOTT@orcl> set head off 16:49:13 SCOTT@orcl> SELECT * FROM EMP WHERE ROWNUM<2; 1214 <null> <null> <null> <null> 1.21E+03 <null> <null> 已选择 1 行。 --设置行的大小(SET linesize <integer>) 16:52:49 SCOTT@orcl> SET linesize 20 16:53:18 SCOTT@orcl> select text from user_source where rownum<2; TEXT -------------------- package csharp_inter face is 已选择 1 行。 已用时间: 00: 00: 00.00 16:53:19 SCOTT@orcl> SET linesize 100 16:53:26 SCOTT@orcl> / TEXT ------------------------------------------ package csharp_interface is 已选择 1 行。 已用时间: 00: 00: 00.00 --设置执行时间显示 (SET timing ON) 16:54:41 SCOTT@orcl> SET timing off 16:54:44 SCOTT@orcl> select 1 from dual; 1 ---------- 1 已选择 1 行。 16:54:54 SCOTT@orcl> SET timing on 16:54:59 SCOTT@orcl> select 1 from dual; 1 ---------- 1 已选择 1 行。 已用时间: 00: 00: 00.01
数字、字符等输出格式控制:
--设置显示列宽(字符类型的) COL object_name FORMAT a30 --设置显示列宽及格式(数字类型的) COL sal FORMAT $99,999 16:06:29 SCOTT@orcl> select sal from emp; SAL -------- $1,212 $4,500 --负数显示格式 - COL sal FORMAT 9999MI 16:10:34 SCOTT@orcl> select -4321.66 sal from dual; SAL ----- 4322- --负数显示格式 <> COL sal FORMAT 9999PR 16:12:01 SCOTT@orcl> select -4321.66 sal from dual; SAL ------ <4322> --数字精度控制 COL sal FORMAT 9999D99 16:12:50 SCOTT@orcl> select 654321.66666 sal from dual; SAL -------- ######## 已选择 1 行。 已用时间: 00: 00: 00.00 16:13:00 SCOTT@orcl> select 6521.6666 sal from dual; SAL -------- 6521.67 已选择 1 行。 --科学计数法显示数字 COL sal FORMAT 9999.99EEEE 16:13:46 SCOTT@orcl> select 6521.6666 sal from dual; SAL ---------- 6.52E+03 已选择 1 行。 --数字显示为日期格式 COL test FORMAT DATE 16:19:32 SCOTT@orcl> select 1 test from dual; TEST -------- 01/01/12
4、查看参数值 (show parameter)
--查看以UNDO开头的初始化参数的值 17:27:08 SYS@orcl> show parameter undo NAME TYPE VALUE ------------------------------------ ----------- --------- undo_management string AUTO undo_retention integer 900 undo_tablespace string UNDOTBS1
5、SPOOL
SPOOL 为SQLPLUS下使用的输出工具,类似如CMD下的ECHO >>文件名,不过SPOOL要强大的多.
1.输出 SPOOL 文件路径
如:
spool c:\temp\test1.txt
...
--各种操作命令后
SPOOL OFF
打开文件TEST1.TXT,发现各种操作命令及显示结果都已经保存到此文件.
6、其他命令
--显示当前用户 17:32:30 SCOTT@orcl> show user USER 为 "SCOTT" --显示oracle服务器版本号 (show release) 17:28:52 SYS@orcl> show release release 1102000100 --显示缺省实例的连接标识符 (show instance) 17:23:26 SCOTT@orcl> show instance instance "local" --显示归档日志路径 (show logsource) 17:26:36 SYS@orcl> show logsource logsource "" --显示错误信息 (show errors) 17:15:51 SCOTT@orcl> CREATE PROCEDURE test123 IS 17:16:03 2 BEGIN 17:16:07 3 ifasdfn; 17:16:08 4 END; 17:16:08 5 / 警告: 创建的过程带有编译错误。 已用时间: 00: 00: 00.04 17:16:09 SCOTT@orcl> show errors PROCEDURE TEST123 出现错误: LINE/COL ERROR -------- ---------------------------------------- 3/3 PL/SQL: Statement ignored 3/3 PLS-00201: 必须声明标识符 'IFASDFN' 17:16:14 SCOTT@orcl> --显示所有配置参数 (show all) 17:14:24 SCOTT@orcl> show all appinfo 为 OFF 并且已设置为 "SQL*Plus" arraysize 15 autocommit OFF autoprint OFF autorecovery OFF autotrace OFF blockterminator "." (hex 2e) btitle OFF 为下一条 SELECT 语句的前几个字符 cmdsep OFF colsep " " compatibility version NATIVE concat "." (hex 2e) copycommit 0 COPYTYPECHECK 为 ON define "&" (hex 26) describe DEPTH 1 LINENUM OFF INDENT ON echo OFF editfile "afiedt.buf" embedded OFF escape OFF escchar OFF exitcommit ON 用于 1 或更多行的 FEEDBACK ON flagger OFF flush ON heading ON headsep "|" (hex 7c) instance "local" linesize 100 lno 7 loboffset 1 logsource "" long 100000 longchunksize 80 markup HTML OFF HEAD "<style type='text/css'> body {font:10pt Arial,Helvetica,sans-serif; color:blac k; background:White;} p {font:10pt Arial,Helvetica,sans-serif; color:black; background:White;} table ,tr,td {font:10pt Arial,Helvetica,sans-serif; color:Black; background:#f7f7e7; padding:0px 0px 0px 0 px; margin:0px 0px 0px 0px;} th {font:bold 10pt Arial,Helvetica,sans-serif; color:#336699; backgroun d:#cccc99; padding:0px 0px 0px 0px;} h1 {font:16pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; border-bottom:1px solid #cccc99; margin-top:0pt; margin-bottom:0pt; padding :0px 0px 0px 0px;- } h2 {font:bold 10pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; margi n-top:4pt; margin-bottom:0pt;} a {font:9pt Arial,Helvetica,sans-serif; color:#663300; background:#ff ffff; margin-top:0pt; margin-bottom:0pt; vertical-align:top;}</style><title>SQL*Plus Report</title>" BODY "" TABLE "border='1' width='90%' align='center' summary='Script output'" SPOOL OFF ENTMAP ON P REFORMAT OFF newpage 1 null "<null>" numformat "" numwidth 10 pagesize 1000 PAUSE 为 OFF pno 1 recsep WRAP recsepchar " " (hex 20) release 1102000100 repfooter OFF 为 NULL repheader OFF 为 NULL securedcol is OFF serveroutput ON SIZE 1000000 FORMAT WORD_WRAPPED shiftinout INVISIBLE showmode OFF spool ON sqlblanklines OFF sqlcase MIXED sqlcode 0 sqlcontinue "> " sqlnumber ON sqlpluscompatibility 11.2.0 sqlprefix "#" (hex 23) sqlprompt "_user@orcl> " sqlterminator ";" (hex 3b) suffix "sql" tab ON termout OFF timing ON trimout ON trimspool ON ttitle OFF 为下一条 SELECT 语句的前几个字符 underline "-" (hex 2d) USER 为 "SCOTT" verify OFF wrap : 将换至下一行 errorlogging is OFF --设置SQL提示符 (SET sqlprompt 字符串) 16:58:31 SCOTT@orcl> SET sqlprompt sqlpro_test> 17:06:04 sqlpro_test> --清空屏幕显示 可简写为CL SCR clear screen --调用系统编辑器 自动打开前一个命令进行编辑 简写为ED edit