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

SQL VISUAL QUICKSTART GUIDE- P27 doc

10 156 0

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

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Table of Contents

  • Introduction

  • About SQL

  • About This Book

  • What You’ll Need

  • Chapter 1: DBMS Specifics

    • Running SQL Programs

    • Microsoft Access

    • Microsoft SQL Server

    • Oracle

    • IBM DB2

    • MySQL

    • PostgreSQL

  • Chapter 2: The Relational Model

    • Tables, Columns, and Rows

    • Primary Keys

    • Foreign Keys

    • Relationships

    • Normalization

    • The Sample Database

    • Creating the Sample Database

  • Chapter 3: SQL Basics

    • SQL Syntax

    • SQL Standards and Conformance

    • Identifiers

    • Data Types

    • Character String Types

    • Binary Large Object Type

    • Exact Numeric Types

    • Approximate Numeric Types

    • Boolean Type

    • Datetime Types

    • Interval Types

    • Unique Identifiers

    • Other Data Types

    • Nulls

  • Chapter 4: Retrieving Data from a Table

    • Retrieving Columns with SELECT and FROM

    • Creating Column Aliases with AS

    • Eliminating Duplicate Rows with DISTINCT

    • Sorting Rows with ORDER BY

    • Filtering Rows with WHERE

    • Combining and Negating Conditions with AND, OR, and NOT

    • Matching Patterns with LIKE

    • Range Filtering with BETWEEN

    • List Filtering with IN

    • Testing for Nulls with IS NULL

  • Chapter 5: Operators and Functions

    • Creating Derived Columns

    • Performing Arithmetic Operations

    • Determining the Order of Evaluation

    • Concatenating Strings with ||

    • Extracting a Substring with SUBSTRING()

    • Changing String Case with UPPER() and LOWER()

    • Trimming Characters with TRIM()

    • Finding the Length of a String with CHARACTER_LENGTH()

    • Finding Substrings with POSITION()

    • Performing Datetime and Interval Arithmetic

    • Getting the Current Date and Time

    • Getting User Information

    • Converting Data Types with CAST()

    • Evaluating Conditional Values with CASE

    • Checking for Nulls with COALESCE()

    • Comparing Expressions with NULLIF()

  • Chapter 6: Summarizing and Grouping Data

    • Using Aggregate Functions

    • Creating Aggregate Expressions

    • Finding a Minimum with MIN()

    • Finding a Maximum with MAX()

    • Calculating a Sum with SUM()

    • Calculating an Average with AVG()

    • Counting Rows with COUNT()

    • Aggregating Distinct Values with DISTINCT

    • Grouping Rows with GROUP BY

    • Filtering Groups with HAVING

  • Chapter 7: Joins

    • Qualifying Column Names

    • Creating Table Aliases with AS

    • Using Joins

    • Creating Joins with JOIN or WHERE

    • Creating a Cross Join with CROSS JOIN

    • Creating a Natural Join with NATURAL JOIN

    • Creating an Inner Join with INNER JOIN

    • Creating Outer Joins with OUTER JOIN

    • Creating a Self-Join

  • Chapter 8: Subqueries

    • Understanding Subqueries

    • Subquery Syntax

    • Subqueries vs. Joins

    • Simple and Correlated Subqueries

    • Qualifying Column Names in Subqueries

    • Nulls in Subqueries

    • Using Subqueries as Column Expressions

    • Comparing a Subquery Value by Using a Comparison Operator

    • Testing Set Membership with IN

    • Comparing All Subquery Values with ALL

    • Comparing Some Subquery Values with ANY

    • Testing Existence with EXISTS

    • Comparing Equivalent Queries

  • Chapter 9: Set Operations

    • Combining Rows with UNION

    • Finding Common Rows with INTERSECT

    • Finding Different Rows with EXCEPT

  • Chapter 10: Inserting, Updating, and Deleting Rows

    • Displaying Table Definitions

    • Inserting Rows with INSERT

    • Updating Rows with UPDATE

    • Deleting Rows with DELETE

  • Chapter 11: Creating, Altering, and Dropping Tables

    • Creating Tables

    • Understanding Constraints

    • Creating a New Table with CREATE TABLE

    • Forbidding Nulls with NOT NULL

    • Specifying a Default Value with DEFAULT

    • Specifying a Primary Key with PRIMARY KEY

    • Specifying a Foreign Key with FOREIGN KEY

    • Forcing Unique Values with UNIQUE

    • Adding a Check Constraint with CHECK

    • Creating a Temporary Table with CREATE TEMPORARY TABLE

    • Creating a New Table from an Existing One with CREATE TABLE AS

    • Altering a Table with ALTER TABLE

    • Dropping a Table with DROP TABLE

  • Chapter 12: Indexes

    • Creating an Index with CREATE INDEX

    • Dropping an Index with DROP INDEX

  • Chapter 13: Views

    • Creating a View with CREATE VIEW

    • Retrieving Data Through a View

    • Updating Data Through a View

    • Dropping a View with DROP VIEW

  • Chapter 14: Transactions

    • Executing a Transaction

  • Chapter 15: SQL Tricks

    • Calculating Running Statistics

    • Generating Sequences

    • Finding Sequences, Runs, and Regions

    • Limiting the Number of Rows Returned

    • Assigning Ranks

    • Calculating a Trimmed Mean

    • Picking Random Rows

    • Handling Duplicates

    • Creating a Telephone List

    • Retrieving Metadata

    • Working with Dates

    • Calculating a Median

    • Finding Extreme Values

    • Changing Running Statistics Midstream

    • Pivoting Results

    • Working with Hierarchies

  • Index

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    • I

    • J

    • K

    • L

    • M

    • N

    • O

    • P

    • Q

    • R

    • S

    • T

    • U

    • V

    • W

