文章目录
0 登录数据库
-
查看数据库进程
# 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 # 运行一个脚本
[[email protected] ~]$ 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
- 登陆到scott用户的两种方法:
1)直接登陆:sqlplus dcott/tiger
2)切换用户:在数据库中
SQL> conn scott/tiger
Connected.
- 退出
在SQL>exit是退出到oracle用户。
1 Select查询语句
1.1 基本的SELECT子句
功能:可以按col列(column)查看,也可以按row行查看;可以把意思相同的列或行关联,根据关联关系多表查询。
语法:(不区分大小写)
SELECT *|{[DISTINCT] column | expression [alias],...}FROM table;
其中,
*表示所有的列
[DISTINCT] :[关键字]可写可不写 distinct是去重
column:列的名字
expression:表达式
table:表名
; 代表一句话结束
SELECT 标识选择哪些列
FROM 标识从哪个表中选择
在scott用户下登录
- 查看dept所有列(通过* 或将所有列一一列举)
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
- 查看dept表,自定义表达式列
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一页显示200
或打在一行:set lines 200 pages 200
1.2 SQL语句的注意事项
1)SQL语言大小写不敏感
2)SQL可以写在一行或多行
3)关键字不能被缩写也不能分行
4)各子句一般要分行写
5)使用缩进提高语句的可读性
6)SQL语句在SQL Developer中,可以以;终止,当执行多个SQL时,结束分号是必需的。
7)在SQL *Plus中,必须用;结束每条SQL语句
1.3 SELECT语句中的算术表达式
- 使用运算符 (+ - * /)
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.4 NULL
1.4.1 意义
1)NULL是无效的、未指定的、未知的或不可预知的值
2)NULL不是0,也不是空格
1.4.2 计算
包含空值的数学表达式的值都为空值
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.
- nvl函数
将null暂时设定成0,以便计算
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.
1.5 列别名
重命名一个列标题,便于计算,紧跟列名(也可以在列名和别名之间加入关键字‘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
1.6 连接符
把列与列,列与字符连接在一起,用“||”,可以用来合成列。
SQL> select deptno||dname name from dept;
NAME
------------------------------------------------------
10ACCOUNTING
20RESEARCH
30SALES
40OPERATIONS
1.7 字符串
字符串可以是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
- sysdate函数
输出当前系统日期(日-月-年)
SQL> select deptno,sysdate from dept;
DEPTNO SYSDATE
---------- ---------
10 07-MAY-19
20 07-MAY-19
30 07-MAY-19
40 07-MAY-19
- 在数据库里如果想执行Linux的命令,输入 !后再输入命令(!相当于暂时退出,去执行系统的命令)
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_
1.8 distinct去掉重复的行
默认情况下,查询会返回全部的行,包括重复行,通过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.
1.9 DESCRIBE显示表结构
使用DESCRIBE命令显示表结构,或者选择表中的Connections树,并使用列选项卡,查看表结构。
- not null说明不允许有空值。
- type 表示数据类型
number(4)说明最多只可以有四位数字
number(7,2)说明小数点前最多7位,小数点后最多两位 varchar 字符类型(a、b、c、1、2、3等)一个字母占一位,一个汉字占2个字符。
2 过滤数据
过滤和排序数据可以在查询中过滤行,在查询中对行进行排序,在运行时使用“&”字符替换来限制和排序输出。
2.1 WHERE子句(限制行)
作用:限制行、过滤行数据
语法:只显示满足条件的行,把不满足的行都过滤掉。WHERE子句要紧跟FROM子句。
SELECT *|{[DISTINCT] column | expression [alias],...}FROM tableWHERE condition(s);
- 查看所有在部门20的员工:
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
- 查看叫smith的员工的员工号:
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
- upper函数:转换为大写
SQL> select ename,deptno from emp where ename = upper('smith');
ENAME DEPTNO
---------- ----------
SMITH 20
- lower函数:转换列上所有的数据为小写,然后再匹配。所以不论是大写还是小写或是大小写混合的,都可以。(但是不推荐在列上改东西,因为如果这个列很长,那么非常耗时,增加运算量)
2.2 比较运算符
= > >= < <=
<>(不等于)或用 != 或 ^= 表示
BETWEEN…AND… 区间
IN(set) 多个值
LIKE 匹配字符(要加通配符)
IS NULL
- 查询工资小于3000的名单
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.
- 工资在2500~3000的名单:
SQL> select ename,sal from emp where sal between 2500 and 3000;
ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
SCOTT 3000
FORD 3000
- 在81年以后入职的员工
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.
- 根据ASCII码找到k-s之间的名字
SQL> select ename from emp
2 where ename between 'KING' and 'SMITH';
ENAME
----------
SMITH
MARTIN
SCOTT
KING
MILLER
- 使用IN操作符返回smith、scott、king的sal列
SQL> select ename,sal from emp
2 where ename in ('SMITH','SCOTT','KING');
ENAME SAL
---------- ----------
SMITH 800
SCOTT 3000
KING 5000
- 使用LIKE操作符查看名字是S开头的员工的工资
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个或多个字符,_ 代表一个字符
不建议把%放在最前面,这样的性能是最差的
这里的 _ 是有实际意义的,不是通配符,所以要用一个 \ 进行转译
- 使用IS NULL操作符判断空值,查询奖金是未知的员工名称
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.
- IS NOT NULL
SQL> select ename,comm from emp
2 where comm is not null;
ENAME COMM
---------- ----------
ALLEN 300
WARD 500
MARTIN 1400
TURNER 0
2.3 逻辑运算符
- AND 逻辑并,两个条件都为“真”则返回TRUE(并列关系)
SQL> select ename,sal from emp where ename in ('SMITH','SCOTT','KING')
2 and sal > 4000;
ENAME SAL
---------- ----------
KING 5000
- OR 逻辑或,其中一个条件为“真”则返回TRUE
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的执行效率更高
- NOT 逻辑否,如果条件为“假”则返回TRUE
NOT IN
NOT BETWEEN … AND …
NOT LIKE
IS NOT NULL