数据库编程1 Oracle 过滤 函数 分组 外连接 自连接

本文谢绝转载原文来自http://990487026.blog.51cto.com

<大纲>

数据库编程1 Oracle 过滤 函数 分组 外连接 自连接
	本文实验基于的数据表:
	winsows安装好Oracle11g之后,开始实验
	SQLplus 登陆 ORacle
	sqlplus 退出的方式
	查看用户之下有什么表
	查看表的所有记录,不区分大小写
	设置SQLplus行宽,页宽,列宽:
	清屏命令
	select as 语法
		1,as别名的使用
		2,没有引号带有空格的别名,无法识别:
		3,带有空格的别名解决办法:
		年收入抛出问题,年收入显示为空
		解决办法,nvl(a,b)函数
	1,NULL空值 任何数与NULL运算都是NULL
	2,空值不是空 null != null
	ed修改上一条命令:保存退出,/开始执行
	where 过滤,查询奖金为空的员工:
	||连接符,与虚表
	dual虚表
	DISTINCT 删除重复行
	DISTINCT修饰多个字段时,当两个数据完全一样才认为是重复的
	SQLplus工具的 help
	edit的帮助:
	Oracle系统常用的参数
	默认的时间显示
	临时修改系统时间的显示方式:
	where过滤字句 > 大于
	where过滤字句between and
	where过滤字句between A  and B,A 必须小于等于 B
	where过滤字句 in
	where过滤字句 not in
	where过滤字句 not in 与null在一起,受影响
	where like模糊查询,查询员工姓名S开头的信息:
	where like模糊查询,查询员工姓名是4个字母
	where like模糊查询,转义字符
	where order by 工资排序,默认升序
	where order by 别名
		order by 遇到null
		1,任何表达式与null运算都为空
		2,null!=null
	nulls last 把null放在最后
	order修饰多个列,desc的作用范围只是最近的哪一个!
	count函数

	字符函数
		大小写转换函数	LOWER	UPPER	INITCAP
	字符串连接:
		concat
		substr(a,b) 从a中第b位开始取字符串
		length 字符数
		lengthb 字节数
		instr求子串的位置
		lpad,rpad左填充右填充:
		trim去除前后指定字符:
		replace 字符串替换

	数字函数
		round 四舍五入
		tuec截断函数
		mod求余函数
		to_char格式化输出
		两个日期相减,返回日期之间相差的天数
		精确显示员工入职月数,时间运算months_between
		add_months()向指定的日期中加若干的月数
		3,1隐式转换,字符串转日期
		3,1显示转换,字符串转日期
		字符串与字符串比较
		to_char()对数字转字符
		to_number()字符转数字
	通用函数
		nvl2(a,b,c)当a为null时返回c,否则返回b
		nullif(a,b)当a,b相等时返回NULL,否则返回a

	案例:总裁涨1000,经理涨800,其他涨500
		方法1,SQL规范方法:
		方法2:Oracle专有方法

	组函数:	avg sum max min count 
		组函数与null在一起
		组函数会自动过滤空值:
			修正:

	分组数据:
		求出各个部门的平均工资

	分组最难的地方:
	having 分组过滤:
	求10号部门的平均工资的两种方法
		1,先分组再过滤
		2,先过滤再分组
	SQL优化问题:
	多表查询:
	[等值连接],显示员工信息,员工号,姓名,月薪
	[不等值连接]显示员工信息,员工号,姓名,月薪,薪水级别
	多表查询4,求每个部门的人数:(需要分组)
	[少了一个部门,这个方法有漏洞,因为40号部门没有员工] 
	[外连接]技术引出:
	右外连接,+在等号的左边
	左外连接,+在等号的右边

	[难点]:[自连接]

	显示老板的信息:
	[优化显示]
	[再优化]
	[难点]:查询每个部门的人数

[说明]本文实验基于以下数据表

dept表;
emp表;
salgrade表;
______________________________________________________________________________
SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
______________________________________________________________________________

SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 17-12月-80            800                 20
      7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300      30
      7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500      30
      7566 JONES                MANAGER                  7839 02-4月 -81           2975                 20
      7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400      30
      7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                 30
      7782 CLARK                MANAGER                  7839 09-6月 -81           2450                 10
      7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                 20
      7839 KING                 PRESIDENT                     17-11月-81           5000                 10
      7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0      30
      7876 ADAMS                CLERK                    7788 23-5月 -87           1100                 20
      7900 JAMES                CLERK                    7698 03-12月-81            950                 30
      7902 FORD                 ANALYST                  7566 03-12月-81           3000                 20
      7934 MILLER               CLERK                    7782 23-1月 -82           1300                 10
         1 tom_abc                                                                 8000                    10

已选择15行。
______________________________________________________________________________

SQL> select * from salgrade;

     GRADE      LOSAL      HISAL
---------- ---------- ----------
         1        700       1200
         2       1201       1400
         3       1401       2000
         4       2001       3000
         5       3001       9999
______________________________________________________________________________

