The Language of SQL- P9 pdf

5 274 0
The Language of SQL- P9 pdf

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

Thông tin tài liệu

Table Aliases In addition to providing alternate names for columns, aliases can also be speci- fied for tables, using the same AS keyword. There are three general reasons for using table aliases. The first reason relates to tables with obscure or complex names. For example, if a table is named Orders123, you can use the following SELECT to give it an alias of Orders. SELECT LastName FROM Orders123 AS Orders Unlike column aliases, table aliases are not enclosed in quotes. When using table aliases, you have the option of using the alias as a prefix for any selected columns. For example, the above could also be written as: SELECT Orders.LastName FROM Orders123 AS Orders The prefix Orders has now been added as a prefix to LastName, using a period to separate the prefix from the column name. In this situation, the prefix wasn’t necessary. However, when data is selected from multiple tables, the prefix will sometimes be required. This will be seen in later chapters. DATABASE DIFFERENCES: Oracle In Oracle, table aliases are specified without the AS keyword. The syntax for the previous statement in Oracle is: SELECT Orders.LastName FROM Orders123 Orders; Two remaining reasons for using table aliases will be covered in Chapters 11 and 14: ■ Situations when selecting from multiple tables ■ Situations when using a subquery in a SELECT statement Chapter 3 ■ Calculations and Aliases26 The meaning of the term subquery will become clear in Chapter 14 when the topic is covered in detail. Looking Ahead In this chapter, you learned about three general ways to create calculated fields in a SELECT statement. First, literal values can be used to select specific words or values. Second, arithmetic calculations can be used to perform calculations on single or multiple columns. Third, concatenation can be used to combine col- umns and literal values together. We also discussed the related topic of column aliases, which are often employed when using calculated fields. In the next chapter, we’ll be moving on to the subject of functions, which provide a slightly more complex way to perform calculations. As mentioned before, we’re not quite at the point where you can apply selection criteria to your statements. I’m still building on the basics of what can be done with the columnlist in a SELECT. Don’t worry. We’ll get to the exciting stuff soon enough. In the meantime, your patience in sticking with this methodical approach will soon pay off. Looking Ahead 27 This page intentionally left blank chapter 4 Using Functions Keywords Introduced: LEFT, RIGHT, SUBSTRING, LTRIM, RTRIM, CONCAT, UPPER, LOWER, GETDATE/NOW/CURRENT_DATE, DATEPART/DATE_FORMAT, DATEDIFF, ROUND, RAND, PI, CAST, ISNULL/IFNULL/NVL For those of you familiar with spreadsheet software such as Microsoft Excel, you know that functions provide a huge amount of functionality for the typical spreadsheet user. Without the ability to use functions, most of the data available in spreadsheets would be of limited value. The same is true in the world of SQL. Your familiarity with some of the most commonly used SQL functions will greatly enhance your ability to generate dynamic results for those who will be using your reports. This chapte r c ove r s a wide variety of s om e of t he m os t com monl y u se d fun ction s i n four different categories: character functions, date/time functions, numeric func- tions, and con ve rs ion functions. Additiona l ly, we will talk ab out composite fun c - tions, which ar e a way o f com bin in g mu ltipl e f un ction s in to a sin gl e ex pr ess ion . The Function of Functions Similar to the calculations covered in the previous chapter, functions provide another way to manipulate data. As was seen, calculations involve multiple fields, either with arithmetic operators such as multiplication or by concatenation. In contrast, functions are often performed on a single column. 29 What is a function? A function is merely a rule for transforming a value (or values) into another value, using a specific formula. For example, the function SUBSTRING can be used to determine that the first initial of the name JOAN is J. There are two types of functions: scalar and aggregate. The term scalar comes from mathematics and refers to an operation that is done on a single number. In computer usage, it means that the function is performed on data in a single row. For example, the LTRIM function removes leading spaces from one specified value. In contrast, aggregate functions are meant to be performed on a larger set of data. For example, the SUM function can be used to calculate the sum of all the values of a specified column. Since aggregate functions apply to sets or groups of data, we will leave our discussion of them to Chapter 10. Every SQL database offers dozens of scalar functions. The functions vary widely between databases, in terms of their names and also how they work. As a result, we will only cover a few representative examples of some of the more useful functions. The most common types of scalar functions can be classified under three cate- gories: character, date/time, and numeric. Obviously, these are functions that allow you to manipulate character, date/time, or numeric datatypes. In addition, you will learn about some useful conversion functions that can be used to convert data from one datatype to another. Character Functions Character functions are those functions that enable you to manipulate character data. Just as character datatypes are sometimes called string datatypes, character functions are sometimes called string functions. I’m going to cover these eight examples of character functions: LEFT, RIGHT, SUBSTRING, LTRIM, RTRIM, CONCAT, UPPER, and LOWER. In this chapter, rather than retrieve data from specified tables, I’m going to simply use SELECT statements with literal values. Let’s start with our first example, which is for the LEFT function. When you issue this SQL command: SELECT LEFT ('sunlight',3) AS 'The Answer' Chapter 4 ■ Using Functions30 . ability to use functions, most of the data available in spreadsheets would be of limited value. The same is true in the world of SQL. Your familiarity with some of the most commonly used SQL functions. leave our discussion of them to Chapter 10. Every SQL database offers dozens of scalar functions. The functions vary widely between databases, in terms of their names and also how they work. As a. function SUBSTRING can be used to determine that the first initial of the name JOAN is J. There are two types of functions: scalar and aggregate. The term scalar comes from mathematics and refers to an operation

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