Oracle 数据库入门之----------------------基本查询

基本查询

 

SQL> --当前用户

SQL> show user

USER  "SCOTT"

SQL>  --当前用户下的表

SQL> select * from tab;

 

TNAME                          TABTYPE  CLUSTERID                               

------------------------------ ------- ----------                               

DEPT                           TABLE                                            

EMP                            TABLE                                            

BONUS                          TABLE                                            

SALGRADE                       TABLE                                            

 

SQL> --员工表的结构

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> host cls

 

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 120

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                                     

 

已选择 14 行。

 

SQL> /*

SQL> SQL优化的原则:

SQL> 1。尽量使用列名

SQL> */

SQL> host cls

 

SQL> --查询员工信息:员工号 姓名 月薪

SQL> select empno,ename,sal

  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                                                                                               

---------- -------- -----                                                                                               

      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                                                                                               

 

已选择 14 行。

 

SQL> host cls

 

SQL> --查询员工信息:员工号 姓名 月薪 年薪

SQL> select empno,ename,sal,sal*12

  2  from emp;

 

     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                                                                                    

 

已选择 14 行。

 

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                                                                                    

 

已选择 14 行。

 

SQL> /*

SQL> SQL中的null

SQL> 1、包含null的表达式都为null

SQL> 2、null永远!=null

SQL> */

SQL> --nvl(a,b)   nvl2

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                                                      

 

已选择 14 行。

 

SQL> --2、null永远!=null

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                                     

 

已选择 10 行。

 

SQL> host cls

 

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                                                      

 

已选择 14 行。

 

SQL> ed

已写入 file afiedt.buf

 

  1  select empno as "员工号",ename "姓名",sal 月    薪,sal*12,comm,sal*12+nvl(comm,0)

  2* from emp

SQL> /

select empno as "员工号",ename "姓名",sal 月    薪,sal*12,comm,sal*12+nvl(comm,0)

                                                *

 1 行出现错误: 

ORA-00923: 未找到要求的 FROM 关

 

 

SQL> ed

已写入 file afiedt.buf

 

  1  select empno as "员工号",ename "姓名",sal "月    薪",sal*12,comm,sal*12+nvl(comm,0)

  2* from emp

SQL> /

 

    员工号 姓名         月    薪     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                                               

 

    员工号 姓名         月    薪     SAL*12       COMM SAL*12+NVL(COMM,0)                                               

---------- ---------- ---------- ---------- ---------- ------------------                                               

      7900 JAMES             950      11400                         11400                                               

      7902 FORD             3000      36000                         36000                                               

      7934 MILLER           1300      15600                         15600                                               

 

已选择 14 行。

 

SQL> host cls

 

SQL> --distinct 去掉重复记录

SQL> select deptno from emp;

 

    DEPTNO                                                                                                              

----------                                                                                                              

        20                                                                                                              

        30                                                                                                              

        30                                                                                                              

        20                                                                                                              

        30                                                                                                              

        30                                                                                                              

        10                                                                                                              

        20                                                                                                              

        10                                                                                                              

        30                                                                                                              

        20                                                                                                              

 

    DEPTNO                                                                                                              

----------                                                                                                              

        30                                                                                                              

        20                                                                                                              

        10                                                                                                              

 

已选择 14 行。

 

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                                                                                                                   

 

已选择 14 行。

 

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                                                                                                      

 

已选择 9 行。

 

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

SQL> host cls

 

SQL> --连接符 ||

SQL> --concat函数

SQL> select concat('Hello','  World');

select concat('Hello','  World')

                               *

 1 行出现错误: 

ORA-00923: 未找到要求的 FROM 关

 

 

SQL> select concat('Hello','  World') from emp;

 

CONCAT('HELL                                                                                                            

------------                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

 

CONCAT('HELL                                                                                                            

------------                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

Hello  World                                                                                                            

 

已选择 14 行。

 

SQL> select concat('Hello','  World') from dual;

 

CONCAT('HELL                                                                                                            

------------                                                                                                            

Hello  World                                                                                                            

 

SQL> select 3+2 from dual;

 

       3+2                                                                                                              

----------                                                                                                              

         5                                                                                                              

 

SQL> --dual表:伪表

SQL> --伪列

SQL> select 'Hello'||'  World' 字from dual;

 

串                                                                                                                  

------------                                                                                                            

Hello  World                                                                                                            

 

SQL> --查询员工信息:***的薪水是****

SQL> select ename||'的薪水是'||sal 信息 from emp;

 

信息                                                                                                                    

----------------------------------------------------------                                                              

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> host cls

 

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                                     

      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> spool off

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值