- 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
Date Functions
Date functions are available in SQL depending on the options with each specific implementation. Date functions, similar to character string functions, are used to manipulate the representation of date and time data. Available date functions are often used to format the output of dates and time in an appealing format, compare date values with one another, compute intervals between dates, and so on.
The Current Date
You may have already raised the question: How do I get the current date from the database? The need to retrieve the current date from the database may originate from several situations, but the current date is normally returned either to compare to a stored date or to return the value of the current date as some sort of timestamp.
The current date is ultimately stored on the host computer for the database, and is called the system date. The database, which interfaces with the appropriate operating system, has the capability to retrieve the system date for its own purpose or to resolve database requests, such as queries. |
Take a look at a couple of methods of attaining the system date based on commands from two different implementations.
Sybase uses a function called GETDATE() to return the system date. This function is used in a query as follows. The output is what would return if today's current date were New Year's Eve for 1999.
SELECT GETDATE() Dec 31, 1999
Oracle uses what is calls a pseudocolumn, SYSDATE, to retrieve the current date. SYSDATE acts as any other column in a table and can be selected from any table in the database, although it is not actually part of the table's definition.
To return the system date in Oracle, the following statement returns the output if today were New Year's Eve before 2002:
SELECT SYSDATE FROM TABLE_NAME 31-DEC-01
Time Zones
The use of time zones may be a factor when dealing with date and time information. For instance, a time of 6:00 p.m. in central United States does not equate to the same time in Australia, although the actual point in time is the same. Some of us who live within the daylight saving time zone are used to adjusting our clocks twice a year. If time zones are considerations when maintaining data in your case, you may find it necessary to consider time zones and perform time conversions, if available with your SQL implementation.
The following are some common time zones and their abbreviations:
Abbreviation |
Definition |
AST, ADT |
Atlantic standard, daylight time |
BST, BDT |
Bering standard, daylight time |
CST, CDT |
Central standard, daylight time |
EST, EDT |
Eastern standard, daylight time |
GMT |
Greenwich mean time |
HST, HDT |
Alaska/Hawaii standard, daylight time |
MST, MDT |
Mountain standard, daylight time |
NST |
Newfoundland standard, daylight time |
PST, PDT |
Pacific standard, daylight time |
YST, YDT |
Yukon standard, daylight time |
The following table shows examples of time zone differences based on a given time:
Time Zone |
Time |
AST |
June 12th, 2002 at 1:15 PM |
BST |
June 12th, 2002 at 6:15 AM |
CST |
June 12th, 2002 at 11:15 AM |
EST |
June 12th, 2002 at 12:15 PM |
GMT |
June 12th, 2002 at 5:15 PM |
HST |
June 12th, 2002 at 7:15 AM |
MST |
June 12th, 2002 at 10:15 AM |
NST |
June 12th, 2002 at 1:45 PM |
PST |
June 12th, 2002 at 9:15 AM |
YST |
June 12th, 2002 at 8:15 AM |
Adding Time to Dates
Days, months, and other parts of time can be added to dates for the purpose of comparing dates to one another, or to provide more specific conditions in the WHERE clause of a query.
Intervals can be used to add periods of time to a DATETIME value. As defined by the standard, intervals are used to manipulate the value of a DATETIME value, as in the following examples:
DATE '1999-12-31' + INTERVAL '1' DAY '2000-01-01' DATE '1999-12-31' + INTERVAL '1' MONTH '2000-01-31'
The following is an example using the SQL Server function DATEADD:
SELECT DATEADD(MONTH, 1, DATE_HIRE) FROM EMPLOYEE_PAY_TBL; DATE_HIRE ADD_MONTH --------- --------- 23-MAY-89 23-JUN-89 17-JUN-90 17-JUL-90 14-AUG-94 14-SEP-94 28-JUN-97 28-JUL-97 22-JUL-96 22-AUG-96 14-JAN-91 14-FEB-91 6 rows affected.
The following example uses the Oracle function ADD_MONTHS:
SELECT DATE_HIRE, ADD_MONTHS(DATE_HIRE,1) FROM EMPLOYEE_PAY_TBL; DATE_HIRE ADD_MONTH --------- --------- 23-MAY-89 23-JUN-89 17-JUN-90 17-JUL-90 14-AUG-94 14-SEP-94 28-JUN-97 28-JUL-97 22-JUL-96 22-AUG-96 14-JAN-91 14-FEB-91 6 rows selected.
To add one day to a date in Oracle, use the following:
SELECT DATE_HIRE, DATE_HIRE + 1 FROM EMPLOYEE_PAY_TBL WHERE EMP_ID = '311549902'; DATE_HIRE DATE_HIRE --------- --------- 23-MAY-89 24-MAY-89 1 row selected.
Notice that these examples in SQL Server and Oracle, although they differ syntactically from the ANSI examples, derive their results based on the same concept as described by the SQL standard.
Comparing Dates and Time Periods
OVERLAPS is a powerful standard SQL conditional operator for DATETIME values. The OVERLAPS operator is used to compare two timeframes and return the Boolean value TRUE or FALSE, depending on whether the two timeframes overlap. The following comparison returns the value TRUE:
(TIME '01:00:00' , TIME '05:59:00') OVERLAPS (TIME '05:00:00' , TIME '07:00:00')
The following comparison returns the value FALSE:
(TIME '01:00:00' , TIME '05:59:00') OVERLAPS (TIME '06:00:00 , TIME '07:00:00')
Miscellaneous Date Functions
The following list shows some powerful date functions that exist in the implementations for SQL Server, Oracle, and MySQL.
SQL Server |
|
DATEPART |
Returns the integer value of a DATEPART for a date |
DATENAME |
Returns the text value of a DATEPART for a date |
GETDATE() |
Returns the system date |
DATEDIFF |
Returns the difference between two dates for specified date parts, such as days, minutes, and seconds |
Oracle |
|
NEXT_DAY |
Returns the next day of the week as specified (for example, FRIDAY) since a given date |
MONTHS_BETWEEN |
Returns the number of months between two given dates |
MySQL |
|
DAYNAME(date) |
Displays day of week |
DAYOFMONTH(date) |
Displays day of month |
DAYOFWEEK(date) |
Displays day of week |
DAYOFYEAR(date) |
Displays day of year |