zoukankan      html  css  js  c++  java
  • oracle实验6-8:关于null值,列的别名,去掉重复行

    NULL值

    实验6:关于null值得问题

    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                                                          
    KING              5000                                                          
    TURNER          1500             0                                               
    JAMES             950                                                          
    FORD             3000                                                                                                          
    MILLER           1300                                                          

    已选择12行。

    其中COMM列中有一些行没有值,是空值(null)。

    • null值不等于0,也不等于空格。
    • null值是未赋值的值。
    • null值是双银剑,使用好了提高性能。

    别名

    别名的使用原则

    • 区别同名列的名称
    • 非法的表达式合法化
    • 按照你的意愿显示列的名称
    • 特殊的别名要双引
    • 直接写列的后面
    • 使用as增加可读性

    实验7:在列上起一个别名

    SQL> select sal as salary , hiredate "上班日期",sal*12 total_salary from emp;

        SALARY 上班日期       TOTAL_SALARY                                         
    ---------- -------------- ------------                                         
           800 17-12月-80             9600                                         
          1600 20-2月 -81            19200                                         
          1250 22-2月 -81            15000                                         
          2975 02-4月 -81            35700                                         
          1250 28-9月 -81            15000                                         
          2850 01-5月 -81            34200                                         
          2450 09-6月 -81            29400                                         
          5000 17-11月-81            60000                                         
          1500 08-9月 -81            18000                                         
           950 03-12月-81            11400                                         
          3000 03-12月-81            36000                                                                                   
          1300 23-1月 -82            15600                                         

    已选择12行。

    SQL> select sal salary from emp;

        SALARY                                                                     
    ----------                                                                     
           800                                                                     
          1600                                                                     
          1250                                                                     
          2975                                                                     
          1250                                                                     
          2850                                                                     
          2450                                                                     
          5000                                                                     
          1500                                                                     
           950                                                                     
          3000                                                                                                                                            
          1300                                                                     

    已选择12行。

    SQL> select sal as salary , hiredate as 日期 from emp;

        SALARY 日期                                                                
    ---------- --------------                                                      
           800 17-12月-80                                                          
          1600 20-2月 -81                                                          
          1250 22-2月 -81                                                          
          2975 02-4月 -81                                                          
          1250 28-9月 -81                                                          
          2850 01-5月 -81                                                          
          2450 09-6月 -81                                                          
          5000 17-11月-81                                                          
          1500 08-9月 -81                                                          
           950 03-12月-81                                                          
          3000 03-12月-81                                                                                                                 
          1300 23-1月 -82                                                          

    已选择12行。

    重复的行

    select语句显示重复的行,可以使用distinct语法去掉重复的行

    SQL> select deptno from emp;

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

    已选择12行。

    实验8:在显示的时候去掉重复的行

    使用distinct关键字去掉重复的行

    SQL> select distinct deptno from emp;

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

    在oracle10g前的版本需要排序才能去掉重复的行,在10g中不需要排序,所以输出也是无序的。

    返回目录  http://www.cnblogs.com/downpour/p/3155689.html

  • 相关阅读:
    bzoj 4012: [HNOI2015]开店
    POJ 1054 The Troublesome Frog
    POJ 3171 Cleaning Shifts
    POJ 3411 Paid Roads
    POJ 3045 Cow Acrobats
    POJ 1742 Coins
    POJ 3181 Dollar Dayz
    POJ 3040 Allowance
    POJ 3666 Making the Grade
    洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
  • 原文地址:https://www.cnblogs.com/downpour/p/3139352.html
Copyright © 2011-2022 走看看