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

The Language of SQL- P27 pps

5 186 0

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

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Contents

  • Introduction

  • Chapter 1 Relational Databases and SQL

    • Language and Logic

    • SQL Defined

    • Microsoft SQL Server, Oracle, and MySQL

    • Other Databases

    • Relational Databases

    • Primary and Foreign Keys

    • Datatypes

    • NULL Values

    • The Significance of SQL

    • Looking Ahead

  • Chapter 2 Basic Data Retrieval

    • A Simple SELECT

    • Syntax Notes

    • Specifying Columns

    • Column Names with Embedded Spaces

    • Looking Ahead

  • Chapter 3 Calculations and Aliases

    • Calculated Fields

    • Literal Values

    • Arithmetic Calculations

    • Concatenating Fields

    • Column Aliases

    • Table Aliases

    • Looking Ahead

  • Chapter 4 Using Functions

    • The Function of Functions

    • Character Functions

    • Composite Functions

    • Date/Time Functions

    • Numeric Functions

    • Conversion Functions

    • Looking Ahead

  • Chapter 5 Sorting Data

    • Adding a Sort

    • Sorting in Ascending Order

    • Sorting in Descending Order

    • Sorting by Multiple Columns

    • Sorting by a Calculated Field

    • More on Sort Sequences

    • Looking Ahead

  • Chapter 6 Column-Based Logic

    • IF-THEN-ELSE Logic

    • The Simple Format

    • The Searched Format

    • Looking Ahead

  • Chapter 7 Row-Based Logic

    • Applying Selection Criteria

    • WHERE Clause Operators

    • Limiting Rows

    • Limiting Rows with a Sort

    • Looking Ahead

  • Chapter 8 Boolean Logic

    • Complex Logical Conditions

    • The AND Operator

    • The OR Operator

    • Using Parentheses

    • Multiple Sets of Parentheses

    • The NOT Operator

    • The BETWEEN Operator

    • The IN Operator

    • Boolean Logic and NULL Values

    • Looking Ahead

  • Chapter 9 Inexact Matches

    • Pattern Matching

    • Wildcards

    • Matching by Sound

    • Looking Ahead

  • Chapter 10 Summarizing Data

    • Eliminating Duplicates

    • Aggregate Functions

    • The COUNT Function

    • Grouping Data

    • Multiple Columns and Sorting

    • Selection Criteria on Aggregates

    • Looking Ahead

  • Chapter 11 Combining Tables with an Inner Join

    • Joining Two Tables

    • The Inner Join

    • Table Order in Inner Joins

    • Alternate Specification of Inner Joins

    • Table Aliases Revisited

    • Looking Ahead

  • Chapter 12 Combining Tables with an Outer Join

    • The Outer Join

    • Left Joins

    • Testing for NULL Values

    • Right Joins

    • Table Order in Outer Joins

    • Full Joins

    • Looking Ahead

  • Chapter 13 Self Joins and Views

    • Self Joins

    • Creating Views

    • Referencing Views

    • Benefits of Views

    • Modifying and Deleting Views

    • Looking Ahead

  • Chapter 14 Subqueries

    • Types of Subqueries

    • Using a Subquery as a Data Source

    • Using a Subquery in Selection Criteria

    • Correlated Subqueries

    • The EXISTS Operator

    • Using a Subquery as a Calculated Column

    • Looking Ahead

  • Chapter 15 Set Logic

    • Using the UNION Operator

    • Distinct and Non-Distinct Unions

    • Intersecting Queries

    • Looking Ahead

  • Chapter 16 Stored Procedures and Parameters

    • Creating Stored Procedures

    • Parameters in Stored Procedures

    • Executing Stored Procedures

    • Modifying and Deleting Stored Procedures

    • Functions Revisited

    • Looking Ahead

  • Chapter 17 Modifying Data

    • Modification Strategies

    • Inserting Data

    • Deleting Data

    • Updating Data

    • Correlated Subquery Updates

    • Looking Ahead

  • Chapter 18 Maintaining Tables

    • Data Definition Language

    • Table Attributes

    • Table Columns

    • Primary Keys and Indexes

    • Foreign Keys

    • Creating Tables

    • Creating Indexes

    • Looking Ahead

  • Chapter 19 Principles of Database Design

    • Goals of Normalization

    • How to Normalize Data

    • The Art of Database Design

    • Alternatives to Normalization

    • Looking Ahead

  • Chapter 20 Strategies for Displaying Data

    • Beyond SQL

    • Reporting Tools and Crosstab Reports

    • Spreadsheets and Pivot Tables

    • Looking Ahead

  • Appendix A: Getting Started with Microsoft SQL Server

    • Overview

    • Installing SQL Server Express 2008

    • Installing SQL Server Management Studio

    • Using SQL Server Management Studio

  • Appendix B: Getting Started with MySQL

    • Overview

    • Installing MySQL Community Server

    • Installing MySQL Workbench

    • Using MySQL Workbench

  • Appendix C: Getting Started with Oracle

    • Overview

    • Installing Oracle Database Express Edition

    • Using Oracle Database Express Edition

  • Appendix D: Listing of All SQL Statements

  • 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