winsows安装好Oracle11g之后,

实验基于[OracleServiceORCL]后台服务进程的开启:

以超级用户方式登录:

C:\Users\VMware>sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on 星期三 8月 24 22:03:26 2016
Copyright (c) 1982, 2010, Oracle.  All rights reserved.

SQL> connect /as sysdba
已连接。 
SQL>

1,SQLplus 登陆 ORacle:

winsows安装好Oracle11g之后,修改scott的密码

C:\Users\VMware>sqlplus

请输入用户名:  scott
输入口令:tiger
ERROR:
ORA-28001: the password has expired
更改 scott 的口令
新口令:
重新键入新口令:
口令已更改

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

输入账户和密码,直接登录

C:\Users\VMware>sqlplus scott/11

SQL*Plus: Release 11.2.0.1.0 Production on 星期三 8月 24 22:00:50 2016

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

sqlplus 退出的方式:

C:\Users\VMware>sqlplus scott/11
SQL> quit
SQL> exit

查看用户之下有什么表:

tab是关键字
C:\Users\VMware>sqlplus scott/11
SQL> select * from tab;

TNAME                                                        TABTYPE
------------------------------------------------------------ --------------
 CLUSTERID
----------
BONUS                                                        TABLE

DEPT                                                         TABLE

EMP                                                          TABLE

TNAME                                                        TABTYPE
------------------------------------------------------------ --------------
 CLUSTERID
----------
SALGRADE                                                     TABLE

SQL>


查看表的所有记录,不区分大小写

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON

SQL>

查看表结构:

SQL> desc dept;
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 DEPTNO                                    NOT NULL NUMBER(2)
 DNAME                                              VARCHAR2(14)
 LOC                                                VARCHAR2(13)

SQL>

设置SQLplus行宽,页宽,列宽:

设置行宽前:
SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
       SAL       COMM     DEPTNO
---------- ---------- ----------
      7369 SMITH                CLERK                    7902 17-12月-80
       800                    20

      7499 ALLEN                SALESMAN                 7698 20-2月 -81
      1600        300         30

      7521 WARD                 SALESMAN                 7698 22-2月 -81
      1250        500         30

     EMPNO ENAME                JOB                       MGR HIREDATE
---------- -------------------- ------------------ ---------- --------------
       SAL       COMM     DEPTNO
---------- ---------- ----------
      7566 JONES                MANAGER                  7839 02-4月 -81
      2975                    20
设置行宽后:
SQL> set linesize 999;
SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 17-12月-80            800                 20
      7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300      30
      7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500      30
      7566 JONES                MANAGER                  7839 02-4月 -81           2975                 20
      7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400      30
      7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                 30
      7782 CLARK                MANAGER                  7839 09-6月 -81           2450                 10
      7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                 20
      7839 KING                 PRESIDENT                     17-11月-81           5000                 10
      7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0      30
      7876 ADAMS                CLERK                    7788 23-5月 -87           1100                 20

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
      7900 JAMES                CLERK                    7698 03-12月-81            950                 30
      7902 FORD                 ANALYST                  7566 03-12月-81           3000                 20
      7934 MILLER               CLERK                    7782 23-1月 -82           1300                 10

已选择14行。

SQL>      

设置页宽:     
SQL> set pagesize 999;
SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 17-12月-80            800                 20
      7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300      30
      7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500      30
      7566 JONES                MANAGER                  7839 02-4月 -81           2975                 20
      7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400      30
      7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                 30
      7782 CLARK                MANAGER                  7839 09-6月 -81           2450                 10
      7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                 20
      7839 KING                 PRESIDENT                     17-11月-81           5000                 10
      7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0      30
      7876 ADAMS                CLERK                    7788 23-5月 -87           1100                 20
      7900 JAMES                CLERK                    7698 03-12月-81            950                 30
      7902 FORD                 ANALYST                  7566 03-12月-81           3000                 20
      7934 MILLER               CLERK                    7782 23-1月 -82           1300                 10

已选择14行。

设置数字的列宽:
oracle 9代表占位:
SQL> set linesize 199;
SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 17-12月-80            800                 20
      7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300      30
      7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500      30
      7566 JONES                MANAGER                  7839 02-4月 -81           2975                 20
      7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400      30
      7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                 30
      7782 CLARK                MANAGER                  7839 09-6月 -81           2450                 10
      7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                 20
      7839 KING                 PRESIDENT                     17-11月-81           5000                 10
      7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0      30
      7876 ADAMS                CLERK                    7788 23-5月 -87           1100                 20
      7900 JAMES                CLERK                    7698 03-12月-81            950                 30
      7902 FORD                 ANALYST                  7566 03-12月-81           3000                 20
      7934 MILLER               CLERK                    7782 23-1月 -82           1300                 10

已选择14行。

SQL> col empno for 9999999999999999999
SQL> select * from emp;

               EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
