- Sams Teach Yourself SQL in 24 Hours, Third Edition
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: A SQL Concepts Overview
- Hour 1. Welcome to the World of SQL
- SQL Definition and History
- SQL Sessions
- Types of SQL Commands
- An Introduction to the Database Used in This Book
- Summary
- Q&A
- Workshop
- Part II: Building Your Database
- Hour 2. Defining Data Structures
- What Is Data?
- Basic Data Types
- Summary
- Q&A
- Workshop
- Hour 3. Managing Database Objects
- What Are Database Objects?
- What Is a Schema?
- A Table: The Primary Storage for Data
- Integrity Constraints
- Summary
- Q&A
- Workshop
- Hour 4. The Normalization Process
- Normalizing a Database
- Summary
- Q&A
- Workshop
- Hour 5. Manipulating Data
- Overview of Data Manipulation
- Populating Tables with New Data
- Updating Existing Data
- Deleting Data from Tables
- Summary
- Q&A
- Workshop
- Hour 6. Managing Database Transactions
- What Is a Transaction?
- What Is Transactional Control?
- Transactional Control and Database Performance
- Summary
- Q&A
- Workshop
- Part III: Getting Effective Results from Queries
- Hour 7. Introduction to the Database Query
- What Is a Query?
- Introduction to the <tt>SELECT</tt> Statement
- Examples of Simple Queries
- Summary
- Q&A
- Workshop
- Hour 8. Using Operators to Categorize Data
- What Is an Operator in SQL?
- Comparison Operators
- Logical Operators
- Conjunctive Operators
- Negating Conditions with the <tt>NOT</tt> Operator
- Arithmetic Operators
- Summary
- Q&A
- Workshop
- Hour 9. Summarizing Data Results from a Query
- What Are Aggregate Functions?
- Summary
- Q&A
- Workshop
- Hour 10. Sorting and Grouping Data
- Why Group Data?
- The <tt>GROUP BY</tt> Clause
- <tt>GROUP BY</tt> Versus <tt>ORDER BY</tt>
- The <tt>HAVING</tt> Clause
- Summary
- Q&A
- Workshop
- Hour 11. Restructuring the Appearance of Data
- The Concepts of ANSI Character Functions
- Various Common Character Functions
- Miscellaneous Character Functions
- Mathematical Functions
- Conversion Functions
- The Concept of Combining Character Functions
- Summary
- Q&A
- Workshop
- Hour 12. Understanding Dates and Times
- How Is a Date Stored?
- Date Functions
- Date Conversions
- Summary
- Q&A
- Workshop
- Part IV: Building Sophisticated Database Queries
- Hour 13. Joining Tables in Queries
- Selecting Data from Multiple Tables
- Types of Joins
- Join Considerations
- Summary
- Q&A
- Workshop
- Hour 14. Using Subqueries to Define Unknown Data
- What Is a Subquery?
- Embedding a Subquery Within a Subquery
- Summary
- Q&A
- Workshop
- Hour 15. Combining Multiple Queries into One
- Single Queries Versus Compound Queries
- Why Would I Ever Want to Use a Compound Query?
- Compound Query Operators
- Using an <tt>ORDER BY</tt> with a Compound Query
- Using <tt>GROUP BY</tt> with a Compound Query
- Retrieving Accurate Data
- Summary
- Workshop
- Q&A
- Part V: SQL Performance Tuning
- Hour 16. Using Indexes to Improve Performance
- What Is an Index?
- How Do Indexes Work?
- The <tt>CREATE INDEX</tt> Command
- Types of Indexes
- When Should Indexes Be Considered?
- When Should Indexes Be Avoided?
- Summary
- Q&A
- Workshop
- Hour 17. Improving Database Performance
- What Is SQL Statement Tuning?
- Database Tuning Versus SQL Tuning
- Formatting Your SQL Statement
- Full Table Scans
- Other Performance Considerations
- Performance Tools
- Summary
- Q&A
- Workshop
- Part VI: Using SQL to Manage Users and Security
- Hour 18. Managing Database Users
- Users Are the Reason
- The Management Process
- Tools Utilized by Database Users
- Summary
- Q&A
- Workshop
- Hour 19. Managing Database Security
- What Is Database Security?
- How Does Security Differ from User Management?
- What Are Privileges?
- Controlling User Access
- Controlling Privileges Through Roles
- Summary
- Q&A
- Workshop
- Part VII: Summarized Data Structures
- Hour 20. Creating and Using Views and Synonyms
- What Is a View?
- Creating Views
- Dropping a View
- What Is a Synonym?
- Summary
- Q&A
- Workshop
- Hour 21. Working with the System Catalog
- What Is the System Catalog?
- How Is the System Catalog Created?
- What Is Contained in the System Catalog?
- Examples of System Catalog Tables by Implementation
- Querying the System Catalog
- Updating System Catalog Objects
- Summary
- Q&A
- Workshop
- Part VIII: Applying SQL Fundamentals in Today's World
- Hour 22. Advanced SQL Topics
- Advanced Topics
- Cursors
- Stored Procedures and Functions
- Triggers
- Dynamic SQL
- Call-Level Interface
- Using SQL to Generate SQL
- Direct Versus Embedded SQL
- Summary
- Q&A
- Workshop
- Hour 23. Extending SQL to the Enterprise, the Internet, and the Intranet
- SQL and the Enterprise
- Accessing a Remote Database
- Accessing a Remote Database Through a Web Interface
- SQL and the Internet
- SQL and the Intranet
- Summary
- Q&A
- Workshop
- Hour 24. Extensions to Standard SQL
- Various Implementations
- Examples of Extensions from Some Implementations
- Interactive SQL Statements
- Summary
- Q&A
- Workshop
- Part IX: Appendixes
- Appendix A. Common SQL Commands
- SQL Statements
- SQL Clauses
- Appendix B. Using MySQL for Exercises
- Windows Installation Instructions
- Linux Installation Instructions
- Appendix C. Answers to Quizzes and Exercises
- Hour 1, "Welcome to the World of SQL"
- Hour 2, "Defining Data Structures"
- Hour 3, "Managing Database Objects"
- Hour 4, "The Normalization Process"
- Hour 5, "Manipulating Data"
- Hour 6, "Managing Database Transactions"
- Hour 7, "Introduction to the Database Query"
- Hour 8, "Using Operators to Categorize Data"
- Hour 9, "Summarizing Data Results from a Query"
- Hour 10, "Sorting and Grouping Data"
- Hour 11, "Restructuring the Appearance of Data"
- Hour 12, "Understanding Dates and Time"
- Hour 13, "Joining Tables in Queries"
- Hour 14, "Using Subqueries to Define Unknown Data"
- Hour 15, "Combining Multiple Queries into One"
- Hour 16, "Using Indexes to Improve Performance"
- Hour 17, "Improving Database Performance"
- Hour 18, "Managing Database Users"
- Hour 19, "Managing Database Security"
- Hour 20, "Creating and Using Views and Synonyms"
- Hour 21, "Working with the System Catalog"
- Hour 22, "Advanced SQL Topics"
- Hour 23, "Extending SQL to the Enterprise, the Internet, and the Intranet"
- Hour 24, "Extensions to Standard SQL"
- Appendix D. <tt>CREATE TABLE</tt> Statements for Book Examples
- <tt>EMPLOYEE_TBL</tt>
- <tt>EMPLOYEE_PAY_TBL</tt>
- <tt>CUSTOMER_TBL</tt>
- <tt>ORDERS_TBL</tt>
- <tt>PRODUCTS_TBL</tt>
- Appendix E. <tt>INSERT</tt> Statements for Data in Book Examples
- <tt>INSERT</tt> Statements
- Appendix F. Glossary
- Appendix G. Bonus Exercises
An Introduction to the Database Used in This Book
Before continuing with your journey through SQL fundamentals, the next step is introducing the tables and data that you use throughout the course of instruction for the next 23 one-hour lessons. The next two sections provide an overview of the specific tables (the database) being used, their relationship to one another, their structure, and examples of the data contained.
Diagram of the Tables in This Book
Figure 1.4 reveals the relationship between the tables that you use for examples, quiz questions, and exercises in this book. Each table is identified by the table name as well as each residing field in the table. Follow the mapping lines to compare the specific tables' relationship through a common field, in most cases referred to as the primary key (discussed in Hour 3).
Figure 1.4 Table relationships for this book.
Table-Naming Standards
Table-naming standards, as well as any standard within a business, are critical to maintaining control. After studying the tables and data in the previous sections, you probably noticed that each table's suffix is _TBL. This is a naming standard selected for use, such as what's been used at various client sites. The _TBL simply tells you that the object is a table; there are many different types of objects in a relational database. For example, you will see that the suffix _INX is used to identify indexes on tables in later hours. Naming standards exist almost exclusively for overall organization and assist immensely in the administration of any relational database. Remember, the use of a suffix is not mandatory when naming database objects.
A Look at the Data
This section offers a picture of the data contained in each one of the tables used in this book. Take a few minutes and study the data, the variations, and the relationships between the tables and the data itself. Notice that some fields may not require data, which is specified when each table is created in the database.
EMPLOYEE_TBL EMP_ID LAST_NAM FIRST_NA M ADDRESS CITY ST ZIP PHONE --------- -------- -------- - --------------- ------------ -- ----- ---------- 311549902 STEPHENS TINA D RR 3 BOX 17A GREENWOOD IN 47890 3178784465 442346889 PLEW LINDA C 3301 BEACON INDIANAPOLIS IN 46224 3172978990 213764555 GLASS BRANDON S 1710 MAIN ST WHITELAND IN 47885 3178984321 313782439 GLASS JACOB 3789 RIVER BLVD INDIANAPOLIS IN 45734 3175457676 220984332 WALLACE MARIAH 7889 KEYSTONE INDIANAPOLIS IN 46741 3173325986 443679012 SPURGEON TIFFANY 5 GEORGE COURT INDIANAPOLIS IN 46234 3175679007 EMPLOYEE_PAY_TBL EMP_ID POSITION DATE_HIRE PAY_RATE DATE_LAST SALARY BONUS --------- --------------- ----------- -------- ------------- ----------- ------ 311549902 MARKETING 23-MAY-89 01-MAY-99 4000 442346889 TEAM LEADER 17-JUN-90 14.75 01-JUN-99 213764555 SALES MANAGER 14-AUG-94 01-AUG-99 3000 2000 313782439 SALESMAN 28-JUN-97 2000 1000 220984332 SHIPPER 22-JUL-96 11 01-JUL-99 443679012 SHIPPER 14-JAN-91 15 01-JAN-99 CUSTOMER_TBL CUST_ID CUST_NAME ADDRESS CUST_CITY ST ZIP CUST_PHONE CUST_FAX ------- --------------- ---------- ------------ -- ----- ------------ -------- 232 LESLIE GLEASON 798 HARDAW INDIANAPOLIS IN 47856 3175457690 AY DR 109 NANCY BUNKER APT A 4556 BROAD RIPPLE IN 47950 3174262323 WATERWAY 345 ANGELA DOBKO RR3 BOX 76 LEBANON IN 49967 7658970090 090 WENDY WOLF 3345 GATEW INDIANAPOLIS IN 46224 3172913421 AY DR 12 MARYS GIFT SHOP 435 MAIN S DANVILLE IL 47978 3178567221 3178523434 T 432 SCOTTYS MARKET RR2 BOX 17 BROWNSBURG IN 45687 3178529835 3178529836 3 333 JASONS AND DALL LAFAYETTE INDIANAPOLIS IN 46222 3172978886 3172978887 AS GOODIES SQ MALL 21 MORGANS CANDIES 5657 W INDIANAPOLIS IN 46234 3172714398 AND TREATS TENTH ST 43 SCHYLERS NOVELT 17 MAPLE LEBANON IN 48990 3174346758 IES ST 287 GAVINS PLACE 9880 ROCKV INDIANAPOLIS IN 46244 3172719991 3172719992 ILLE RD 288 HOLLYS GAMEARAMA 567 US 31 WHITELAND IN 49980 3178879023 590 HEATHERS FEATHE 4090 N SHA INDIANAPOLIS IN 43278 3175456768 RS AND THINGS DELAND AVE 610 REGANS HOBBIES 451 GREEN PLAINFIELD IN 46818 3178393441 3178399090 560 ANDYS CANDIES RR 1 NASHVILLE IN 48756 8123239871 BOX 34 221 RYANS STUFF 2337 S INDIANAPOLIS IN 47834 3175634402 SHELBY ST 175 CAMERON'S PIES 178 N TIBBS AVON IN 46234 3174543390 290 CALEIGH'S KITTENS 244 WEST ST LEBANON IN 47890 3174867754 56 DANIELS SPANIELS 17 MAIN ST GREENWOOD IN 46578 3172319908 978 AUTUMN'S BASKETS 5648 CENTER ST SOUTHPORT IN 45631 3178887565 ORDERS_TBL ORD_NUM CUST_ID PROD_ID QTY ORD_DATE ---------- ------- ----------------- --- --------- 56A901 232 11235 1 22-OCT-99 56A917 12 907 100 30-SEP-99 32A132 43 222 25 10-OCT-99 16C17 090 222 2 17-OCT-99 18D778 287 90 10 17-OCT-99 23E934 432 13 20 15-OCT-99 PRODUCTS_TBL PROD_ID PROD_DESC COST ---------- ------------------------------ ------ 11235 WITCHES COSTUME 29.99 222 PLASTIC PUMPKIN 18 INCH 7.75 13 FALSE PARAFFIN TEETH 1.10 90 LIGHTED LANTERNS 14.50 15 ASSORTED COSTUMES 10.00 9 CANDY CORN 1.35 6 PUMPKIN CANDY 1.45 87 PLASTIC SPIDERS 1.05 119 ASSORTED MASKS 4.95
A Closer Look at What Comprises a Table
The storage and maintenance of valuable data is the reason for any database's existence. You have just viewed the data that is used to explain SQL concepts in this book. The following sections take a closer look at the elements within a table. Remember, a table is the most common and simplest form of data storage.
A Field
Every table is broken up into smaller entities called fields. The fields in the PRODUCTS_TBL table consist of PROD_ID, PROD_DESC, and COST. These fields categorize the specific information that is maintained in a given table. A field is a column in a table that is designed to maintain specific information about every record in the table. |
A Record, or Row, of Data
A record, also called a row of data, is each individual entry that exists in a table. Looking at the last table, PRODUCTS_TBL, consider the following first record in that table: |
11235 WITCHES COSTUME 29.99
The record is obviously composed of a product identification, product description, and unit cost. For every distinct product, there should be a corresponding record in the PRODUCTS_TBL table. A record is a horizontal entity in a table.
A row of data is an entire record in a relational database table. |
A Column
A column is a vertical entity in a table that contains all information associated with a specific field in a table. For example, a column in the PRODUCTS_TBL having to do with the product description would consist of the following: |
WITCHES COSTUME PLASTIC PUMPKIN 18 INCH FALSE PARAFFIN TEETH LIGHTED LANTERNS ASSORTED COSTUMES CANDY CORN PUMPKIN CANDY PLASTIC SPIDERS ASSORTED MASKS
This column is based on the field PROD_DESC, the product description. A column pulls information about a certain field from every record within a table.
The Primary Key
A primary key is a column that makes each row of data in the table unique in a relational database. The primary key in the PRODUCTS_TBL table is PROD_ID, which is typically initialized during the table creation process. The nature of the primary key is to ensure that all product identifications are unique, so that each record in the PRODUCTS_TBL table has its own PROD_ID. Primary keys alleviate the possibility of a duplicate record in a table and are used in other ways, which you read about in Hour 3.
A NULL Value
NULL is the term used to represent a missing value. A NULL value in a table is a value in a field that appears to be blank. A field with a NULL value is a field with no value. It is very important to understand that a NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation. Notice that in the EMPLOYEE_TBL table, not every employee has a middle initial. Those records for employees who do not have an entry for middle initial signify a NULL value. |
Additional table elements are discussed in detail during the next two hours.
Examples and Exercises Used in This Book
As stated before, most of the examples used in this book were generated using Oracle. This was done for several reasons: consistency, the popularity of the Oracle database, and Oracle's high compliance to the ANSI SQL-99 standard.
Many exercises in this book use Oracle examples. We have also included exercises for as much of the book as possible using MySQL. We decided to use MySQL in this edition for exercises because MySQL is open source and may be freely distributed, whereas Oracle is not. MySQL is popular, easy to download, and easy to install. MySQL is available for most operating system platforms, including Windows and Linux. Note that because MySQL is not as compliant to the SQL-99 standard as Oracle, MySQL exercises may be somewhat limited in some hours of instruction. Due to MySQL's lack of compliance to several parts of the SQL standard, we have opted to keep our Oracle examples.