zoukankan      html  css  js  c++  java
  • Chapter 06Displaying Data From multiple Tables 01

    Objectives

    After completing this lesson,you should be able to do the following:

    • Write SELECT statements to access data from more than one table using equijoins and nonequijoins.
    • Join a table to itself by using a self-join
    • View data that generally does not meet a join condtions by using outer joins
    • Generate a Cartesian product of all rows from two or more tables.

    Lesson Agenda

    Types of JOINS and its syntax

    • Nature join:
      • -USING clause
      • -ON clause
    • Self-join
    • Nonequijoins
    • OUTER join
      • -LEFT OUTER join
      • -RIGHT OUTER join
      • -FULL OUTER join
    • Cartesian product
      • -Cross join

    Obtaining Data from Multiple Tables

    equijoin:两张表基于某个值相等,而连接起来.

    Types of Joins

    Joins that are complicant with the SQL:1999 standard include the following:

    • Natural joins:
      • -NATURAL JOIN clause
      • -USING clause
      • -ON clause
    • Outer joins:
      • -LEFT OUTER JOIN
      • -RIGHT OUTER JOIN
      • -FULL OUTER JOIN
    • Cross joins

    Joining Tables Using SQL:1999 Syntax

    Use a join to query data from more than one table:

    View Code
    SELECT table1.column,table2.column
    
    FROM table1
    
    [NATURAL JOIN table2] | 
    
    [JOIN table2 USING (column_name)] | 
    
    [JOIN table2 ON (table1.column_name = table2.column_name)] | 
    
    [LEFT|RIGHT|OUTER JOIN table2 ON (table1.column_name = table2.column_name)] | 
    
    [CROSS JOIN table2];

    Joining Tables Using Oracle Syntax

    • Use a join to query data from more than one table:
    SELECT table1.column_name,table2.column_name
    
    FROM table1,table2
    
    WHERE table1.column1 = table2.column2
    • Write the join condition in the WHERE clause
    • Prefix the column name with the table name when the same column name appears in more than one table.
    INNER JOIN
    SQL> SELECT e.employee_id,e.last_name,d.department_id,d.department_name FROM employees e,departments d WHERE e.department_id = d.department_id;
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            200 Whalen                               10 Administration
            201 Hartstein                            20 Marketing
            202 Fay                                  20 Marketing
            114 Raphaely                             30 Purchasing
            119 Colmenares                           30 Purchasing
            115 Khoo                                 30 Purchasing
            116 Baida                                30 Purchasing
            117 Tobias                               30 Purchasing
            118 Himuro                               30 Purchasing
            203 Mavris                               40 Human Resources
            198 OConnell                             50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            199 Grant                                50 Shipping
            120 Weiss                                50 Shipping
            121 Fripp                                50 Shipping
            122 Kaufling                             50 Shipping
            123 Vollman                              50 Shipping
            124 Mourgos                              50 Shipping
            125 Nayer                                50 Shipping
            126 Mikkilineni                          50 Shipping
            127 Landry                               50 Shipping
            128 Markle                               50 Shipping
            129 Bissot                               50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            130 Atkinson                             50 Shipping
            131 Marlow                               50 Shipping
            132 Olson                                50 Shipping
            133 Mallin                               50 Shipping
            134 Rogers                               50 Shipping
            135 Gee                                  50 Shipping
            136 Philtanker                           50 Shipping
            137 Ladwig                               50 Shipping
            138 Stiles                               50 Shipping
            139 Seo                                  50 Shipping
            140 Patel                                50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            141 Rajs                                 50 Shipping
            142 Davies                               50 Shipping
            143 Matos                                50 Shipping
            144 Vargas                               50 Shipping
            180 Taylor                               50 Shipping
            181 Fleaur                               50 Shipping
            182 Sullivan                             50 Shipping
            183 Geoni                                50 Shipping
            184 Sarchand                             50 Shipping
            185 Bull                                 50 Shipping
            186 Dellinger                            50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            187 Cabrio                               50 Shipping
            188 Chung                                50 Shipping
            189 Dilly                                50 Shipping
            190 Gates                                50 Shipping
            191 Perkins                              50 Shipping
            192 Bell                                 50 Shipping
            193 Everett                              50 Shipping
            194 McCain                               50 Shipping
            195 Jones                                50 Shipping
            196 Walsh                                50 Shipping
            197 Feeney                               50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            104 Ernst                                60 IT
            103 Hunold                               60 IT
            107 Lorentz                              60 IT
            106 Pataballa                            60 IT
            105 Austin                               60 IT
            204 Baer                                 70 Public Relation
                                                        s
    
            176 Taylor                               80 Sales
            177 Livingston                           80 Sales
            179 Johnson                              80 Sales
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            175 Hutton                               80 Sales
            174 Abel                                 80 Sales
            173 Kumar                                80 Sales
            172 Bates                                80 Sales
            171 Smith                                80 Sales
            170 Fox                                  80 Sales
            169 Bloom                                80 Sales
            168 Ozer                                 80 Sales
            145 Russell                              80 Sales
            146 Partners                             80 Sales
            147 Errazuriz                            80 Sales
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            148 Cambrault                            80 Sales
            149 Zlotkey                              80 Sales
            150 Tucker                               80 Sales
            151 Bernstein                            80 Sales
            152 Hall                                 80 Sales
            153 Olsen                                80 Sales
            154 Cambrault                            80 Sales
            155 Tuvault                              80 Sales
            156 King                                 80 Sales
            157 Sully                                80 Sales
            158 McEwen                               80 Sales
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            159 Smith                                80 Sales
            160 Doran                                80 Sales
            161 Sewall                               80 Sales
            162 Vishney                              80 Sales
            163 Greene                               80 Sales
            164 Marvins                              80 Sales
            165 Lee                                  80 Sales
            166 Ande                                 80 Sales
            167 Banda                                80 Sales
            101 Kochhar                              90 Executive
            100 King                                 90 Executive
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            102 De Haan                              90 Executive
            110 Chen                                100 Finance
            108 Greenberg                           100 Finance
            111 Sciarra                             100 Finance
            112 Urman                               100 Finance
            113 Popp                                100 Finance
            109 Faviet                              100 Finance
            206 Gietz                               110 Accounting
            205 Higgins                             110 Accounting
    
    106 rows selected.

    Qualifying Ambiguous Column Names

    • Use table prefixes to qualify column names that are in multiple tables.
    • Use table prefixes to improve performance
    • Instead of full table name prefixes,use table aliases.
    • Table alias gives a table a short name:
    • -Keeps SQL code smaller,uses less memory
    • Use column aliases to distinguish columns that have identical names,but reside in different tables.
    正常别名使用
    SQL> SELECT e.employee_id,e.last_name,d.department_id,d.department_name FROM employees e,departments d WHERE e.department_id = d.department_id;
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            200 Whalen                               10 Administration
            201 Hartstein                            20 Marketing
            202 Fay                                  20 Marketing
            114 Raphaely                             30 Purchasing
            119 Colmenares                           30 Purchasing
            115 Khoo                                 30 Purchasing
            116 Baida                                30 Purchasing
            117 Tobias                               30 Purchasing
            118 Himuro                               30 Purchasing
            203 Mavris                               40 Human Resources
            198 OConnell                             50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            199 Grant                                50 Shipping
            120 Weiss                                50 Shipping
            121 Fripp                                50 Shipping
            122 Kaufling                             50 Shipping
            123 Vollman                              50 Shipping
            124 Mourgos                              50 Shipping
            125 Nayer                                50 Shipping
            126 Mikkilineni                          50 Shipping
            127 Landry                               50 Shipping
            128 Markle                               50 Shipping
            129 Bissot                               50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            130 Atkinson                             50 Shipping
            131 Marlow                               50 Shipping
            132 Olson                                50 Shipping
            133 Mallin                               50 Shipping
            134 Rogers                               50 Shipping
            135 Gee                                  50 Shipping
            136 Philtanker                           50 Shipping
            137 Ladwig                               50 Shipping
            138 Stiles                               50 Shipping
            139 Seo                                  50 Shipping
            140 Patel                                50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            141 Rajs                                 50 Shipping
            142 Davies                               50 Shipping
            143 Matos                                50 Shipping
            144 Vargas                               50 Shipping
            180 Taylor                               50 Shipping
            181 Fleaur                               50 Shipping
            182 Sullivan                             50 Shipping
            183 Geoni                                50 Shipping
            184 Sarchand                             50 Shipping
            185 Bull                                 50 Shipping
            186 Dellinger                            50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            187 Cabrio                               50 Shipping
            188 Chung                                50 Shipping
            189 Dilly                                50 Shipping
            190 Gates                                50 Shipping
            191 Perkins                              50 Shipping
            192 Bell                                 50 Shipping
            193 Everett                              50 Shipping
            194 McCain                               50 Shipping
            195 Jones                                50 Shipping
            196 Walsh                                50 Shipping
            197 Feeney                               50 Shipping
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            104 Ernst                                60 IT
            103 Hunold                               60 IT
            107 Lorentz                              60 IT
            106 Pataballa                            60 IT
            105 Austin                               60 IT
            204 Baer                                 70 Public Relation
                                                        s
    
            176 Taylor                               80 Sales
            177 Livingston                           80 Sales
            179 Johnson                              80 Sales
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            175 Hutton                               80 Sales
            174 Abel                                 80 Sales
            173 Kumar                                80 Sales
            172 Bates                                80 Sales
            171 Smith                                80 Sales
            170 Fox                                  80 Sales
            169 Bloom                                80 Sales
            168 Ozer                                 80 Sales
            145 Russell                              80 Sales
            146 Partners                             80 Sales
            147 Errazuriz                            80 Sales
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            148 Cambrault                            80 Sales
            149 Zlotkey                              80 Sales
            150 Tucker                               80 Sales
            151 Bernstein                            80 Sales
            152 Hall                                 80 Sales
            153 Olsen                                80 Sales
            154 Cambrault                            80 Sales
            155 Tuvault                              80 Sales
            156 King                                 80 Sales
            157 Sully                                80 Sales
            158 McEwen                               80 Sales
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            159 Smith                                80 Sales
            160 Doran                                80 Sales
            161 Sewall                               80 Sales
            162 Vishney                              80 Sales
            163 Greene                               80 Sales
            164 Marvins                              80 Sales
            165 Lee                                  80 Sales
            166 Ande                                 80 Sales
            167 Banda                                80 Sales
            101 Kochhar                              90 Executive
            100 King                                 90 Executive
    
    EMPLOYEE_ID LAST_NAME                 DEPARTMENT_ID DEPARTMENT_NAME
    ----------- ------------------------- ------------- ---------------
            102 De Haan                              90 Executive
            110 Chen                                100 Finance
            108 Greenberg                           100 Finance
            111 Sciarra                             100 Finance
            112 Urman                               100 Finance
            113 Popp                                100 Finance
            109 Faviet                              100 Finance
            206 Gietz                               110 Accounting
            205 Higgins                             110 Accounting
    
    106 rows selected.
    错误别名用法
    SQL> SELECT e.employee_id,e.last_name,d.department_id,d.department_name FROM employees e,departments d WHERE employees.department_id = d.department_id;
    SELECT e.employee_id,e.last_name,d.department_id,d.department_name FROM employees e,departments d WHERE employees.department_id = d.department_id
                                                                                                            *
    ERROR at line 1:
    ORA-00904: "EMPLOYEES"."DEPARTMENT_ID": invalid identifier
  • 相关阅读:
    android_自定义布局
    二叉树_学习笔记
    栈的应用-四则表达式(C#代码实现)
    Android Fragment 生命周期
    Android Fragment之间传值
    Android ArrayAdpater 填充集合
    任务和返回栈
    XML Drawable与9-Patches
    《python语言程序设计》_第一章编程题
    CSS-文本超出部分省略号
  • 原文地址:https://www.cnblogs.com/arcer/p/3022212.html
Copyright © 2011-2022 走看看