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

The Language of SQL- P17 pot

5 233 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

In this example, only one row meets the qualification that the QuantityPurchased column be greater than 6. Although not as commonly used, it’s also possible to use the “is greater than” operator with a text column. This example: SELECT FirstName, LastName FROM Orders WHERE LastName > 'K' returns: FirstName LastName William Smith Natalie Lopez Since the test is for last names greater than K, it only brings back Smith and Lopez, but not Harper. When applied to text fields, the greater than and less than operators indicate selection by the alphabetic order of the values. In this case, Smith and Lopez are returned, since S and L are after K in the alphabet. Finally, it should be noted that all of these operators can also be used with the WHEN keyword in the searched format of the CASE expression. For example, a valid CASE expression might be: CASE WHEN column1 > value1 THEN result1 END Limiting Rows What do you do if you want to select a small subset of the rows in a table, but you don’t care which rows are returned? Let’s say that you have a table with 50,000 rows, and you want to see just a few rows of data to see what it looks like. It wouldn’t make sense to use the WHERE clause for this purpose, since you don’t care which rows are returned. The solution is to use a special keyword to specify that you want to limit how many rows are returned. This is another instance where the syntax differs among Chapter 7 ■ Row-Based Logic66 databases. In Microsoft SQL Server, the keyword that accomplishes this task is TOP. The general format is: SELECT TOP number columnlist FROM table DATABASE DIFFERENCES: MySQL and Oracle MySQL uses the keyword LIMIT rather than TOP. The general format is: SELECT columnlist FROM table LIMIT number Oracle uses the keyword ROWNUM rather than TOP. The ROWNUM keyword needs to be specified in a WHERE clause, as follows: SELECT columnlist FROM table WHERE ROWNUM <= number In the remainder of this chapter, you’ll see statements using the Microsoft TOP keyword. If you’re using MySQL or Oracle, you can simply substitute the equivalent LIMIT or ROWNUM keywords. Let’s say that you want to see the first 10 rows from a table. The SELECT to accomplish this looks like: SELECT TOP 10 * FROM table This statem ent returns all columns in the first 10 rows from the table. Like any SELECT statement without an ORDER BY clause, there’s no way to predict which 10 rows will be returned. It depends on how the data is physically stored in the table. Limiting Rows 67 Similarly, you can list specific columns to return: SELECT TOP 10 column1, column2 FROM table In essence, the TOP keyword accomplishes something similar to the WHERE clause, because it permits you to return a small subset of rows in the specified table. Keep in mind, though, that rows returned using the TOP keyword are not a true random sample in a statistical sense. They’re only the first rows that qualify, based on how the data is physically stored in the database. Limiting Rows with a Sort Another use of the TOP keyword is to use it in combination with the ORDER BY clause to obtain a certain number of rows with the highest values, based on a specified catego ry. This type of data selection is commonly referred to as a Top N selection. Here’s an example, taken from this Books table: BookID Title Author CurrentMonthSales 1 Pride and Prejudice Austen 15 2 Animal Farm Orwell 7 3 Merchant of Venice Shakespeare 5 4 Romeo and Juliet Shakespeare 8 5 Oliver Twist Dickens 3 6 Candide Voltaire 9 7 The Scarlet Letter Hawthorne 12 8 Hamlet Shakespeare 2 Let’s say that you want to see the three books that sold the most in the current month. The SELECT to accomplish this is: SELECT TOP 3 Title AS 'Book Title', CurrentMonthSales AS 'Quantity Sold' FROM Books ORDER BY CurrentMonthSales DESC Chapter 7 ■ Row-Based Logic68 The output is: Book Title Quantity Sold Pride and Prejudice 15 The Scarlet Letter 12 Candide 9 Let’s examine this statement in some detail. The TOP 3 in the second line indicates that only three rows are to be returned. The main question to ask is how it determines which three rows to display. The answer is found in the ORDER BY clause. If there were no ORDER BY clause, then SELECT would simply bring back any three rows of data, but that’s not what you want. You’re looking for the three rows with the highest sales. In order to accomplish this, you need to sort the rows by the CurrentMonthSales column in descending order. Why descending? Because when you sort in descending order, the highest numbers appear first. If you had sorted in ascending order, you would get the books with the least amount of sales, not the most. Now, let’s add one more twist to this scenario. Let’s say that you only want to see the book by Shakespeare that sold the most. In order to accomplish this, you need to add a WHERE clause, as follows: SELECT TOP 1 Title AS 'Book Title', CurrentMonthSales AS 'Quantity Sold' FROM Books WHERE Author ¼ 'Shakespeare' ORDER BY CurrentMonthSales DESC This brings back this data: Book Title Quantity Sold Romeo and Juliet 8 The WHERE clause adds the qualification that you are only looking at books by Shakespeare. You also revised the TOP keyword to specify TOP 1, indicating that you only want to see one row of data. Limiting Rows with a Sort 69 DATABASE DIFFERENCES: Oracle The procedure for limiting and sorting rows in Oracle is a bit more complex, since the ROWNUM is in the WHERE clause in Oracle syntax. You need to sort the data first and then apply the ROWNUM selection criteria. The general format is: SELECT * FROM (SELECT columnlist FROM table ORDER BY columnlist DESC) WHERE ROWNUM <= number This is an early example of a subquery, which will be covered in detail in Chapter 14. In brief, this statement consists of two separate SELECT statements. The inner SELECT, enclosed in parentheses, sorts the desired data by the specified columnlist, in descending order. The outer SELECT statement then retrieves data from the inner SELECT using the ROWNUM keyword to limit the number of rows that are displayed. Looking Ahead This chapter introduced the topic of how to apply selection criteria to queries. A number of basic operators, such as equals and greater than, were introduced. The ability to specify some basic selection criteria goes a long way towards making our SELECT statement truly useful. With the WHERE clause, you can now issue a statement that retrieves all customers from the state of New York. The related topic of limiting the number of rows returned in a query was also covered in this chapter. Finally, the ability to limit rows in combination with an ORDER BY clause allows for a useful Top N type of data selection. In our next chapter, “Boolean Logic,” I am going to great ly enhance our selection criteria capabilities by introducing a bunch of new keywords that add sophisti- cated logic to the WHERE clause. Yes, it’s true that you can now select customers from the state of New York, but what if you wanted to select customers who are in New York or California, but not in Buffalo or Los Angeles? The keywords covered in the next chapter will allow you to do that. Chapter 7 ■ Row-Based Logic70 . Sort Another use of the TOP keyword is to use it in combination with the ORDER BY clause to obtain a certain number of rows with the highest values, based on a specified catego ry. This type of data. consists of two separate SELECT statements. The inner SELECT, enclosed in parentheses, sorts the desired data by the specified columnlist, in descending order. The outer SELECT statement then retrieves. statement truly useful. With the WHERE clause, you can now issue a statement that retrieves all customers from the state of New York. The related topic of limiting the number of rows returned in a query

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