Nội dung

Listing 7.28 uses a right outer join to include all publishers in the result, regardless of whether an author lives in the publisher’s city. See Figure 7.28 for the result. ✔ Tip ■ To run Listing 7.28 in Microsoft SQL Server by using WHERE syntax, type: SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a, publishers p WHERE a.city =* p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC; To run Listing 7.28 in Oracle 8i, type: SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a, publishers p WHERE a.city (+) = p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC; 240 Chapter 7 Creating Outer Joins with OUTER JOIN Listing 7.28 This right outer join includes all rows in the table publishers in the result, whether or not there’s a match in the column city in the table authors . See Figure 7.28 for the result. SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a RIGHT OUTER JOIN publishers p ON a.city = p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC; Listing au_fname au_lname pub_name Christian Kells Abatis Publishers Hallie Hull Core Dump Books Klee Hull Core Dump Books NULL NULL Schadenfreude Press NULL NULL Tenterhooks Press Figure 7.28 Result of Listing 7.28. Note that there’s no matching data for two of the listed publishers, so these rows contain nulls in the columns au_fname and au_lname . Listing 7.29 uses a full outer join to include all publishers and all authors in the result, regardless of whether a publisher and author are located in the same city. See Figure 7.29 for the result. ✔ Tip ■ In Microsoft SQL Server, you can’t place the * operator on both sides of the comparison operator to form a full outer join. Instead, form the union of a left and right outer join; see “Combining Rows with UNION ” in Chapter 9. To run Listing 7.29 by using WHERE syntax, type: SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a, publishers p WHERE a.city *= p.city UNION ALL SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a, publishers p WHERE a.city =* p.city AND a.city IS NULL; continues on next page 241 Joins Creating Outer Joins with OUTER JOIN Listing 7.29 This full outer join includes all rows in the tables authors and publishers in the result, whether or not there’s a match in the city columns. See Figure 7.29 for the result. SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a FULL OUTER JOIN publishers p ON a.city = p.city ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC; Listing au_fname au_lname pub_name Sarah Buchman NULL Wendy Heydemark NULL Kellsey NULL Paddy O'Furniture NULL Christian Kells Abatis Publishers Hallie Hull Core Dump Books Klee Hull Core Dump Books NULL NULL Schadenfreude Press NULL NULL Tenterhooks Press Figure 7.29 Result of Listing 7.29. This result contains nine rows: four rows for authors who have no matching rows in the table publishers , three rows in which the author and publisher coexist in the same city, and two rows for publishers who have no matching city in the table authors . In Oracle, you can’t place the (+) operator on both sides of the comparison operator to form a full outer join. Instead, form the union of a left and right outer join; see “Combining Rows with UNION ” in Chapter 9. To run Listing 7.29 in Oracle 8i, type: SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a, publishers p WHERE a.city = p.city (+) UNION ALL SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a, publishers p WHERE a.city (+) = p.city AND a.city IS NULL; Microsoft Access and MySQL don’t sup- port full outer joins, but you can replicate one by taking the union of left and right outer joins; see “Combining Rows with UNION ” in Chapter 9. In the following exam- ple, the first UNION table is a left outer join restricted to return all the rows in authors as well as the matched rows in publishers based on city . The second UNION table is a right outer join restricted to return only the unmatched rows in publishers . To run Listing 7.29, type: SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a LEFT OUTER JOIN publishers p ON a.city = p.city UNION ALL SELECT a.au_fname, a.au_lname, p.pub_name FROM authors a RIGHT OUTER JOIN publishers p ON a.city = p.city WHERE a.city IS NULL; 242 Chapter 7 Creating Outer Joins with OUTER JOIN Listing 7.30 uses a left outer join to list the number of books that each author wrote (or cowrote). See Figure 7.30 for the result. Note that in contrast to Listing 7.11 in “Creating an Inner Join with INNER JOIN ” earlier in this chapter, the author A07 (Paddy O’Furniture) appears in the result even though he has written no books. ✔ Tip ■ To run Listing 7.30 in Oracle 8i, type: SELECT a.au_id, COUNT(ta.title_id) AS “Num books” FROM authors a, title_authors ta WHERE a.au_id = ta.au_id (+) GROUP BY a.au_id ORDER BY a.au_id ASC; 243 Joins Creating Outer Joins with OUTER JOIN Listing 7.30 List the number of books that each author wrote (or cowrote), including authors who have written no books. See Figure 7.30 for the result. SELECT a.au_id, COUNT(ta.title_id) AS "Num books" FROM authors a LEFT OUTER JOIN title_authors ta ON a.au_id = ta.au_id GROUP BY a.au_id ORDER BY a.au_id ASC; Listing au_id Num books A01 3 A02 4 A03 2 A04 4 A05 1 A06 3 A07 0 Figure 7.30 Result of Listing 7.30. Listing 7.31 uses a WHERE condition to test for null and list only the authors who haven’t written a book. See Figure 7.31 for the result. ✔ Tip ■ To run Listing 7.31 in Oracle 8i, type: SELECT a.au_id, a.au_fname, a.au_lname FROM authors a, title_authors ta WHERE a.au_id = ta.au_id (+) AND ta.au_id IS NULL; 244 Chapter 7 Creating Outer Joins with OUTER JOIN Listing 7.31 List the authors who haven’t written (or cowritten) a book. See Figure 7.31 for the result. SELECT a.au_id, a.au_fname, a.au_lname FROM authors a LEFT OUTER JOIN title_authors ta ON a.au_id = ta.au_id WHERE ta.au_id IS NULL; Listing au_id au_fname au_lname A07 Paddy O'Furniture Figure 7.31 Result of Listing 7.31. Listing 7.32 combines an inner join and a left outer join to list all authors and any books they wrote (or cowrote) that sold more than 100,000 copies. In this example, first I created a filtered INNER JOIN result and then OUTER JOIN ed it with the table authors , from which I wanted all rows. See Figure 7.32 for the result. 245 Joins Creating Outer Joins with OUTER JOIN Listing 7.32 List all authors and any books written (or cowritten) that sold more than 100,000 copies. See Figure 7.32 for the result. SELECT a.au_id, a.au_fname, a.au_lname, tta.title_id, tta.title_name, tta.sales FROM authors a LEFT OUTER JOIN (SELECT ta.au_id, t.title_id, t.title_name, t.sales FROM title_authors ta INNER JOIN titles t ON t.title_id = ta.title_id WHERE sales > 100000) tta ON a.au_id = tta.au_id ORDER BY a.au_id ASC, tta.title_id ASC; Listing au_id au_fname au_lname title_id title_name sales A01 Sarah Buchman NULL NULL NULL A02 Wendy Heydemark T07 I Blame My Mother 1500200 A02 Wendy Heydemark T12 Spontaneous, Not Annoying 100001 A03 Hallie Hull NULL NULL NULL A04 Klee Hull T05 Exchange of Platitudes 201440 A04 Klee Hull T07 I Blame My Mother 1500200 A05 Christian Kells NULL NULL NULL A06 Kellsey NULL NULL NULL A07 Paddy O'Furniture NULL NULL NULL Figure 7.32 Result of Listing 7.32. ✔ Tip ■ To run Listing 7.32 in Oracle 8i, type: SELECT a.au_id, a.au_fname, a.au_lname, tta.title_id, tta.title_name, tta.sales FROM authors a, (SELECT ta.au_id, t.title_id, t.title_name, t.sales FROM title_authors ta, titles t WHERE t.title_id = ta.title_id AND sales > 100000) tta WHERE a.au_id = tta.au_id (+) ORDER BY a.au_id ASC, tta.title_id ASC; MySQL 4.1 and later will run Listing 7.32, but earlier versions don’t support subqueries; see the DBMS Tip in “Understanding Subqueries” in Chapter 8. For complicated queries, you often can create a temporary table to hold the subquery; see “Creating a Temporary Table with CREATE TEMPORARY TABLE ” in Chapter 11. To run Listing 7.32 in MySQL 4.0 and earlier, type: CREATE TEMPORARY TABLE tta SELECT ta.au_id, t.title_id, t.title_name, t.sales FROM title_authors ta INNER JOIN titles t ON t.title_id = ta.title_id WHERE sales > 100000; SELECT a.au_id, a.au_fname, a.au_lname, tta.title_id, tta.title_name, tta.sales FROM authors a LEFT OUTER JOIN tta ON a.au_id = tta.au_id ORDER BY a.au_id ASC, tta.title_id ASC; DROP TABLE tta; 246 Chapter 7 Creating Outer Joins with OUTER JOIN Creating a Self-Join A self-join is a normal SQL join that joins a table to itself and retrieves rows from a table by comparing values in one or more columns in the same table. Self-joins often are used in tables with a reflexive relationship, which is a primary-key/foreign-key relationship from a column or combination of columns in a table to other columns in that same table. For information about keys, see “Primary Keys” and “Foreign Keys” in Chapter 2. Suppose that you have the following table, named employees : emp_id emp_name boss_id ———––– ———————–––––––––– ————––– E01 Lord Copper NULL E02 Jocelyn Hitchcock E01 E03 Mr. Salter E01 E04 William Boot E03 E05 Mr. Corker E03 emp_id is a primary key that uniquely identi- fies the employee, and boss_id is an employee ID that identifies the employee’s manager. Each manager also is an employee, so to ensure that each manager ID that is added to the table matches an existing employee ID, boss_id is defined as a foreign key of emp_id . Listing 7.33 uses this reflexive relationship to compare rows within the table and retrieve the name of the manager of each employee. (You wouldn’t need a join at all to get just the manager’s ID.) See Figure 7.33 for the result. The same table ( employees ) appears twice in Listing 7.33 with two different aliases ( e1 and e2 ) that are used to qualify column names in the join condition: e1.boss_id = e2.emp_id As with any join, a self-join requires two tables, but instead of adding a second table to the join, you add a second instance of the same table. That way, you can compare a col- umn in the first instance of the table to a column in the second instance. As with all joins, your DBMS combines and returns rows of the table that satisfy the join condition. You really aren’t creating another copy of the table—you’re joining the table to itself— but the effect might be easier to understand if you think about it as being two tables. 247 Joins Creating a Self-Join Listing 7.33 List the name of each employee and the name of his or her manager. See Figure 7.33 for the result. SELECT e1.emp_name AS "Employee name", e2.emp_name AS "Boss name" FROM employees e1 INNER JOIN employees e2 ON e1.boss_id = e2.emp_id; Listing Employee name Boss name Jocelyn Hitchcock Lord Copper Mr. Salter Lord Copper William Boot Mr. Salter Mr. Corker Mr. Salter Figure 7.33 Result of Listing 7.33. Note that Lord Copper, who has no boss, is excluded from the result because his null boss_id doesn’t satisfy the join condition. To create a self-join: ◆ Type: SELECT columns FROM table [AS] alias1 INNER JOIN table [AS] alias2 ON join_conditions columns is one or more comma-separated expressions or column names from table. alias1 and alias2 are different alias names that are used to refer to table in join_conditions. See “Creating Table Aliases with AS ” earlier in this chapter. join_conditions specifies one or more join conditions to be evaluated for each pair of joined rows. A join condition takes this form: alias1.column op alias2.column op can be any comparison operator: = , <> , < , <= , > , or >= (refer to Table 4.2 in Chapter 4). You can combine multiple join conditions with AND or OR ; see “Combining and Negating Conditions with AND, OR, and NOT ” in Chapter 4. ✔ Tips ■ You can join a table to itself even if no reflexive relationship exists. A common type of self-join compares a column in the first instance of the table to the same column in the second instance. This join condition lets you compare the values in a column to one another, as shown in the subsequent examples in this section. ■ See also “Working with Hierarchies” in Chapter 15. ■ Oracle 8i and earlier don’t support JOIN syntax; use WHERE joins instead. Oracle 9i and later support JOIN syntax. 248 Chapter 7 Creating a Self-Join Listing 7.34 uses a WHERE search condition and self-join from the column state to itself to find all authors who live in the same state as author A04 (Klee Hull). See Figure 7.34 for the result. ✔ Tips ■ Using WHERE syntax, Listing 7.34 is equiv- alent to: SELECT a1.au_id, a1.au_fname, a1.au_lname, a1.state FROM authors a1, authors a2 WHERE a1.state = a2.state AND a2.au_id = ‘A04’; ■ Self-joins often can be restated as sub- queries (see Chapter 8). Using a subquery, Listing 7.34 is equivalent to: SELECT au_id, au_fname, au_lname, state FROM authors WHERE state IN (SELECT state FROM authors WHERE au_id = ‘A04’); 249 Joins Creating a Self-Join Listing 7.34 List the authors who live in the same state as author A04 (Klee Hull). See Figure 7.34 for the result. SELECT a1.au_id, a1.au_fname, a1.au_lname, a1.state FROM authors a1 INNER JOIN authors a2 ON a1.state = a2.state WHERE a2.au_id = 'A04'; Listing au_id au_fname au_lname state A03 Hallie Hull CA A04 Klee Hull CA A06 Kellsey CA Figure 7.34 Result of Listing 7.34. . the publisher’s city. See Figure 7.28 for the result. ✔ Tip ■ To run Listing 7.28 in Microsoft SQL Server by using WHERE syntax, type: SELECT a.au_fname, a.au_lname, p.pub_name FROM authors. publisher and author are located in the same city. See Figure 7.29 for the result. ✔ Tip ■ In Microsoft SQL Server, you can’t place the * operator on both sides of the comparison operator to form a full. a.au_lname, p.pub_name FROM authors a, publishers p WHERE a.city (+) = p.city AND a.city IS NULL; Microsoft Access and MySQL don’t sup- port full outer joins, but you can replicate one by taking the union of left and

Ngày đăng: 05/07/2014, 05:20

TỪ KHÓA LIÊN QUAN

w