-------------------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
                7369 SMITH                CLERK                    7902 17-12月-80            800                20
                7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300         30
                7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500         30
                7566 JONES                MANAGER                  7839 02-4月 -81           2975                20
                7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400         30
                7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                30
                7782 CLARK                MANAGER                  7839 09-6月 -81           2450                10
                7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                20
                7839 KING                 PRESIDENT                     17-11月-81           5000                10
                7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0         30
                7876 ADAMS                CLERK                    7788 23-5月 -87           1100                20
                7900 JAMES                CLERK                    7698 03-12月-81            950                30
                7902 FORD                 ANALYST                  7566 03-12月-81           3000                20
                7934 MILLER               CLERK                    7782 23-1月 -82           1300                10

已选择14行。

设置字符的列宽:60个字符的宽度
SQL> col ename for a60;
SQL> select * from emp;

               EMPNO ENAME                                                        JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
-------------------- ------------------------------------------------------------ ------------------ ---------- -------------- ---------- ---------- ----------
                7369 SMITH                                                        CLERK                    7902 17-12月-80            800                    20
                7499 ALLEN                                                        SALESMAN                 7698 20-2月 -81           1600        300         30
                7521 WARD                                                         SALESMAN                 7698 22-2月 -81           1250        500         30
                7566 JONES                                                        MANAGER                  7839 02-4月 -81           2975                    20
                7654 MARTIN                                                       SALESMAN                 7698 28-9月 -81           1250       1400         30
                7698 BLAKE                                                        MANAGER                  7839 01-5月 -81           2850                    30
                7782 CLARK                                                        MANAGER                  7839 09-6月 -81           2450                    10
                7788 SCOTT                                                        ANALYST                  7566 19-4月 -87           3000                    20
                7839 KING                                                         PRESIDENT                 17-11月-81       5000                    10
                7844 TURNER                                                       SALESMAN                 7698 08-9月 -81           1500          0         30
                7876 ADAMS                                                        CLERK                    7788 23-5月 -87           1100                    20
                7900 JAMES                                                        CLERK                    7698 03-12月-81            950                    30
                7902 FORD                                                         ANALYST                  7566 03-12月-81           3000                    20
                7934 MILLER                                                       CLERK                    7782 23-1月 -82           1300                    10

已选择14行。

恢复,便于后面做实验:
SQL> col ename for a20;
SQL> col empno for 999999
SQL> select * from emp;

  EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
   7369 SMITH                CLERK                    7902 17-12月-80            800                 20
   7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300      30
   7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500      30
   7566 JONES                MANAGER                  7839 02-4月 -81           2975                 20
   7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400      30
   7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                 30
   7782 CLARK                MANAGER                  7839 09-6月 -81           2450                 10
   7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                 20
   7839 KING                 PRESIDENT                     17-11月-81           5000                 10
   7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0      30
   7876 ADAMS                CLERK                    7788 23-5月 -87           1100                 20
   7900 JAMES                CLERK                    7698 03-12月-81            950                 30
   7902 FORD                 ANALYST                  7566 03-12月-81           3000                 20
   7934 MILLER               CLERK                    7782 23-1月 -82           1300                 10

已选择14行。

清屏命令:

SQL> host cls

select 语法:

select 参与运算语法:

SQL> select * from emp;

  EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
   7369 SMITH                CLERK                    7902 17-12月-80            800                 20
   7499 ALLEN                SALESMAN                 7698 20-2月 -81           1600        300      30
   7521 WARD                 SALESMAN                 7698 22-2月 -81           1250        500      30
   7566 JONES                MANAGER                  7839 02-4月 -81           2975                 20
   7654 MARTIN               SALESMAN                 7698 28-9月 -81           1250       1400      30
   7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                 30
   7782 CLARK                MANAGER                  7839 09-6月 -81           2450                 10
   7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                 20
   7839 KING                 PRESIDENT                     17-11月-81           5000                 10
   7844 TURNER               SALESMAN                 7698 08-9月 -81           1500          0      30
   7876 ADAMS                CLERK                    7788 23-5月 -87           1100                 20
   7900 JAMES                CLERK                    7698 03-12月-81            950                 30
   7902 FORD                 ANALYST                  7566 03-12月-81           3000                 20
   7934 MILLER               CLERK                    7782 23-1月 -82           1300                 10
SQL> select ename, empno, job, sal, sal*12 from emp;

ENAME                  EMPNO JOB                       SAL     SAL*12
-------------------- ------- ------------------ ---------- ----------
SMITH                   7369 CLERK                     800       9600
ALLEN                   7499 SALESMAN                 1600      19200
WARD                    7521 SALESMAN                 1250      15000
JONES                   7566 MANAGER                  2975      35700
MARTIN                  7654 SALESMAN                 1250      15000
BLAKE                   7698 MANAGER                  2850      34200
CLARK                   7782 MANAGER                  2450      29400
SCOTT                   7788 ANALYST                  3000      36000
KING                    7839 PRESIDENT                5000      60000
TURNER                  7844 SALESMAN                 1500      18000
ADAMS                   7876 CLERK                    1100      13200
JAMES                   7900 CLERK                     950      11400
FORD                    7902 ANALYST                  3000      36000
MILLER                  7934 CLERK                    1300      15600

