1、sql*plus在缓冲区中保存最后一次执行的sql语句或plsql语句
2、启动sql*plus
1、C:\>sqlplus scott/tiger@test1 指定数据库字符串
SQL*Plus: Release 11.2.0.1.0 Production on 星期六 3月 14 01:37:55 2015
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
2、C:\>sqlplus scott/tiger 连接本地数据库
SQL*Plus: Release 11.2.0.1.0 Production on 星期六 3月 14 01:39:06 2015
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
3、C:\>sqlplus scott@test1 隐藏密码
SQL*Plus: Release 11.2.0.1.0 Production on 星期六 3月 14 01:41:21 2015
Copyright (c) 1982, 2010, Oracle. All rights reserved.
输入口令:
连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
3、conenct(conn) 连接数据库
SQL> connect system@test1
输入口令:
已连接。
SQL> conn scott@test1
输入口令:
已连接。
SQL>
4、disconnect(disconn) 断开数据库
SQL> disconnect
从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断开
SQL> conn scott@test1
输入口令:
已连接。
SQL> disconn
从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断开
SQL>
5、describe(desc) 查看表结构
SQL> describe dept
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL> desc dept
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL>
6、编辑sql语句或plsql语句
1、list 列出缓冲区中所有的行
SQL> list
1 select empno,ename
2 sal,comm
3* from emp where deptno=10 当前行
SQL>
2、list x 列出缓冲区中第x行
SQL> list 2
2* sal,comm
SQL>
3、x 将第x行作为当前行
SQL> 1
1* select empno,ename
SQL>
4、apeend text 将text附加到当前行之后
SQL> 1
1* select empno,ename
SQL> append ,deptno
1* select empno,ename,deptno
SQL> list
1 select empno,ename,deptno
2 sal,comm
3* from emp where deptno=10
SQL>
5、change/old/new 将当前行中的old替换为new
SQL> list
1 select empno,ename,deptno
2 sal,comm
3* from emp where deptno=10
SQL> change/10/20
3* from emp where deptno=20
SQL> list
1 select empno,ename,deptno
2 sal,comm
3* from emp where deptno=20
SQL>
6、run(/) 运行缓冲区中保存的sql语句或plsql语句
SQL> list
1 select empno,ename,deptno
2 sal,comm
3* from emp where deptno=20
SQL> /
EMPNO ENAME SAL COMM
---------- ---------- ---------- ----------
7369 SMITH 20
7566 JONES 20
7788 SCOTT 20
7876 ADAMS 20
7902 FORD 20
SQL> run
1 select empno,ename,deptno
2 sal,comm
3* from emp where deptno=20
EMPNO ENAME SAL COMM
---------- ---------- ---------- ----------
7369 SMITH 20
7566 JONES 20
7788 SCOTT 20
7876 ADAMS 20
7902 FORD 20
SQL>
7、del 删除当前行
SQL> list
1 select empno,ename,deptno
2 sal,comm
3* from emp where deptno=20
SQL> del
SQL> list
1 select empno,ename,deptno
2* sal,comm
SQL>
8、del x 删除第x行
SQL> list
1 select empno,ename,deptno
2* sal,comm
SQL> del 1
SQL> list
1* sal,comm
SQL>
9、clear buffer 清空缓冲区
SQL> list
1 create or replace procedure proc1
2 is
3 begin
4 dbms_output.put_line('proc1');
5* end;
SQL> clear buffer
buffer 已清除
SQL> list
SP2-0223: SQL 缓冲区中不存在行。
SQL> /
SP2-0103: SQL 缓冲区中无可运行的程序。
SQL>
7、保存检索并运行文件
1、save filename [append | replace ] 将缓冲区中内容保存到文件
SQL> save d:\1.txt
已创建 file d:\1.txt
SQL> save d:\1.txt append
已将 file 附加到 d:\1.txt
SQL> save d:\1.txt replace
已写入 file d:\1.txt
SQL>
2、get filename 将文件的内容读入缓冲区
SQL> list
SP2-0223: SQL 缓冲区中不存在行。
SQL> get d:\1.txt
1 select empno,ename
2 sal
3* from emp where deptno=10
SQL> /
EMPNO SAL
---------- ----------
7782 CLARK
7839 KING
7934 MILLER
SQL>
3、start filename 运行指定文件
SQL> start d:\1.txt
EMPNO SAL
---------- ----------
7782 CLARK
7839 KING
7934 MILLER
SQL>
4、@ filename 运行指定文件
SQL> @d:\1.txt
EMPNO SAL
---------- ----------
7782 CLARK
7839 KING
7934 MILLER
SQL>
5、edit(ed) 编辑缓冲区中内容
SQL> ed
已写入 file afiedt.buf
1 select empno,ename
2 sal
3* from emp where deptno=20
SQL>
SQL> ed
已写入 file afiedt.buf
1 select empno,ename
2 sal
3* from emp where deptno=30
SQL>
6、edit filename 编辑指定文件内容
7、spool filename
8、spool off
SQL> spool d:\1.txt
SQL> /
EMPNO SAL
---------- ----------
7499 ALLEN
7521 WARD
7654 MARTIN
7698 BLAKE
7844 TURNER
7900 JAMES
已选择6行。
SQL> select * from dept
2 /
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> spool off
spool filename、spool off 输出sql*plus中从spool filename下一行开始到spool off行为止的所有内容
8、格式化查询
1、clear screen 清屏
2、set serverout on|off 是否显示输出
SQL> set serverout on
SQL> exec proc1
proc1
PL/SQL 过程已成功完成。
SQL> set serverout off
SQL> exec proc1
PL/SQL 过程已成功完成。
SQL>
3、set pagesize x 每页显示记录数
SQL> set pagesize 6
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
DEPTNO DNAME LOC
---------- -------------- -------------
40 OPERATIONS BOSTON
SQL> set pagesize 20
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
4、set linesize x 每行显示字符数
5、set echo on|off 执行脚本时是否显示sql语句或plsql语句
SQL> set echo on
SQL> @d:\1.txt
SQL> select * from dept where rownum<=1;
部门号 部门名称 部门位置
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL> set echo off
SQL> @d:\1.txt
部门号 部门名称 部门位置
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
6、heading 列别名
SQL> column deptno heading 部门号
SQL> column dname heading 部门名称
SQL> column loc heading 部门位置
SQL> select * from dept;
部门号 部门名称 部门位置
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
7、format 列宽Ax 数字格式
SQL> column ename format a2
SQL> column sal format 9999.99
SQL> select empno,ename,sal from emp where rownum<=3;
EMPNO EN SAL
---------- -- --------
7369 SM 800.00
IT
H
7499 AL 1600.00
LE
N
7521 WA 1250.00
RD
SQL> column ename format a2 truncate
SQL> select empno,ename,sal from emp where rownum<=3;
EMPNO EN SAL
---------- -- --------
7369 SM 800.00
7499 AL 1600.00
7521 WA 1250.00
SQL>
8、justify left|right|center 标题左对齐、右对齐、居中对齐
SQL> column deptno heading 部门号
SQL> column dname heading 部门名称
SQL> column loc heading 部门位置
SQL> select * from dept where deptno=10;
部门号 部门名称 部门位置
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL> column deptno justify left
SQL> select * from dept where deptno=10;
部门号 部门名称 部门位置
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
9、cloumn column_name clear 清除某列格式
SQL> select * from dept where deptno=10;
部门号 部门名称 部门位置
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL> column deptno clear
SQL> select * from dept where deptno=10;
DEPTNO 部门名称 部门位置
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
10、清除所有列格式
SQL> clear columns
columns 已清除
SQL> select * from dept where deptno=10;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
9、使用变量
1、set define '$' 默认为 & 设置替换变量的定义符号
SQL> set define '$'
SQL> select * from dept where deptno=$v;
输入 v 的值: 10
原值 1: select * from dept where deptno=$v
新值 1: select * from dept where deptno=10
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
2、set verify on|off 是否显示原值、新值
SQL> set verify on
SQL> select * from dept where deptno=$v;
输入 v 的值: 10
原值 1: select * from dept where deptno=$v
新值 1: select * from dept where deptno=10
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL> set verify off
SQL> select * from dept where deptno=$v;
输入 v 的值: 10
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
3、define variable_name 定义变量
SQL> define v=20
SQL> select * from dept where deptno=$v;
DEPTNO DNAME LOC
---------- -------------- -------------
20 RESEARCH DALLAS
4、define 显示所有变量定义
SQL> define
DEFINE _DATE = "14-3月 -15" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "test1" (CHAR)
DEFINE _USER = "SCOTT" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000100" (CHAR)
DEFINE _EDITOR = "Notepad" (CHAR)
DEFINE _O_VERSION = "Oracle Database 11g Enterprise Edition Release 11.2.0.
1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (
CHAR)
DEFINE _O_RELEASE = "1102000100" (CHAR)
DEFINE _RC = "0" (CHAR)
DEFINE V = "20" (CHAR)
SQL>
5、define variable 显示指定变量定义
SQL> define v
DEFINE V = "20" (CHAR)
SQL>
6、undefine variable_name 删除指定变量
SQL> undefine v
SQL> define v
SP2-0135: 符号 v 未定义
SQL>
7、accept variable_name [type] [format format][prompt prompt] [hide]
SQL> accept v number format 99 prompt '请输入部门号' hide
请输入部门号
SQL> select * from dept where deptno=$v;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
SQL>
8、脚本传递
格式:变量 1,2,3
值 1,2,3
脚本语句:select &2,dname from &3 where &1=&4
SQL> @d:\2.txt deptno deptno dept 30
2 /
DEPTNO DNAME
---------- --------------
30 SALES
9、_date 当前日期
10、sql.user 当前用户
11、sql.pno 当前页号
12、left 、center 、right
13、skip x 跳过x行
14、ttitle
15、btitle
SQL> ttitle left '日期:'_date right sql.user skip 2
SQL> btitle center '页:' format '99' sql.pno
SQL> set pagesize 5
SQL> select * from dept;
日期:14-3月 -15 SCOTT
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
页: 1
日期:14-3月 -15 SCOTT
DEPTNO DNAME LOC
---------- -------------- -------------
20 RESEARCH DALLAS
页: 2
日期:14-3月 -15 SCOTT
DEPTNO DNAME LOC
---------- -------------- -------------
30 SALES CHICAGO
页: 3
日期:14-3月 -15 SCOTT
DEPTNO DNAME LOC
---------- -------------- -------------
40 OPERATIONS BOSTON
页: 4
SQL>
17、break on column_name1 nodup|dup|skip page on column_name2 nodup|dup|skip page
SQL> break on deptno on dname
SQL> select a.deptno,b.dname,a.empno,a.ename,a.sal
2 from dept b,emp a where a.deptno=b.deptno
3 order by a.deptno
4 /
DEPTNO DNAME EMPNO ENAME SAL
---------- -------------- ---------- ---------- ----------
10 ACCOUNTING 7782 CLARK 2450
7839 KING 5000
7934 MILLER 1300
20 RESEARCH 7566 JONES 2975
7902 FORD 3000
7876 ADAMS 1100
7369 SMITH 800
7788 SCOTT 3000
30 SALES 7521 WARD 1250
7844 TURNER 1500
7499 ALLEN 1600
DEPTNO DNAME EMPNO ENAME SAL
---------- -------------- ---------- ---------- ----------
30 SALES 7900 JAMES 950
7698 BLAKE 2850
7654 MARTIN 1250
已选择14行。
SQL>
18、compute 聚合函数 of 计算列 on 分组列
SQL> compute min of sal on deptno
SQL> /
DEPTNO DNAME EMPNO ENAME SAL
---------- -------------- ---------- ---------- ----------
10 ACCOUNTING 7782 CLARK 2450
7839 KING 5000
7934 MILLER 1300
********** ************** ----------
minimum 1300
20 RESEARCH 7566 JONES 2975
7902 FORD 3000
7876 ADAMS 1100
7369 SMITH 800
7788 SCOTT 3000
********** ************** ----------
DEPTNO DNAME EMPNO ENAME SAL
---------- -------------- ---------- ---------- ----------
minimum 800
30 SALES 7521 WARD 1250
7844 TURNER 1500
7499 ALLEN 1600
7900 JAMES 950
7698 BLAKE 2850
7654 MARTIN 1250
********** ************** ----------
minimum 950
已选择14行。
SQL>
19、set autocommit on|off 设置是否自动提交事务
20、set heading on|off 是否显示标题
SQL> set heading on
SQL> select * from scott.dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> set heading off
SQL> select * from scott.dept;
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
21、set feedback on|off 是否显示运行结果
SQL> set feedback on
SQL> select * from scott.dept;
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
已选择4行。
SQL> set feedback off
SQL> select * from scott.dept;
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>