Oracle基本查询

SQL> --清屏命令
SQL> host cls

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

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

SQL> --tab:oracle提供的一个数据字典,包含了当前用户下的表和视图
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 13-7月 -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 13-7月 -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> set linesize 120
SQL> --设置列显示宽度,设置sal
SQL> col sal for 9999
SQL> col ename for a15
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 13-7月 -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 13-7月 -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> 9999:设置数字
SQL> aa15:表示设置字符串宽带15位
SQL> */
SQL> set pagesize 60
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 13-7月 -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 13-7月 -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> --设置feedback
SQL> set feedback off
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 13-7月 -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 13-7月 -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> set feedback on
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 13-7月 -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 13-7月 -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> select empno,ename,job,mgr,hirdate,sal,comm,deptno from emp;
select empno,ename,job,mgr,hirdate,sal,comm,deptno from emp
                           *
第 1 行出现错误:
ORA-00904: "HIRDATE": 标识符无效


SQL> --修改命令:c
SQL> c /hirdate/hiredate
  1* select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp
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 13-7月 -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 13-7月 -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> --修改命令,使用ed命令
SQL> select empno,ename,job,mgr,hirdate,sal,comm,deptno from emp;
select empno,ename,job,mgr,hirdate,sal,comm,deptno from emp
                           *
第 1 行出现错误:
ORA-00904: "HIRDATE": 标识符无效


SQL> ed
已写入 file afiedt.buf

  1* select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp
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 13-7月 -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 13-7月 -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> /*
SQL> select *
SQL> select 列名1,....
SQL> 这两种哪个性能好?第二个好
SQL> 在oracle9i后,可以认为一样
SQL> */
SQL> --查询指定列: 名字,工资,奖金
SQL> select ename,sal,comm from emp;

ENAME             SAL       COMM                                                                                       
--------------- ----- ----------                                                                                       
SMITH             800                                                                                                  
ALLEN            1600        300                                                                                       
WARD             1250        500                                                                                       
JONES            2975                                                                                                  
MARTIN           1250       1400                                                                                       
BLAKE            2850                                                                                                  
CLARK            2450                                                                                                  
SCOTT            3000                                                                                                  
KING             5000                                                                                                  
TURNER           1500          0                                                                                       
ADAMS            1100                                                                                                  
JAMES             950                                                                                                  
FORD             3000                                                                                                  
MILLER           1300                                                                                                  

已选择14行。

SQL> -- 查询员工的年薪=月薪*12
SQL> select ename,sal,sal*12 年薪,comm from emp;

ENAME             SAL       年薪       COMM                                                                            
--------------- ----- ---------- ----------                                                                            
SMITH             800       9600                                                                                       
ALLEN            1600      19200        300                                                                            
WARD             1250      15000        500                                                                            
JONES            2975      35700                                                                                       
MARTIN           1250      15000       1400                                                                            
BLAKE            2850      34200                                                                                       
CLARK            2450      29400                                                                                       
SCOTT            3000      36000                                                                                       
KING             5000      60000                                                                                       
TURNER           1500      18000          0                                                                            
ADAMS            1100      13200                                                                                       
JAMES             950      11400                                                                                       
FORD             3000      36000                                                                                       
MILLER           1300      15600                                                                                       

已选择14行。

SQL> -- 查询员工的年收入=年薪+奖金
SQL> select ename 姓名,sal 月薪,sal*12 年薪, comm 奖金,sal*12+comm 年收入 from emp;

姓名             月薪       年薪       奖金     年收入                                                                 
---------- ---------- ---------- ---------- ----------                                                                 
SMITH             800       9600                                                                                       
ALLEN            1600      19200        300      19500                                                                 
WARD             1250      15000        500      15500                                                                 
JONES            2975      35700                                                                                       
MARTIN           1250      15000       1400      16400                                                                 
BLAKE            2850      34200                                                                                       
CLARK            2450      29400                                                                                       
SCOTT            3000      36000                                                                                       
KING             5000      60000                                                                                       
TURNER           1500      18000          0      18000                                                                 
ADAMS            1100      13200                                                                                       
JAMES             950      11400                                                                                       
FORD             3000      36000                                                                                       
MILLER           1300      15600                                                                                       