select as 语法

as 可以省略
冒号可以省略
别名之间不能有空格

1,as别名的使用
SQL> select ename as "员工编号", empno "编号", job 工作 , sal 月薪 , sal*12 年薪  from emp;

员工编号                   编号 工作                     月薪       年薪
-------------------- ---------- ------------------ ---------- ----------
SMITH                      7369 CLERK                     800       9600
ALLEN                      7499 SALESMAN                 1600      19200
WARD                       7521 SALESMAN                 1250      15000
JONES                      7566 MANAGER                  2975      35700
MARTIN                     7654 SALESMAN                 1250      15000
BLAKE                      7698 MANAGER                  2850      34200
CLARK                      7782 MANAGER                  2450      29400
SCOTT                      7788 ANALYST                  3000      36000
KING                       7839 PRESIDENT                5000      60000
TURNER                     7844 SALESMAN                 1500      18000
ADAMS                      7876 CLERK                    1100      13200
JAMES                      7900 CLERK                     950      11400
FORD                       7902 ANALYST                  3000      36000
MILLER                     7934 CLERK                    1300      15600

已选择14行。

2,没有引号带有空格的别名,无法识别:

SQL> select ename as "员工编号", empno "编号", job 工作 , sal 月 薪 , sal*12 年薪  from emp;
select ename as "员工编号", empno "编号", job 工作 , sal 月 薪 , sal*12 年薪  from emp
                                                                       *
第 1 行出现错误:
ORA-00923: 未找到要求的 FROM 关键字

3,带有空格的别名解决办法:

SQL> select ename as "员工编号", empno "编号", job 工作 , sal "月  薪" , sal*12 年薪  from emp;

员工编号                   编号 工作                   月  薪       年薪
-------------------- ---------- ------------------ ---------- ----------
SMITH                      7369 CLERK                     800       9600
ALLEN                      7499 SALESMAN                 1600      19200
WARD                       7521 SALESMAN                 1250      15000
JONES                      7566 MANAGER                  2975      35700
MARTIN                     7654 SALESMAN                 1250      15000
BLAKE                      7698 MANAGER                  2850      34200
CLARK                      7782 MANAGER                  2450      29400
SCOTT                      7788 ANALYST                  3000      36000
KING                       7839 PRESIDENT                5000      60000
TURNER                     7844 SALESMAN                 1500      18000
ADAMS                      7876 CLERK                    1100      13200
JAMES                      7900 CLERK                     950      11400
FORD                       7902 ANALYST                  3000      36000
MILLER                     7934 CLERK                    1300      15600

已选择14行。

年收入抛出问题,年收入显示为空

SQL> select ename as "员工编号", empno "编号", job 工作 , sal "月  薪" , sal*12+comm 年收入  from emp;

员工编号                   编号 工作                   月  薪     年收入
-------------------- ---------- ------------------ ---------- ----------
SMITH                      7369 CLERK                     800
ALLEN                      7499 SALESMAN                 1600      19500
WARD                       7521 SALESMAN                 1250      15500
JONES                      7566 MANAGER                  2975
MARTIN                     7654 SALESMAN                 1250      16400
BLAKE                      7698 MANAGER                  2850
CLARK                      7782 MANAGER                  2450
SCOTT                      7788 ANALYST                  3000
KING                       7839 PRESIDENT                5000
TURNER                     7844 SALESMAN                 1500      18000
ADAMS                      7876 CLERK                    1100
JAMES                      7900 CLERK                     950
FORD                       7902 ANALYST                  3000
MILLER                     7934 CLERK                    1300



1,NULL空值 任何数与NULL运算都是NULL

2,空值不是空 null != null


解决办法,nvl(a,b)函数,当a是null是返回b,b可以是数字/字符串

SQL> select ename,empno, job, sal, nvl(sal*12+comm,0) from emp;

ENAME                     EMPNO JOB                       SAL NVL(SAL*12+COMM,0)
-------------------- ---------- ------------------ ---------- ------------------
SMITH                      7369 CLERK                     800                  0
ALLEN                      7499 SALESMAN                 1600              19500
WARD                       7521 SALESMAN                 1250              15500
JONES                      7566 MANAGER                  2975                  0
MARTIN                     7654 SALESMAN                 1250              16400
BLAKE                      7698 MANAGER                  2850                  0
CLARK                      7782 MANAGER                  2450                  0
SCOTT                      7788 ANALYST                  3000                  0
KING                       7839 PRESIDENT                5000                  0
TURNER                     7844 SALESMAN                 1500              18000
ADAMS                      7876 CLERK                    1100                  0
JAMES                      7900 CLERK                     950                  0
FORD                       7902 ANALYST                  3000                  0
MILLER                     7934 CLERK                    1300                  0

已选择14行。

SQL> select ename,empno, job, sal, nvl(sal*12+comm,0) "年收入" from emp;

