Oracle数据库之基本查询

前端之家收集整理的这篇文章主要介绍了Oracle数据库之基本查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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,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,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,sal*12,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,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,0)
2 from emp;**

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数据库总结:

@L_301_6@
Oracle数据库之数据处理
Oracle数据库之建表和管理表
Oracle数据库之对象视图、索引、序列、同义词

猜你在找的Oracle相关文章