The results are: Cust ID First Name Last Name Order ID Qty Price 1 William Smith 1 4 2.50 2 Natalie Lopez 2 10 1.25 2 Natalie Lopez 3 12 1.50 3 Brenda Harper 4 5 4.00 Notice that we’re using the AS keyword to specify both column and table aliases. It should also be mentioned that the AS keyword is completely optional. All of the AS keywords can be removed from the SELECT, and the statement would still be valid and return the same results. However, I recommend the use of the AS keyword for the sake of clarity. DATABASE DIFFERENCES: Oracle As mentioned in Chapter 3, table aliases are specified in Oracle without the AS keyword. The syntax for the statement in Oracle is: SELECT C.CustomerID AS 'Cust ID', C.FirstName AS 'First Name', C.LastName AS 'Last Name', O.OrderID AS 'Order ID', O.Quantity AS 'Qty', O.PricePerItem AS 'Price' FROM Customers C INNER JOIN Orders O ON C.CustomerID ¼ O.CustomerID; Looking Ahead The ability to join tables together in query is an essential feature of SQL. Rela- tional databases would be of little use without joins. This chapter focused on the formulation of the inner join. The inner join brings back data for which there is a match between both tables being joined. We also talked about an alternate way of specifying the inner join and the usefulness of specifying table aliases. In our next chapter, ‘‘Combi ning Tables with an Outer Join,’’ we will turn to another important type of join, the outer join. As mentioned, inner joins only Chapter 11 ■ Combining Tables with an Inner Join116 allow us to view data when there is a match between the tables being joined. So, if you have a customer with no orders, you won’t see any customer information when doing an inner join between a Customers and an Orders table. The outer join will allow you to view customer information even if there are no orders for a customer. In other words, the outer join lets us see data that we would not otherwise be able to obtain with an inner join. Looking Ahead 117 This page intentionally left blank chapter 12 Combining Tables with an Outer Join Keywords Introduced: LEFT JOIN, RIGHT JOIN, FULL JOIN We now advance from inner to outer joins. The main restriction of inner joins is that they requir e a match in all table s being joined to show any results . If you’re joining a C ustomers table to an Or ders table, no data for the customer is shown if that customer hasn’t yet placed an order. This may seem like a relatively unim- portant problem, but it often become s more significant with dif f er en t types of data. Let’s say, for example, that we have an Orders table and a Refunds table. The Refunds table is related to the Orders table by an OrderID. In other words, all refunds are tied to a specific order. The refund can’t exist unless the order exists. The problem arises when you want to see both orders and refunds in a single query. If you join these two tables with an inner join, you won’t see any orders if refunds were never issued against that order. Presumably, this will be the majority of your orders. In contrast, the outer join allows you to view orders even if they don’t have a matching refund, and it is therefore an essential tech- nique to understand and use. The Outer Join All the joins seen in the last chapter were inner joins. Since inner joins are the most common join type, SQL specifies these as a default, so you can specify an inner join using only the keyword JOIN. It isn’t necessary to state INNER JOIN. 119 In contrast to inner joins, there are three types of outer joins: LEFT OUTER JOIN , RIGHT OUTER JOIN, and FULL OUTER JOIN. These can be referred to as simply: LEFT JOIN, RIGHT JOIN, and FULL JOIN. In this case, the wo rd OUTER isn’t necessary. To summarize, my recommendation is to refer to the four join types as: ■ INNER JOIN ■ LEFT JOIN ■ RIGHT JOIN ■ FULL JOIN This keeps the syntax consistent and easy to remember. In our discussion of outer joins, we’re going to utilize three tables in our examples. First, there will be a Customers table with information about each customer. Second, there will be an Orders table with data on each order placed. Finally, we will add a Refunds table with information about any refunds that have been issued to customers. Figure 12.1 shows how these three tables are connected. Figure 12.1 Entity-relationship diagram. In contrast to the figure seen in the last chapter, the lines connecting the tables are now shown as arrows. You can see an arrow drawn from the CustomerID field of the Customers table to the CustomerID field of the Orders table. This arrow indicates that the link between the Customers and Orders tables is possibly one-sided in the sense that there may not be any orders for any given customer. Additionally, there may be multiple orders for a single customer. Similarly, the arrow drawn between the Orders and Refunds tables indicates that there may not be any refunds for any given order, and that there may be multiple refunds for an order. Chapter 12 ■ Combining Tables with an Outer Join120 . from the CustomerID field of the Customers table to the CustomerID field of the Orders table. This arrow indicates that the link between the Customers and Orders tables is possibly one-sided in the. Ahead The ability to join tables together in query is an essential feature of SQL. Rela- tional databases would be of little use without joins. This chapter focused on the formulation of the inner. using the AS keyword to specify both column and table aliases. It should also be mentioned that the AS keyword is completely optional. All of the AS keywords can be removed from the SELECT, and the

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