ENAME                     EMPNO JOB                       SAL     年收入
-------------------- ---------- ------------------ ---------- ----------
SMITH                      7369 CLERK                     800          0
ALLEN                      7499 SALESMAN                 1600      19500
WARD                       7521 SALESMAN                 1250      15500
JONES                      7566 MANAGER                  2975          0
MARTIN                     7654 SALESMAN                 1250      16400
BLAKE                      7698 MANAGER                  2850          0
CLARK                      7782 MANAGER                  2450          0
SCOTT                      7788 ANALYST                  3000          0
KING                       7839 PRESIDENT                5000          0
TURNER                     7844 SALESMAN                 1500      18000
ADAMS                      7876 CLERK                    1100          0
JAMES                      7900 CLERK                     950          0
FORD                       7902 ANALYST                  3000          0
MILLER                     7934 CLERK                    1300          0

已选择14行。

SQL> select ename,empno, job, sal, nvl(sal*12+comm,0)as  "年收入" from emp;

ENAME                     EMPNO JOB                       SAL     年收入
-------------------- ---------- ------------------ ---------- ----------
SMITH                      7369 CLERK                     800          0
ALLEN                      7499 SALESMAN                 1600      19500
WARD                       7521 SALESMAN                 1250      15500
JONES                      7566 MANAGER                  2975          0
MARTIN                     7654 SALESMAN                 1250      16400
BLAKE                      7698 MANAGER                  2850          0
CLARK                      7782 MANAGER                  2450          0
SCOTT                      7788 ANALYST                  3000          0
KING                       7839 PRESIDENT                5000          0
TURNER                     7844 SALESMAN                 1500      18000
ADAMS                      7876 CLERK                    1100          0
JAMES                      7900 CLERK                     950          0
FORD                       7902 ANALYST                  3000          0
MILLER                     7934 CLERK                    1300          0

已选择14行。

ed修改上一条命令:保存退出,/开始执行

SQL> ed
已写入 file afiedt.buf

  1* select ename,empno, job, sal, nvl(sal*12+comm,0)as  "年 入" from emp
SQL> /

ENAME                     EMPNO JOB                       SAL      年 入
-------------------- ---------- ------------------ ---------- ----------
SMITH                      7369 CLERK                     800          0
ALLEN                      7499 SALESMAN                 1600      19500
WARD                       7521 SALESMAN                 1250      15500
JONES                      7566 MANAGER                  2975          0
MARTIN                     7654 SALESMAN                 1250      16400
BLAKE                      7698 MANAGER                  2850          0
CLARK                      7782 MANAGER                  2450          0
SCOTT                      7788 ANALYST                  3000          0
KING                       7839 PRESIDENT                5000          0
TURNER                     7844 SALESMAN                 1500      18000
ADAMS                      7876 CLERK                    1100          0
JAMES                      7900 CLERK                     950          0
FORD                       7902 ANALYST                  3000          0
MILLER                     7934 CLERK                    1300          0

已选择14行。

where 过滤,查询奖金为空的员工:

SQL> select * from emp where comm is null;

     EMPNO ENAME                JOB                       MGR HIREDATE              SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- -------------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 17-12月-80            800                 20
      7566 JONES                MANAGER                  7839 02-4月 -81           2975                 20
      7698 BLAKE                MANAGER                  7839 01-5月 -81           2850                 30
      7782 CLARK                MANAGER                  7839 09-6月 -81           2450                 10
      7788 SCOTT                ANALYST                  7566 19-4月 -87           3000                 20
      7839 KING                 PRESIDENT                     17-11月-81           5000                 10
      7876 ADAMS                CLERK                    7788 23-5月 -87           1100                 20
      7900 JAMES                CLERK                    7698 03-12月-81            950                 30
      7902 FORD                 ANALYST                  7566 03-12月-81           3000                 20
      7934 MILLER               CLERK                    7782 23-1月 -82           1300                 10

已选择10行。

||连接符,与虚表

SQL> select ‘Hello ‘ || ‘World‘ from emp;

‘HELLO‘||‘WORLD‘
----------------------
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

已选择14行。


dual虚表:

SQL> select ‘Hello ‘ || ‘World‘ from dual;

‘HELLO‘||‘WORLD‘
----------------------
Hello World

利用伪表,显示系统时间

SQL> select sysdate from dual;

SYSDATE
--------------
25-8月 -16

DISTINCT 删除重复行,显示部门个数:

SQL> select DISTINCT deptno from emp;

    DEPTNO
----------
        30
        20
        10


DISTINCT修饰多个字段时,当两个数据完全一样才认为是重复的

SQL> select deptno,job from emp;

    DEPTNO JOB
---------- ------------------
        20 CLERK
        30 SALESMAN
        30 SALESMAN
        20 MANAGER
        30 SALESMAN
        30 MANAGER
        10 MANAGER
        20 ANALYST
        10 PRESIDENT
        30 SALESMAN
        20 CLERK
        30 CLERK
        20 ANALYST
        10 CLERK

