The Language of SQL- P20 pdf

5 236 0
The Language of SQL- P20 pdf

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

Thông tin tài liệu

or you can use this equivalent statement that utilizes the IN operator: SELECT CustomerName, State FROM Orders WHERE State IN ('IL', 'NY' ) In either case, the data retrieved is: CustomerName State William Smith IL Brenda Harper NY Notice that commas are used to separate all values within the parentheses fol- lowing the IN keyword. The usefulness of the IN operator may not be obvious in this example, where only two states are listed. However, the IN can just as easily be used in situations where you want to list dozens of specific values. This greatly reduces the amount of typing required for such a statement. Another handy use for the IN operator comes in situations where you want to use data from Excel in a SQL statement. If you want to obtain multiple values from adjacent cells in a spreadsheet for your SQL statement, Excel allows you to copy those values with a comma delimiter. This result can then be pasted inside the parentheses following the IN operator. As with the BETWEEN operator, the NOT operator can be used with the IN,as shown in this example: SELECT CustomerName, State FROM Orders WHERE State NOT IN ('IL', 'NY') This retrieves this data: CustomerName State Natalie Lopez CA The IN Operator 81 One final note about the IN operator. There is a second way to use the IN, which is substantially different from the syntax just discussed. In the second format of the IN operator, an entire SELECT statement is specified within the parentheses, allowing the individual values to be created logically when needed. This is called a subquery, and it will be covered in detail in Chapter 14. Boolean Logic and NULL Values At the start of this chapter, I stated that the Boolean logic in SQL evaluates complex expressions as either true or false. This assertion was not completely correct. When evaluating the conditions in a WHERE clause, there are actually three possibilities: true, false, and unknown. The possibility of unknown derives from the fact that columns in SQL databases are sometimes allowed to have a NULL value. As mentioned in Chapter 1, NULL values are those for which there is an absence of data. SQL provides a special keyword to test for the presence of NULL values for a column specified in a WHERE clause. The keyword is IS NULL. Let’s look at an example taken from the following Products table: ProductID ProductDescription Weight 1 Printer A NULL 2 Printer B 0 3 Monitor C 2 4 Laptop D 4 For this example, you have to imagine that as rows are added to the Products table, they are initially not given any value in the Weight column. They are initially given a value of NULL, and a user of the system later assigns a weight to the product. Let’s say that you attempt to use the following SELECT to find products missing a weight: SELECT ProductDescription, Weight FROM Products WHERE Weight ¼ 0 Chapter 8 ■ Boolean Logic82 This would return: ProductDescription Weight Printer B 0 This is not quite what you want. A weight equal to zero is not the same as a weight with a NULL value. To correct this, you need to issue: SELECT ProductDescription, Weight FROM Products WHERE Weight ¼ 0 OR Weight IS NULL This returns: ProductDescription Weight Printer A NULL Printer B 0 The IS NULL keyword can also be negated as IS NOT NULL, which allows you to retrieve all rows that do not have NULL for the specified column. It should be mentioned that the ISNULL function, discussed in Chapter 4, can provide an alternative to the IS NULL keyword. The equivalent of the previous SELECT statement, utilizing the ISNULL function is: SELECT ProductDescription, Weight FROM Products WHERE ISNULL(Weight, 0) ¼ 0 This SELECT retrieves the same two rows. The ISNULL function converts all values for the Weight column with a value of NULL to 0. Since the WHERE clauses tests for a value of 0, it, in effect, tests for values of 0 or NULL. Boolean Logic and NULL Values 83 You can also combine the ISNULL function and the IS NULL keyword in a single SELECT statement, such as: SELECT ProductDescription, ISNULL (Weight, 0) AS 'Weight' FROM Products WHERE Weight ¼ 0 OR Weight IS NULL This produces this dat a: ProductDescription Weight Printer A 0 Printer B 0 Looking Ahead This chapter covered the important topic of how to create complex expressions of selection logic. The basic Boolean operators used in this endeavor were AND, OR, and NOT. We also discussed the BETWEEN and IN operators, which allow for a more concise statement of the AND and OR operators in certain situations. Parentheses are another essential tool in the formulation of complex expressions. By using parentheses or multiple sets of parentheses, you can create almost every imaginable logical condition. Finally, we talked about how to deal with NULL values when selecting data. In our next chapter, we’re going to take an interesting detour into some alter- native ways to specify selection criteria. We’re first going to look at the topic of pattern matching. This will allow you to match by portions of a word or phrase and to do such things as find all products that contain the word ‘‘white.’’ The second half of the chapter will turn to the possibility of matching by the sound of a word or phrase. This, for example, will let you find all customers who have a first name that sounds like Haley, even if the name is spelled Hailey. Chapter 8 ■ Boolean Logic84 chapter 9 Inexact Matches Keywords Introduced: LIKE, SOUNDEX, DIFFERENCE I would like to now turn to two situations in which the data to be retrieved is not precisely defined. In the first circumstance, we will look at the need to retrieve data based on inexact matches with words or phrases. For example, you may be interested in finding customers whose name contains the word ‘‘bank.’’ In the second situation, we will extend the idea of inexact matches to include the possibility of matching by the sound of a word or phrase. For instance, you may be interested in customers whose name sounds like ‘‘Smith,’’ even though it may not be spelled exactly that way. Pattern Matching Let’s first look at inexact matches within phrases, which is often referred to as pattern matching. In SQL, the LIKE operator is utilize d in th e WHERE clause to enable you to find matches against parts of a column value. The LIKE ope rator requires the use of special wildcard characters to specify exactly how the match is to work. Let’s start with an example from the Movies table shown on the next page. Our first example of a SELECT statement with a LIKE operator is: SELECT MovieTitle AS 'Movie' FROM Movies WHERE MovieTitle LIKE '%LOVE%' 85 . statement of the AND and OR operators in certain situations. Parentheses are another essential tool in the formulation of complex expressions. By using parentheses or multiple sets of parentheses,. Lopez CA The IN Operator 81 One final note about the IN operator. There is a second way to use the IN, which is substantially different from the syntax just discussed. In the second format of the IN. customers whose name contains the word ‘‘bank.’’ In the second situation, we will extend the idea of inexact matches to include the possibility of matching by the sound of a word or phrase. For instance,

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

Từ khóa liên quan

Mục lụ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

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan