Oracle数据库之基本查询

时间:2022-04-29 07:41:52

oracle安装参照: Oracle数据库之安装教程

Oracle数据库总结:

Oracle数据库之基本查询
Oracle数据库之过滤和排序
Oracle数据库之单行函数
Oracle数据库之多行函数
Oracle数据库之多表查询

注意: 这里用到的数据库是上面安装教程里面的scott/tiger和hr/hr这两个用户。

SQL> –清屏
SQL> host cls

SQL> –当前用户
SQL> show user
USER 为 “SCOTT”

SQL> –当前用户下的表
SQL> select * from tab;

TNAME                          TABTYPE  CLUSTERID                               
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE

SQL> –emp表的结构
SQL> desc emp

名称                                      是否为空? 类型
----------------------------------------- -------- ----------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)

SQL> –查询所有员工的所有信息
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

7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400
30

7698 BLAKE MANAGER 7839 01-5月 -81 2850
30


EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
DEPTNO
----------
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


EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
DEPTNO
----------
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


EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
DEPTNO
----------
7902 FORD ANALYST 7566 03-12月-81 3000
20

7934 MILLER CLERK 7782 23-1月 -82 1300
10

已选择 14 行。

SQL> –设置行宽
SQL> show linesize
linesize 80
SQL> set linesize 150

SQL> –设置列宽
SQL> col ename for a8
SQL> col sal for 9999
SQL> /

     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> select empno,ename,job,mgr,hiredate,sal,comm,deptno
2 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

SQL> –查询员工信息:员工号 姓名 月薪
SQL> select empno,ename,sal
2 from emp;

   EMPNO ENAME      SAL                                                                                                                             
---------- -------- -----
7369 SMITH 800
7499 ALLEN 1600
7521 WARD 1250
7566 JONES 2975
7654 MARTIN 1250
7698 BLAKE 2850
7782 CLARK 2450
7788 SCOTT 3000
7839 KING 5000
7844 TURNER 1500
7876 ADAMS 1100

EMPNO ENAME SAL
---------- -------- -----
7900 JAMES 950
7902 FORD 3000
7934 MILLER 1300

SQL> –查询员工信息:员工号 姓名 月薪 年薪
SQL> select empno,ename,sal,sal*12
2 form emp;
form emp
*
第 2 行出现错误:
ORA-00923: 未找到要求的 FROM 关键字

SQL> –c命令 change
SQL> 2
2* form emp
SQL> c /form/from
2* from emp
SQL> /

     EMPNO ENAME      SAL     SAL*12      

   7369 SMITH      800       9600                                                                                                                  
7499 ALLEN 1600 19200
7521 WARD 1250 15000
7566 JONES 2975 35700
7654 MARTIN 1250 15000
7698 BLAKE 2850 34200
7782 CLARK 2450 29400
7788 SCOTT 3000 36000
7839 KING 5000 60000
7844 TURNER 1500 18000
7876 ADAMS 1100 13200

EMPNO ENAME SAL SAL*12
---------- -------- ----- ----------
7900 JAMES 950 11400
7902 FORD 3000 36000
7934 MILLER 1300 15600

SQL> –查询员工信息:员工号 姓名 月薪 年薪 奖金 年收入
SQL> select empno,ename,sal,sal*12,comm,sal*12+comm
2 from emp;

     EMPNO ENAME      SAL     SAL*12       COMM SAL*12+COMM                                                                                           
---------- -------- ----- ---------- ---------- -----------
7369 SMITH 800 9600
7499 ALLEN 1600 19200 300 19500
7521 WARD 1250 15000 500 15500
7566 JONES 2975 35700
7654 MARTIN 1250 15000 1400 16400
7698 BLAKE 2850 34200
7782 CLARK 2450 29400
7788 SCOTT 3000 36000
7839 KING 5000 60000
7844 TURNER 1500 18000 0 18000
7876 ADAMS 1100 13200

EMPNO ENAME SAL SAL*12 COMM SAL*12+COMM
---------- -------- ----- ---------- ---------- -----------
7900 JAMES 950 11400
7902 FORD 3000 36000
7934 MILLER 1300 15600

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

     EMPNO ENAME      SAL     SAL*12       COMM SAL*12+NVL(COMM,0)                                                                                    
---------- -------- ----- ---------- ---------- ------------------
7369 SMITH 800 9600 9600
7499 ALLEN 1600 19200 300 19500
7521 WARD 1250 15000 500 15500
7566 JONES 2975 35700 35700
7654 MARTIN 1250 15000 1400 16400
7698 BLAKE 2850 34200 34200
7782 CLARK 2450 29400 29400
7788 SCOTT 3000 36000 36000
7839 KING 5000 60000 60000
7844 TURNER 1500 18000 0 18000
7876 ADAMS 1100 13200 13200

EMPNO ENAME SAL SAL*12 COMM SAL*12+NVL(COMM,0)
---------- -------- ----- ---------- ---------- ------------------
7900 JAMES 950 11400 11400
7902 FORD 3000 36000 36000
7934 MILLER 1300 15600 15600

SQL> –查询奖金为null的员工
SQL> select *
2 from emp
3 where comm=null;

未选定行

SQL> select *
2 from emp
3 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

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

    EMPNO ENAME      SAL     SAL*12       COMM SAL*12+NVL(COMM,0)                                                                                    
---------- -------- ----- ---------- ---------- ------------------
7369 SMITH 800 9600 9600
7499 ALLEN 1600 19200 300 19500
7521 WARD 1250 15000 500 15500
7566 JONES 2975 35700 35700
7654 MARTIN 1250 15000 1400 16400
7698 BLAKE 2850 34200 34200
7782 CLARK 2450 29400 29400
7788 SCOTT 3000 36000 36000
7839 KING 5000 60000 60000
7844 TURNER 1500 18000 0 18000
7876 ADAMS 1100 13200 13200

EMPNO ENAME SAL SAL*12 COMM SAL*12+NVL(COMM,0)
---------- -------- ----- ---------- ---------- ------------------
7900 JAMES 950 11400 11400
7902 FORD 3000 36000 36000
7934 MILLER 1300 15600 15600

SQL> –distinct 去掉重复记录
SQL> select deptno from emp;

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

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

SQL> select distinct deptno from emp;

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

SQL> select job from emp;

JOB                                                                                                                                                   
---------
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
PRESIDENT
SALESMAN
CLERK

JOB
---------
CLERK
ANALYST
CLERK

SQL> select distinct job from emp;

JOB                                                                                                                                                   
---------
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST

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

SQL> –distinct作用于后面所有的列

SQL> –连接符
SQL> –concat
SQL> select concat(‘Hello’,’ World’) from emp;

Oracle数据库总结:

Oracle数据库之集合运算
Oracle数据库之数据处理
Oracle数据库之建表和管理表
Oracle数据库之对象视图、索引、序列、同义词