已选择14行。

SQL> select DISTINCT deptno,job from emp;

    DEPTNO JOB
---------- ------------------
        20 CLERK
        30 SALESMAN
        20 MANAGER
        30 CLERK
        10 PRESIDENT
        30 MANAGER
        10 CLERK
        10 MANAGER
        20 ANALYST

已选择9行。

SQLplus工具的 help

SQL> help topic

 Help is available on the following topics:

/
@
@@
ACCEPT
APPEND
ARCHIVE LOG
ATTRIBUTE
BREAK
BTITLE
CHANGE
CLEAR
COLUMN
COMPUTE
CONNECT
COPY
DEFINE
DEL
DESCRIBE
DISCONNECT
EDIT
EXECUTE
EXIT
GET
HELP
HOST
INDEX
INPUT
LIST
MENU
PASSWORD
PAUSE
PRINT
PROMPT
QUIT
RECOVER
REMARK
REPFOOTER
REPHEADER
RESERVED WORDS (PL/SQL)
RESERVED WORDS (SQL)
RUN
SAVE
SET
SHOW
SHUTDOWN
SPOOL
SQLPLUS
START
STARTUP
STORE
TIMING
TOPICS
TTITLE
UNDEFINE
VARIABLE
WHENEVER OSERROR
WHENEVER SQLERROR
XQUERY

SQL>


edit的帮助:

SQL> ? edit

 EDIT
 ----

 Invokes an operating system text editor on the contents of the
 specified file or on the contents of the SQL buffer. The buffer
 has no command history list and does not record SQL*Plus commands.

 ED[IT] [file_name[.ext]]

SQL>

Oracle系统常用的参数

SQL> set pagesize 299
SQL> set linesize 299
SQL> select * from v$nls_parameters;

PARAMETER                         VALUE                                                                                       
----------------------------- ----------- 
NLS_LANGUAGE                      SIMPLIFIED CHINESE
NLS_TERRITORY                     CHINA
NLS_CURRENCY                      ¥
NLS_ISO_CURRENCY                  CHINA
NLS_NUMERIC_CHARACTERS            .,
NLS_CALENDAR                      GREGORIAN
NLS_DATE_FORMAT                   DD-MON-RR
NLS_DATE_LANGUAGE                 SIMPLIFIED CHINESE
NLS_CHARACTERSET                  AL32UTF8
NLS_SORT                          BINARY
NLS_TIME_FORMAT                   HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT              DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT                HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT           DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY                 ¥
NLS_NCHAR_CHARACTERSET            AL16UTF16
NLS_COMP                          BINARY
NLS_LENGTH_SEMANTICS              BYTE
NLS_NCHAR_CONV_EXCP               FALSE

已选择19行。


默认的时间显示

SQL> select sysdate from dual;

SYSDATE
--------------
25-8月 -16


临时修改系统时间的显示方式:

SQL> alter session set NLS_DATE_FORMAT=‘yyyy-mm-dd‘;

会话已更改。

SQL> select sysdate from dual;

SYSDATE
----------
2016-08-25

SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 1980-12-17        800                    20
      7499 ALLEN                SALESMAN                 7698 1981-02-20       1600        300         30
      7521 WARD                 SALESMAN                 7698 1981-02-22       1250        500         30
      7566 JONES                MANAGER                  7839 1981-04-02       2975                    20
      7654 MARTIN               SALESMAN                 7698 1981-09-28       1250       1400         30
      7698 BLAKE                MANAGER                  7839 1981-05-01       2850                    30
      7782 CLARK                MANAGER                  7839 1981-06-09       2450                    10
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
      7839 KING                 PRESIDENT                     1981-11-17       5000                    10
      7844 TURNER               SALESMAN                 7698 1981-09-08       1500          0         30
      7876 ADAMS                CLERK                    7788 1987-05-23       1100                    20
      7900 JAMES                CLERK                    7698 1981-12-03        950                    30
      7902 FORD                 ANALYST                  7566 1981-12-03       3000                    20
      7934 MILLER               CLERK                    7782 1982-01-23       1300                    10

已选择14行。

where过滤字句 > 大于:

查询入职比80年1月1号晚的员工:日期格式是敏感的

SQL> select * from emp where hiredate > ‘1980-01-01‘;

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 1980-12-17        800                    20
      7499 ALLEN                SALESMAN                 7698 1981-02-20       1600        300         30
      7521 WARD                 SALESMAN                 7698 1981-02-22       1250        500         30
      7566 JONES                MANAGER                  7839 1981-04-02       2975                    20
      7654 MARTIN               SALESMAN                 7698 1981-09-28       1250       1400         30
      7698 BLAKE                MANAGER                  7839 1981-05-01       2850                    30
      7782 CLARK                MANAGER                  7839 1981-06-09       2450                    10
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
      7839 KING                 PRESIDENT                     1981-11-17       5000                    10
      7844 TURNER               SALESMAN                 7698 1981-09-08       1500          0         30
      7876 ADAMS                CLERK                    7788 1987-05-23       1100                    20
      7900 JAMES                CLERK                    7698 1981-12-03        950                    30
      7902 FORD                 ANALYST                  7566 1981-12-03       3000                    20
      7934 MILLER               CLERK                    7782 1982-01-23       1300                    10