已选择14行。

SQL> --关于null值的使用:
SQL> --1. 如果一个表达式中含义null, 会导致整个表达式为null
SQL> --2. 在sql中, null!=null
SQL> --对于第一种情况,使用滤空函数
SQL> select ename 姓名,sal 月薪,sal*12 年薪, comm 奖金,sal*12+ nvl(comm,0) 年收入 from emp;

姓名             月薪       年薪       奖金     年收入                                                                 
---------- ---------- ---------- ---------- ----------                                                                 
SMITH             800       9600                  9600                                                                 
ALLEN            1600      19200        300      19500                                                                 
WARD             1250      15000        500      15500                                                                 
JONES            2975      35700                 35700                                                                 
MARTIN           1250      15000       1400      16400                                                                 
BLAKE            2850      34200                 34200                                                                 
CLARK            2450      29400                 29400                                                                 
SCOTT            3000      36000                 36000                                                                 
KING             5000      60000                 60000                                                                 
TURNER           1500      18000          0      18000                                                                 
ADAMS            1100      13200                 13200                                                                 
JAMES             950      11400                 11400                                                                 
FORD             3000      36000                 36000                                                                 
MILLER           1300      15600                 15600                                                                 

已选择14行。

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

未选定行

SQL> ed
已写入 file afiedt.buf

  1  select * from emp
  2* where comm is null
SQL> /

     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 13-7月 -87      3000                    20                             
      7839 KING            PRESIDENT            17-11月-81      5000                    10                             
      7876 ADAMS           CLERK           7788 13-7月 -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 *
  2  frm emp
  3  where comm is null;
frm emp
*
第 2 行出现错误:
ORA-00923: 未找到要求的 FROM 关键字


SQL> ed
已写入 file afiedt.buf

  1  select *
  2  frm emp
  3* where comm is null
SQL> 2
  2* frm emp
SQL> c /frm/from
  2* from emp
SQL> /

     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 13-7月 -87      3000                    20                             
      7839 KING            PRESIDENT            17-11月-81      5000                    10                             
      7876 ADAMS           CLERK           7788 13-7月 -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> --连接符:||
SQL> --查询员工的工资,要求显示的格式: Tom的工资是1000
SQL> select ename||'的工资是'||sal
  2  from emp;

ENAME||'的工资是'||SAL                                                                                                 
----------------------------------------------------------                                                             
SMITH的工资是800                                                                                                       
ALLEN的工资是1600                                                                                                      
WARD的工资是1250                                                                                                       
JONES的工资是2975                                                                                                      
MARTIN的工资是1250                                                                                                     
BLAKE的工资是2850                                                                                                      
CLARK的工资是2450                                                                                                      
SCOTT的工资是3000                                                                                                      
KING的工资是5000                                                                                                       
TURNER的工资是1500                                                                                                     
ADAMS的工资是1100                                                                                                      
JAMES的工资是950                                                                                                       
FORD的工资是3000                                                                                                       
MILLER的工资是1300                                                                                                     

已选择14行。

SQL> select 'Hello World'
  2  from dual;

'HELLOWORLD                                                                                                            
-----------                                                                                                            
Hello World                                                                                                            

已选择 1 行。

SQL> --dual表: Oracle提供的一个伪表
SQL> select 1+2 from dual;

       1+2                                                                                                             
----------                                                                                                             
         3                                                                                                             

已选择 1 行。

SQL> --distinct的使用
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 13-7月 -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 13-7月 -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> select distinct job from emp;

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

已选择5行。

SQL> -- distinct作用于多列:当作用列全部相同时,才认为是重复记录
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行。

SQL> --追加命令: a(append)
SQL> a order by deptno
  1* select distinct job,deptno from emporder by deptno
SQL> ed
已写入 file afiedt.buf

  1* select distinct job,deptno from emp order by deptno
SQL> /

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

已选择9行。

SQL> --a命令:两个空格
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> spool off

 

你可能感兴趣的:(oracle,sql,manager,null,table,360)