查看数据库进程
# ps -ef | grep ora_
只查看oracle数据库进程
连接到数据库软件
$ sqlplus / as sysdba
sqlplus是一个工具,可以把系统连接到数据库
看到以下说明已经连接:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> startup
ORACLE instance started.
Total System Global Area 830930944 bytes
Fixed Size 2257800 bytes
Variable Size 536874104 bytes
Database Buffers 289406976 bytes
Redo Buffers 2392064 bytes
Database mounted.
Database opened.
看到Database opened说明已经连接成功。
SQL> shutdown immediate
SQL> @?/rdbms/admin/utlsampl.sql # 运行一个脚本
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
建立完成以后会自动退出sqlplus,重新登录既可。
[oracle@yr ~]$ sqlplus scott/tiger # 登录到scott用户,密码是tiger sysdba是超级用户,相当于root
SQL*Plus: Release 11.2.0.4.0 Production on Tue Jan 22 10:28:20 2019
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing options
sqlplus dcott/tiger
SQL> conn scott/tiger
Connected.
SQL>exit
是退出到oracle用户。功能:可以按col列(column)查看,也可以按row行查看;可以把意思相同的列或行关联,根据关联关系多表查询。
语法:(不区分大小写)
SELECT *|{[DISTINCT] column | expression [alias],...}FROM table;
其中,
*表示所有的列
[DISTINCT] :[关键字]可写可不写 distinct是去重
column:列的名字
expression:表达式
table:表名
; 代表一句话结束
SELECT 标识选择哪些列
FROM 标识从哪个表中选择
在scott用户下登录
SQL> show user
USER is "SCOTT"
SQL> select * from dept;
DEPTNO DNAME LOC # DEPTNO 部门编号 DNAME 部门名称 LOC 地点
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select dname from dept;
DNAME
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS
SQL> select dname,deptno from dept;
DNAME DEPTNO
-------------- ----------
ACCOUNTING 10
RESEARCH 20
SALES 30
OPERATIONS 40
SQL> select 1+1 from dept;
1+1
----------
2
2
2
2
SQL> select dname,deptno+100,deptno from dept;
DNAME DEPTNO+100 DEPTNO # 表达式也可以在列上进行计算
-------------- ---------- ----------
ACCOUNTING 110 10
RESEARCH 120 20
SALES 130 30
OPERATIONS 140 40
SQL> select dname,deptno+100 as "noplus",deptno from dept;
DNAME noplus DEPTNO
-------------- ---------- ----------
ACCOUNTING 110 10
RESEARCH 120 20
SALES 130 30
OPERATIONS 140 40
SQL> select deptno+100 a from dept;
A # 列名一般都是默认大写的:
----------
110
120
130
140
SQL> select dname,'is',deptno from dept;
DNAME 'I DEPTNO
-------------- -- ----------
ACCOUNTING is 10
RESEARCH is 20
SALES is 30
OPERATIONS is 40
SQL> select dname,'is' as "is",deptno from dept;
DNAME is DEPTNO
-------------- ----- ----------
ACCOUNTING is 10
RESEARCH is 20
SALES is 30
OPERATIONS is 40
set lines 200
线的长度设定为200set pages 200
一页显示200set lines 200 pages 200
1)SQL语言大小写不敏感
2)SQL可以写在一行或多行
3)关键字不能被缩写也不能分行
4)各子句一般要分行写
5)使用缩进提高语句的可读性
6)SQL语句在SQL Developer中,可以以;终止,当执行多个SQL时,结束分号是必需的。
7)在SQL *Plus中,必须用;结束每条SQL语句
SQL> set lines 200 pages 200
SQL> select sal*12 from emp;
SAL*12
----------
9600
19200
15000
35700
15000
34200
29400
36000
60000
18000
13200
11400
36000
15600
14 rows selected.
能进行计算的前提是这一列是数字类型,并且只是临时显示,不会更改表的数据
SQL> select sal,12*sal+100,12*(sal+100) from emp;
SAL 12*SAL+100 12*(SAL+100)
---------- ---------- ------------
800 9700 10800
1600 19300 20400
1250 15100 16200
2975 35800 36900
1250 15100 16200
2850 34300 35400
2450 29500 30600
3000 36100 37200
5000 60100 61200
1500 18100 19200
1100 13300 14400
950 11500 12600
3000 36100 37200
1300 15700 16800
14 rows selected.
1)NULL是无效的、未指定的、未知的或不可预知的值
2)NULL不是0,也不是空格
包含空值的数学表达式的值都为空值
SQL> select comm,2*comm from emp;
COMM 2*COMM
---------- ----------
300 600
500 1000
1400 2800
0 0
14 rows selected.
SQL> select ename,sal,comm,sal*12+comm from emp;
ENAME SAL COMM SAL*12+COMM
---------- ---------- ---------- -----------
SMITH 800
ALLEN 1600 300 19500
WARD 1250 500 15500
JONES 2975
MARTIN 1250 1400 16400
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500 0 18000
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
14 rows selected.
SQL> select ename,sal*12+nvl(comm,0) from emp;
ENAME SAL*12+NVL(COMM,0)
---------- ------------------
SMITH 9600
ALLEN 19500
WARD 15500
JONES 35700
MARTIN 16400
BLAKE 34200
CLARK 29400
SCOTT 36000
KING 60000
TURNER 18000
ADAMS 13200
JAMES 11400
FORD 36000
MILLER 15600
14 rows selected.
重命名一个列标题,便于计算,紧跟列名(也可以在列名和别名之间加入关键字‘AS’),如果它包含空格或特殊字符,或者它是区分大小写的,那么需要双引号
SQL> select dname name from dept;
NAME
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS
SQL> select dname as name from dept;
NAME
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS
# 加上双引号后会区分大小写
SQL> select dname "name" from dept;
name
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS
把列与列,列与字符连接在一起,用“||”,可以用来合成列。
SQL> select deptno||dname name from dept;
NAME
------------------------------------------------------
10ACCOUNTING
20RESEARCH
30SALES
40OPERATIONS
字符串可以是SELECT列表中的一个字符,数字,日期。日期和字符只能在单引号中出现。每当返回一行时,字符串被输出一次。
SQL> select deptno,'aaa' from dept;
DEPTNO 'AA
---------- ---
10 aaa
20 aaa
30 aaa
40 aaa
SQL> select dname || ' is No. ' || deptno name from dept;
NAME
-------------------------------------------------------------
ACCOUNTING is No. 10
RESEARCH is No. 20
SALES is No. 30
OPERATIONS is No. 40
SQL> select deptno,sysdate from dept;
DEPTNO SYSDATE
---------- ---------
10 07-MAY-19
20 07-MAY-19
30 07-MAY-19
40 07-MAY-19
SQL> !ps -ef | grep ora_
oracle 53858 1 0 May06 ? 00:00:24 ora_pmon_nsfcdc
oracle 53860 1 0 May06 ? 00:00:20 ora_psp0_nsfcdc
oracle 53862 1 0 May06 ? 00:00:31 ora_vktm_nsfcdc
oracle 53866 1 0 May06 ? 00:00:04 ora_gen0_nsfcdc
oracle 53868 1 0 May06 ? 00:00:08 ora_diag_nsfcdc
oracle 53870 1 0 May06 ? 00:00:08 ora_dbrm_nsfcdc
oracle 53872 1 0 May06 ? 00:01:23 ora_dia0_nsfcdc
oracle 53874 1 0 May06 ? 00:00:04 ora_mman_nsfcdc
oracle 53876 1 0 May06 ? 00:00:05 ora_dbw0_nsfcdc
oracle 53878 1 0 May06 ? 00:00:05 ora_dbw1_nsfcdc
oracle 53880 1 0 May06 ? 00:00:05 ora_dbw2_nsfcdc
oracle 53882 1 0 May06 ? 00:00:05 ora_dbw3_nsfcdc
oracle 53884 1 0 May06 ? 00:00:05 ora_dbw4_nsfcdc
oracle 53886 1 0 May06 ? 00:00:06 ora_lgwr_nsfcdc
oracle 53888 1 0 May06 ? 00:00:23 ora_ckpt_nsfcdc
oracle 53890 1 0 May06 ? 00:00:05 ora_smon_nsfcdc
oracle 53892 1 0 May06 ? 00:00:02 ora_reco_nsfcdc
oracle 53894 1 0 May06 ? 00:00:17 ora_mmon_nsfcdc
oracle 53896 1 0 May06 ? 00:01:52 ora_mmnl_nsfcdc
oracle 53898 1 0 May06 ? 00:00:02 ora_d000_nsfcdc
oracle 53900 1 0 May06 ? 00:00:01 ora_s000_nsfcdc
oracle 53907 1 0 May06 ? 00:00:04 ora_rvwr_nsfcdc
oracle 53910 1 0 May06 ? 00:00:02 ora_arc0_nsfcdc
oracle 53912 1 0 May06 ? 00:00:02 ora_arc1_nsfcdc
oracle 53914 1 0 May06 ? 00:00:03 ora_arc2_nsfcdc
oracle 53916 1 0 May06 ? 00:00:02 ora_arc3_nsfcdc
oracle 53920 1 0 May06 ? 00:00:02 ora_qmnc_nsfcdc
oracle 53934 1 0 May06 ? 00:00:16 ora_cjq0_nsfcdc
oracle 53942 1 0 May06 ? 00:00:01 ora_q000_nsfcdc
oracle 53944 1 0 May06 ? 00:00:02 ora_q001_nsfcdc
oracle 53969 1 0 May06 ? 00:00:04 ora_smco_nsfcdc
oracle 56592 1 0 08:34 ? 00:00:00 ora_w000_nsfcdc
oracle 56629 56579 0 09:00 pts/8 00:00:00 /bin/bash -c ps -ef | grep ora_
oracle 56631 56629 0 09:00 pts/8 00:00:00 grep ora_
默认情况下,查询会返回全部的行,包括重复行,通过distinct命令去除重复行
SQL> select distinct job from emp;
JOB
---------
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST
SQL> select distinct job,deptno from emp;
JOB DEPTNO
--------- ----------
MANAGER 20
PRESIDENT 10
CLERK 10
SALESMAN 30
ANALYST 20
MANAGER 30
MANAGER 10
CLERK 30
CLERK 20
9 rows selected.
使用DESCRIBE命令显示表结构,或者选择表中的Connections树,并使用列选项卡,查看表结构。
过滤和排序数据可以在查询中过滤行,在查询中对行进行排序,在运行时使用“&”字符替换来限制和排序输出。
作用:限制行、过滤行数据
语法:只显示满足条件的行,把不满足的行都过滤掉。WHERE子句要紧跟FROM子句。
SELECT *|{[DISTINCT] column | expression [alias],...}FROM tableWHERE condition(s);
SQL> select ename,deptno from emp where deptno=20;
ENAME DEPTNO
---------- ----------
SMITH 20
JONES 20
SCOTT 20
ADAMS 20
FORD 20
SQL> select * from emp
2 where deptno = 20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
SQL> select ename,deptno from emp where ename = 'SMITH';
ENAME DEPTNO
---------- ----------
SMITH 20
SQL> select ename,deptno from emp where ename='smith';
no rows selected
字符和日期要包含在单引号中,字符大小写敏感,日期格式敏感,默认的日期格式是DD-MON-RR,number类型不用加引号如果给数字加了’‘,也可以返回,但是增加计算次数,浪费效率
SQL> select ename,hiredate from emp where hiredate = '03-DEC-81';
ENAME HIREDATE
---------- ---------
JAMES 03-DEC-81
FORD 03-DEC-81
SQL> select ename from emp where hiredate = '03-DEC-81';
ENAME
----------
JAMES
FORD
SQL> select ename,deptno from emp where ename = upper('smith');
ENAME DEPTNO
---------- ----------
SMITH 20
= > >= < <=
<>(不等于)或用 != 或 ^= 表示
BETWEEN…AND… 区间
IN(set) 多个值
LIKE 匹配字符(要加通配符)
IS NULL
SQL> select ename,sal from emp
2 where sal<=3000;
ENAME SAL
---------- ----------
SMITH 800
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
CLARK 2450
SCOTT 3000
TURNER 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
13 rows selected.
SQL> select ename,sal from emp where sal between 2500 and 3000;
ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
SCOTT 3000
FORD 3000
SQL> select * from emp
2 where hiredate >= to_date('01-JAN-81','DD-MON-RR');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
13 rows selected.
SQL> select ename,hiredate from emp
2 where hiredate > '01-JAN-81';
ENAME HIREDATE
---------- ---------
ALLEN 20-FEB-81
WARD 22-FEB-81
JONES 02-APR-81
MARTIN 28-SEP-81
BLAKE 01-MAY-81
CLARK 09-JUN-81
SCOTT 19-APR-87
KING 17-NOV-81
TURNER 08-SEP-81
ADAMS 23-MAY-87
JAMES 03-DEC-81
FORD 03-DEC-81
MILLER 23-JAN-82
13 rows selected.
SQL> select ename from emp
2 where ename between 'KING' and 'SMITH';
ENAME
----------
SMITH
MARTIN
SCOTT
KING
MILLER
SQL> select ename,sal from emp
2 where ename in ('SMITH','SCOTT','KING');
ENAME SAL
---------- ----------
SMITH 800
SCOTT 3000
KING 5000
SQL> select ename,sal from emp
2 where ename like 's%';
no rows selected
SQL> select ename,sal from emp
2 where ename like 'S%';
ENAME SAL
---------- ----------
SMITH 800
SCOTT 3000
LIKE运算是选择类似的值,选择条件可以包含字符或数字:% 代表0个或多个字符,_ 代表一个字符
不建议把%放在最前面,这样的性能是最差的
这里的 _ 是有实际意义的,不是通配符,所以要用一个 \ 进行转译
SQL> select ename,comm from emp
2 where comm is null;
ENAME COMM
---------- ----------
SMITH
JONES
BLAKE
CLARK
SCOTT
KING
ADAMS
JAMES
FORD
MILLER
10 rows selected.
SQL> select ename,comm from emp
2 where comm is not null;
ENAME COMM
---------- ----------
ALLEN 300
WARD 500
MARTIN 1400
TURNER 0
SQL> select ename,sal from emp where ename in ('SMITH','SCOTT','KING')
2 and sal > 4000;
ENAME SAL
---------- ----------
KING 5000
SQL> select ename,sal from emp where ename in ('SMITH','SCOTT','KING')
2 or sal > 2000;
ENAME SAL
---------- ----------
SMITH 800
JONES 2975
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
FORD 3000
7 rows selected.
IN相当于OR,但是IN的执行效率更高
在表达式中运算符的优先规则
1 算术运算符
2 连接符
3 比较符
4 IS [NOT] NULL,LIKE,[NOT] IN
5 [NOT] BETWEEN
6 不等于
7 NOT
8 AND
9 OR(所以能用IN的就不用OR)
可以使用括号改变优先级顺序
SQL> select ename,sal,job from emp
2 where job=' SALESMAN '
3 or job = 'MANAGER'
4 and sal > 2500;
ENAME SAL JOB
---------- ---------- ---------
ALLEN 1600 SALESMAN
WARD 1250 SALESMAN
JONES 2975 MANAGER
MARTIN 1250 SALESMAN
BLAKE 2850 MANAGER
TURNER 1500 SALESMAN
6 rows selected.
因为工资大于2500这个条件和job等于经理这个条件是用and连接的,所以会先执行他们,最后的结果是工资大于2500且职位为经理。然后才执行or job=销售
这个sql相当于:
select ename,sal,job from emp
where job = 'MANAGER'
and sal >2500
or job = 'MANAGER'
如果有两个and的情况,就按顺序执行
SQL> select ename,sal,job from emp
2 where (job = 'SALESMAN'
3 or job = 'MANAGER')
4 and sal > 2500;
ENAME SAL JOB
---------- ---------- ---------
JONES 2975 MANAGER
BLAKE 2850 MANAGER
加了括号以后,会先执行括号内的
SQL改写:(不改变结果,只改变效率)
SQL> select ename,sal,job from emp
2 where job in('MANAGER','SALESMAN')
3 and sal > 2500;
ENAME SAL JOB
---------- ---------- ---------
JONES 2975 MANAGER
BLAKE 2850 MANAGER
ASC:升序(默认可以不写)
DESC:降序
ORDER BY子句在SELECT语句的最后
SQL> select ename,hiredate from emp
2 order by hiredate;
ENAME HIREDATE
---------- ---------
SMITH 17-DEC-80
ALLEN 20-FEB-81
WARD 22-FEB-81
JONES 02-APR-81
BLAKE 01-MAY-81
CLARK 09-JUN-81
TURNER 08-SEP-81
MARTIN 28-SEP-81
KING 17-NOV-81
JAMES 03-DEC-81
FORD 03-DEC-81
MILLER 23-JAN-82
SCOTT 19-APR-87
ADAMS 23-MAY-87
14 rows selected.
SQL> select ename,hiredate from emp
2 order by hiredate desc;
ENAME HIREDATE
---------- ---------
ADAMS 23-MAY-87
SCOTT 19-APR-87
MILLER 23-JAN-82
FORD 03-DEC-81
JAMES 03-DEC-81
KING 17-NOV-81
MARTIN 28-SEP-81
TURNER 08-SEP-81
CLARK 09-JUN-81
BLAKE 01-MAY-81
JONES 02-APR-81
WARD 22-FEB-81
ALLEN 20-FEB-81
SMITH 17-DEC-80
14 rows selected.
SQL> select ename,sal s from emp
2 order by s;
ENAME S
---------- ----------
SMITH 800
JAMES 950
ADAMS 1100
WARD 1250
MARTIN 1250
MILLER 1300
TURNER 1500
ALLEN 1600
CLARK 2450
BLAKE 2850
JONES 2975
SCOTT 3000
FORD 3000
KING 5000
14 rows selected.
SQL> select ename,job,sal,hiredate from emp
2 order by 3;
ENAME JOB SAL HIREDATE
---------- --------- ---------- ---------
SMITH CLERK 800 17-DEC-80
JAMES CLERK 950 03-DEC-81
ADAMS CLERK 1100 23-MAY-87
WARD SALESMAN 1250 22-FEB-81
MARTIN SALESMAN 1250 28-SEP-81
MILLER CLERK 1300 23-JAN-82
TURNER SALESMAN 1500 08-SEP-81
ALLEN SALESMAN 1600 20-FEB-81
CLARK MANAGER 2450 09-JUN-81
BLAKE MANAGER 2850 01-MAY-81
JONES MANAGER 2975 02-APR-81
SCOTT ANALYST 3000 19-APR-87
FORD ANALYST 3000 03-DEC-81
KING PRESIDENT 5000 17-NOV-81
14 rows selected.
SQL> select deptno,ename,sal from emp
2 order by deptno,sal desc;
DEPTNO ENAME SAL
---------- ---------- ----------
10 KING 5000
10 CLARK 2450
10 MILLER 1300
20 SCOTT 3000
20 FORD 3000
20 JONES 2975
20 ADAMS 1100
20 SMITH 800
30 BLAKE 2850
30 ALLEN 1600
30 TURNER 1500
30 MARTIN 1250
30 WARD 1250
30 JAMES 950
14 rows selected.
改写
SQL> select deptno,ename,sal from emp
2 order by 1,3 desc;
DEPTNO ENAME SAL
---------- ---------- ----------
10 KING 5000
10 CLARK 2450
10 MILLER 1300
20 SCOTT 3000
20 FORD 3000
20 JONES 2975
20 ADAMS 1100
20 SMITH 800
30 BLAKE 2850
30 ALLEN 1600
30 TURNER 1500
30 MARTIN 1250
30 WARD 1250
30 JAMES 950
14 rows selected.
替换变量的功能
& :“&变量名”
eg:&name;
生命周期:单次引用中,不需要声明,如果替换字符或日期类型,最好用单引号扩起
使用范围:where、order by、列表达式、表名、整个SELECT 语句中
SQL> select ename,sal,deptno from emp
2 where deptno = &no;
Enter value for no: 10
old 2: where deptno = &no
new 2: where deptno = 10
ENAME SAL DEPTNO
---------- ---------- ----------
CLARK 2450 10
KING 5000 10
MILLER 1300 10
SQL> l
1 select ename,sal,deptno from emp
2* where deptno = &no
SQL> /
Enter value for no: 20
old 2: where deptno = &no
new 2: where deptno = 20
ENAME SAL DEPTNO
---------- ---------- ----------
SMITH 800 20
JONES 2975 20
SCOTT 3000 20
ADAMS 1100 20
FORD 3000 20
变量获取的是用户传进来的值。解析需要消耗CPU,有了变量就减少解析,不会过多地消耗CPU。
替代变量可以替代以下内容:
SQL> select ename,sal,deptno from emp
2 where deptno = &no
3 order by &a;
Enter value for no: 20
old 2: where deptno = &no
new 2: where deptno = 20
Enter value for a: 2
old 3: order by &a
new 3: order by 2
ENAME SAL DEPTNO
---------- ---------- ----------
SMITH 800 20
ADAMS 1100 20
JONES 2975 20
FORD 3000 20
SCOTT 3000 20
SQL> select ename,sal,&col_name from &table_name
2 where deptno = &no
3 order by &a;
Enter value for col_name: deptno,job
Enter value for table_name: emp
old 1: select ename,sal,&col_name from &table_name
new 1: select ename,sal,deptno,job from emp
Enter value for no: 20
old 2: where deptno = &no
new 2: where deptno = 20
Enter value for a: 1
old 3: order by &a
new 3: order by 1
ENAME SAL DEPTNO JOB
---------- ---------- ---------- ---------
ADAMS 1100 20 CLERK
FORD 3000 20 ANALYST
JONES 2975 20 MANAGER
SCOTT 3000 20 ANALYST
SMITH 800 20 CLERK
SQL> select &col_name
2 from &table_name
3 where deptno = &no
4 order by &a;
Enter value for col_name: ename,job,sal
old 1: select &col_name
new 1: select ename,job,sal
Enter value for table_name: emp
old 2: from &table_name
new 2: from emp
Enter value for no: 30
old 3: where deptno = &no
new 3: where deptno = 30
Enter value for a: 2
old 4: order by &a
new 4: order by 2
ENAME JOB SAL
---------- --------- ----------
JAMES CLERK 950
BLAKE MANAGER 2850
TURNER SALESMAN 1500
WARD SALESMAN 1250
ALLEN SALESMAN 1600
MARTIN SALESMAN 1250
6 rows selected.
SQL> select &col_name
2 from &table_name
3 where &where
4 order by &a;
Enter value for col_name: ename,sal,job,deptno
old 1: select &col_name
new 1: select ename,sal,job,deptno
Enter value for table_name: emp
old 2: from &table_name
new 2: from emp
Enter value for where: deptno in (10,20)
old 3: where &where
new 3: where deptno in (10,20)
Enter value for a: 2,4
old 4: order by &a
new 4: order by 2,4
ENAME SAL JOB DEPTNO
---------- ---------- --------- ----------
SMITH 800 CLERK 20
ADAMS 1100 CLERK 20
MILLER 1300 CLERK 10
CLARK 2450 MANAGER 10
JONES 2975 MANAGER 20
SCOTT 3000 ANALYST 20
FORD 3000 ANALYST 20
KING 5000 PRESIDENT 10
8 rows selected.
“&&变量名”
eg:&&name;
生命周期:整个会话(session连接),不需要声明
SQL> select ename,sal,deptno
2 from &&table_name
3 where deptno = &no
4 order by &a;
Enter value for table_name: emp
old 2: from &&table_name
new 2: from emp
Enter value for no: 30
old 3: where deptno = &no
new 3: where deptno = 30
Enter value for a: deptno
old 4: order by &a
new 4: order by deptno
ENAME SAL DEPTNO
---------- ---------- ----------
ALLEN 1600 30
WARD 1250 30
MARTIN 1250 30
BLAKE 2850 30
TURNER 1500 30
JAMES 950 30
6 rows selected.
SQL> /
old 2: from &&table_name
new 2: from emp
Enter value for no: 10
old 3: where deptno = &no
new 3: where deptno = 10
Enter value for a: 2
old 4: order by &a
new 4: order by 2
ENAME SAL DEPTNO
---------- ---------- ----------
MILLER 1300 10
CLARK 2450 10
KING 5000 10
define即host变量主要作用是起到一个替代变量的作用,是主机环境可以和oracle进行交互的一个媒介。通过define定义host变量的时候必须指定变量名和变量的值,如果变量名已经存在于host变量中,则自动覆盖,这个值不可以指定类型,一律按char存储。define变量需要预先声明,使用时用&引用声明的变量,生命周期是整个会话,但只在当前session环境中有效。
define variable_name = value
SQL> define num = 1;
SQL> define emp_deptno = 20
# 显示指定的define变量值和类型
SQL> define num;
DEFINE NUM = "1" (CHAR)
SQL> define emp_deptno;
DEFINE EMP_DEPTNO = "20" (CHAR)
# 显示所有的define变量值和类型
SQL> define;
DEFINE _DATE = "14-MAY-19" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "nsfcdc" (CHAR)
DEFINE _USER = "SCOTT" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000400" (CHAR)
DEFINE _EDITOR = "ed" (CHAR)
DEFINE _O_VERSION = "Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE = "1102000400" (CHAR)
DEFINE EMP_DEPTNO = "20" (CHAR)
DEFINE NUM = "1" (CHAR)
SQL> select ename,sal,empno from emp
2 where deptno=&emp_deptno;
old 2: where deptno=&emp_deptno
new 2: where deptno=20
ENAME SAL EMPNO
---------- ---------- ----------
SMITH 800 7369
JONES 2975 7566
SCOTT 3000 7788
ADAMS 1100 7876
FORD 3000 7902
# 通过undefine命令撤销
SQL> undefine emp_deptno;
set define on;
set define off;
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
BONUS TABLE
DEPT TABLE
EMP TABLE
SALGRADE TABLE
T1 TABLE
TEST_DEPT TABLE
TEST_EMP TABLE
7 rows selected.
不预先声明初始化define变量,直接在sql中在字符串前面加&符号,会提示输入变量的值,然后替换。这种情况下是临时的变量,define查不到。
经常使用host变量的场合是在写脚本的时候,脚本有些地方经常变化,其他地方固定,那么可以使用&引用。