已选择14行。

where过滤字句between and,查询工资在1000 ~ 2000之间的

SQL> select * from emp where sal between 1000 and 2000;

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7499 ALLEN                SALESMAN                 7698 1981-02-20       1600        300         30
      7521 WARD                 SALESMAN                 7698 1981-02-22       1250        500         30
      7654 MARTIN               SALESMAN                 7698 1981-09-28       1250       1400         30
      7844 TURNER               SALESMAN                 7698 1981-09-08       1500          0         30
      7876 ADAMS                CLERK                    7788 1987-05-23       1100                    20
      7934 MILLER               CLERK                    7782 1982-01-23       1300                    10

已选择6行。

where过滤字句between A  and B,A 必须小于等于 B

SQL> select * from emp where sal between 9000 and 200;

未选定行

SQL>

where过滤字句 and

SQL> select * from emp where sal <=9000 and sal > 2000;

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7566 JONES                MANAGER                  7839 1981-04-02       2975                    20
      7698 BLAKE                MANAGER                  7839 1981-05-01       2850                    30
      7782 CLARK                MANAGER                  7839 1981-06-09       2450                    10
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
      7839 KING                 PRESIDENT                     1981-11-17       5000                    10
      7902 FORD                 ANALYST                  7566 1981-12-03       3000                    20

已选择6行。


where过滤字句 in

SQL> select * from emp where deptno in (10,20);

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 1980-12-17        800                    20
      7566 JONES                MANAGER                  7839 1981-04-02       2975                    20
      7782 CLARK                MANAGER                  7839 1981-06-09       2450                    10
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
      7839 KING                 PRESIDENT                     1981-11-17       5000                    10
      7876 ADAMS                CLERK                    7788 1987-05-23       1100                    20
      7902 FORD                 ANALYST                  7566 1981-12-03       3000                    20
      7934 MILLER               CLERK                    7782 1982-01-23       1300                    10

已选择8行。

where过滤字句 not in

SQL> select * from emp where deptno not in (10,20);

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7499 ALLEN                SALESMAN                 7698 1981-02-20       1600        300         30
      7521 WARD                 SALESMAN                 7698 1981-02-22       1250        500         30
      7654 MARTIN               SALESMAN                 7698 1981-09-28       1250       1400         30
      7698 BLAKE                MANAGER                  7839 1981-05-01       2850                    30
      7844 TURNER               SALESMAN                 7698 1981-09-08       1500          0         30
      7900 JAMES                CLERK                    7698 1981-12-03        950                    30

已选择6行。

where过滤字句 not in 与null在一起,受影响

SQL> select * from emp where deptno in (10,20);

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 1980-12-17        800                    20
      7566 JONES                MANAGER                  7839 1981-04-02       2975                    20
      7782 CLARK                MANAGER                  7839 1981-06-09       2450                    10
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
      7839 KING                 PRESIDENT                     1981-11-17       5000                    10
      7876 ADAMS                CLERK                    7788 1987-05-23       1100                    20
      7902 FORD                 ANALYST                  7566 1981-12-03       3000                    20
      7934 MILLER               CLERK                    7782 1982-01-23       1300                    10

已选择8行。

SQL> select * from emp where deptno in (10,20,null);

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 1980-12-17        800                    20
      7566 JONES                MANAGER                  7839 1981-04-02       2975                    20
      7782 CLARK                MANAGER                  7839 1981-06-09       2450                    10
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
      7839 KING                 PRESIDENT                     1981-11-17       5000                    10
      7876 ADAMS                CLERK                    7788 1987-05-23       1100                    20
      7902 FORD                 ANALYST                  7566 1981-12-03       3000                    20
      7934 MILLER               CLERK                    7782 1982-01-23       1300                    10

已选择8行。

SQL> select * from emp where deptno not in (10,20,null);

未选定行

where like模糊查询,查询员工姓名S开头的信息:

SQL> select * from emp;

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 1980-12-17        800                    20
      7499 ALLEN                SALESMAN                 7698 1981-02-20       1600        300         30
      7521 WARD                 SALESMAN                 7698 1981-02-22       1250        500         30
      7566 JONES                MANAGER                  7839 1981-04-02       2975                    20
      7654 MARTIN               SALESMAN                 7698 1981-09-28       1250       1400         30
      7698 BLAKE                MANAGER                  7839 1981-05-01       2850                    30
      7782 CLARK                MANAGER                  7839 1981-06-09       2450                    10
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
      7839 KING                 PRESIDENT                     1981-11-17       5000                    10
      7844 TURNER               SALESMAN                 7698 1981-09-08       1500          0         30
      7876 ADAMS                CLERK                    7788 1987-05-23       1100                    20
      7900 JAMES                CLERK                    7698 1981-12-03        950                    30
      7902 FORD                 ANALYST                  7566 1981-12-03       3000                    20
      7934 MILLER               CLERK                    7782 1982-01-23       1300                    10

