1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu displaying data from Multiple tables docx

38 340 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 38
Dung lượng 218,89 KB

Nội dung

Displaying Data from Multiple Tables 4 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć2 Displaying Data from Multiple Tables 4Ć3 Objectives This lesson will cover how to obtain data from more than one table, using the many different methods available. At the end of this lesson, you should be able to D Write SELECT statements to access data from more than one table using equality and non-equality joins. D View data that would not normally meet a join condition by using outer joins. D Join a table to itself. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć4 Displaying Data from Multiple Tables 4Ć5 Overview When data from more than one table in the database is required, a join condition is used. Rows in one table may be joined to rows in another table according to common values existing in corresponding columns, that is to say primary and foreign key columns. There are two main types of join conditions: D Equijoins D Non-equijoins Additional join methods include the following: D Outer joins D Self joins D Set operators For more information about set operators, attend Advanced SQL and SQL*Plus course. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć6 Displaying Data from Multiple Tables 4Ć7 What Is a Cartesian Product? When a join condition is invalid or omitted completely, the result is a Cartesian Product, in which all combinations of rows will be displayed. All rows in the first table are joined to all rows in the second table. And Why Should You Care? A Cartesian product tends to generate a large number of rows, and its result is rarely useful. You should always include a valid join condition in a WHERE clause, unless you have a specific need to combine all rows from all tables. SQL> SELECT name, last_name 2 FROM s_dept, s_emp; . 300 rows selected. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć8 Displaying Data from Multiple Tables 4Ć9 Simple Join Query To display data from two or more related tables, write a simple join condition in the WHERE clause. Syntax SELECT table.column, table.column . FROM table1, table2 WHERE table1.column1 = table2.column2; where: table.column denotes the table and column from which data is retrieved. table1.column1 = is the condition that joins (or relates) table2.column2 the tables together. Guidelines D When writing a SELECT statement that joins tables, precede the column name with the table name for clarity and to enhance database access. D If the same column name appears in more than one table, then the column name must be prefixed with the table name. D To join tables together, you need a minimum of the number of join conditions summarized as the number of tables minus one. Therefore, to join four tables, a minimum of three joins would be required. This rule may not apply if your table has a concatenated primary key, in which case more than one column is required to uniquely identify each row. For more information, see Oracle7 Server SQL Language Reference Manual, “SELECT.” Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć10 Server ÉÉÉÉ [...]... which one or more rows from the non-deficient table can be joined Syntax SELECT table.column, table.column FROM table1, table2 WHERE table1.column = table2.column(+); or SELECT table.column, table.column FROM table1, table2 WHERE table1.column(+) = table2.column; where: table1.column = table2.column (+) Displaying Data from Multiple Tables is the condition that joins (or relates) the tables together is... selected Displaying Data from Multiple Tables 4Ć25 4Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary There are multiple ways to join tables The common thread, though, is that you want to link them through a condition in the WHERE clause The method you choose will be based on the required result and the data structures you are using Syntax SELECT table.column, table.column FROM. .. code smaller, therefore conserving memory Displaying Data from Multiple Tables 4Ć27 4Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice Overview This practice is intended to give you practical experience extracting data from more than one table You will be required to join and restrict rows in the WHERE clause Practice Contents D Joining tables using an equijoin D Performing outer... appear on one side of the expression—the side that has information missing It returns those rows from one table which have no direct match in the other table D A condition involving an outer join may not use the IN operator or be linked to another condition by the OR operator Displaying Data from Multiple Tables 4Ć23 Server É ÉÉ 4Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Joining... SELECT statement D Table aliases should be meaningful D The table alias is only valid for the current SELECT statement Displaying Data from Multiple Tables 4Ć17 4Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder NonĆEquijoin The relationship between the EMP and SALGRADE tables is a non-equijoin, in that no column in EMP corresponds directly to a column in SALGRADE The relationship is... S_EMP.DEPT_ID and S_DEPT.ID are equal Displaying Data from Multiple Tables 4Ć11 4Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Simple Join Query continued Qualifying Ambiguous Column Names You need to qualify the names of the columns in the WHERE clause with the table name to avoid ambiguity Without the table prefixes, the ID column could be from either the S_DEPT or the S_EMP table... Contents D Joining tables using an equijoin D Performing outer and self joins D Adding additional conditions Displaying Data from Multiple Tables 4Ć29 4Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 4 Use the S_EMP, S_DEPT, S_CUSTOMER, S_REGION, S_ORD, S_ITEM, and S_PRODUCT tables to complete the following exercises 1 Write a report containing each employee’s last name, department... simplest Remember to specify the low value first and the high value last when using BETWEEN Table aliases have been specified for performance reasons, not because of possible ambiguity Displaying Data from Multiple Tables 4Ć19 4Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Returning Records with No Direct Match If a row does not satisfy a join condition, then the row will not appear... s_dept.id s_dept.region_id = s_region.id s_emp.commission_pct > 0; NAME COMMISSION_PCT -North America 10 South America 12.5 Africa / Middle East 10 Asia 15 Europe 17.5 Displaying Data from Multiple Tables 4Ć15 4Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Using Table Aliases Qualifying column names with table names can be very time consuming, particularly if table... Asia 45 5 Europe 50 1 North America 12 rows selected The requirement to qualify ambiguous column names is also applicable to columns that may be ambiguous in a SELECT or ORDER BY clause Displaying Data from Multiple Tables 4Ć13 Server ÉÉ ÉÉ 4Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Simple Join Query continued Additional Search Conditions In addition to the join, you may have . Displaying Data from Multiple Tables 4 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć2 Displaying Data from Multiple Tables. PL/SQL Using Procedure Builder4Ć4 Displaying Data from Multiple Tables 4Ć5 Overview When data from more than one table in the database is required, a join condition

Ngày đăng: 21/12/2013, 06:17

TỪ KHÓA LIÊN QUAN

w