已选择14行。

SQL> select * from emp where ename like ‘S%‘;

     EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ---------- ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 1980-12-17        800                    20
      7788 SCOTT                ANALYST                  7566 1987-04-19       3000                    20
时间: 2024-10-01 02:44:34

数据库编程1 Oracle 过滤 函数 分组 外连接 自连接的相关文章

数据库编程2 Oracle 过滤 函数 分组 外连接 自连接

[本文谢绝转载原文来自http://990487026.blog.51cto.com] 续:数据库编程1 Oracle 过滤 函数 分组 外连接 自连接 where like模糊查询,查询员工姓名是4个字母 SQL> select * from emp where ename like '____';      EMPNO ENAME                JOB                       MGR HIREDATE          SAL       COMM    

数据库编程3 Oracle 子查询 insert update delete 事务 回收站 字段操作 企业级项目案例

[本文谢绝转载原文来自http://990487026.blog.51cto.com] <大纲> 数据库编程3 Oracle 子查询 insert update delete 事务 回收站 字段操作 企业级项目案例 实验所用数据表 子查询,解决一步不能求解 查询工资比scott高的员工信息: 子查询知识体系搭建: 解释3,查询部门是sales的员工信息: 方法1:子查询 [方法2]:多表: 优化考虑: 解释4[select],只能放单行子查询 解释4[from] 考题:显示员工姓名,薪水 解释

数据库编程4 Oracle Pro*C/C++开发

[本文谢绝转载原文来自http://990487026.blog.51cto.com] 1,上次难点复习 1 求所有部门的平均奖金 2 求各部门的平均薪水 3 求各部门每个工种的平均薪水 4 求各部门每个工种大于2000的平均薪水 5 求10号部门的平均工资(2种写法) 6 创建一个学生表 7 并向表中插入一条数据 8 列出不是管理层的员工: 9 工资最高前10名的员工信息(rownum是一个属性值) 10 找到各部门大于本部门平均薪水的员工: 11 创建索引 Linux 环境 Oracle  

mysql数据库编程,内置函数,存储过程(循环插入)

mysql编程注释:行:# 或者 -- 定义变量set 变量名 =变量名注意:为了区分系统变量和字段 在加一个@标识符例如   set @who ='韩当'; 使用select 可以获得当前变量的值 select 10,15,20 into @a,@b,@c; 注意:=应该是赋值,但是在select语句内,就成了关系等于,使用专门的赋值运算符:=同样使用与set 1.作用域, 用户定义的函数,是全局的(函数内可以),村子局部作用域变量,函数内定义的变量2.有效期,回话结束(连接结束) 内置函数

内连接 外连接 自连接 交叉连接

======================================================== My SQL如下: ======================================================== 1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students和courses表中学生标识号相同的所有行. 2.外联接.外联接

【C/C++学院】(24)Oracle数据库编程--管理oracle

一.启动和停止oracle 停止和启动oracle需要切换到oracle用户才可以,其他用户都没有权限启动和停止oracle(包括root也没有权限). 1.运行sqlplus但不登录到oracle: sqlplus /nolog 2.以系统管理员的权限连接到oracle服务器 conn /as sysdba; 3.启动oracle服务 startup 4.停止oracle服务 shutdown immediate; oracle的listener 相当于oracle的TCPserver,lie

php操作Mysql 以及封装常用的函数 用外连接连接3个表的案例

<?php header("content-type;text/html;charset=utf-8"); //数据库连接define('DB_HOST','localhost');define('DB_USER','root');define('DB_PASSWORD','admin');define('DB_NAME','php2016');//连接mysql服务器function _connect(){ //global 表示全局变量的意思,意图是将此变量在函数外部也能访问

18 12 06 sql 的 基本语句 查询 条件查询 逻辑运算符 模糊查询 范围查询 排序 聚合函数 分组 分页 连接查询 自关联 子查询

-- 数据的准备 -- 创建一个数据库 create database python_test charset=utf8; -- 使用一个数据库 use python_test; -- 显示使用的当前数据是哪个? select database(); -- 创建一个数据表 -- students表 create table students( id int unsigned primary key auto_increment not null, name varchar(20) default

VB.net数据库编程(03):一个SQLserver连接查询的简单样例

这个样例,因为在ADO.net入门已经专门学了,再次进行复习 一下. 主要掌握连接字串的情况. 过程就是: 1.引用System.Data.SqlClient.而Access中引用 的是System.Data.OleDB.所以是有差别的 2.相关连接.适配器.数据集. 3.DataGridView绑定显示. 连接字串例如以下: Data Source                     IP地址或计算名(数据库所在位置的),假设是本地计算机能够用(local)或直接用.来取代,或者本地IP: