When you need to find the right SQL keyword or MySQL client command-line option right away, turn to this convenient reference, known for the same speed and flexibility as the system it covers so thoroughly. MySQL is packed with so many capabilities that the odds of remembering a particular function or statement at the right moment are pretty slim. With MySQL in a Nutshell, you get the details you need, day in and day out, in one concise and extremely well organized book.
The new edition contains all the commands and programming information for version 5.1, including new features and language interfaces. It's ideal for anyone using MySQL, from novices who need to get up to speed to advanced users who want a handy reference. Like all O'Reilly Nutshell references, it's easy to use and highly authoritative, written by the editor of the MySQL Knowledge Base at MySQL AB, the creator and owner of MySQL.
Inside, you'll find:
"synopsis" may belong to another edition of this title.
Russell Dyer is a freelance writer and currently the CurriculumManager at MariaDB Ab. He worked previously for six years at MySQL Ab. as their Knowledge Base Editor. He is the author of MySQL in a Nutshell (http://www.oreilly.com/catalog/mysqlian/) and has writen articles for several magazines: Dev Zone (a MySQL publication), Linux Journal, ONlamp.com, The Perl Journal, Red Hat Magazine, SysAdmin Magazine, Tech Republic, Unix Review, and XML.com. He has also finished his first novel, "In Search of Kafka". More information on Russell, along with a list of his published articles with links to them, can be found on his web site at http://russell.dyerhouse.com
The ability to record dates and times in a MySQL database is a very common requirement. This chapter presents the date and time functions for MySQL.
Date and time data comprises only numeric strings, so it can be stored in a regular character column. However, by using temporal datatype columns, you can use several built-in functions offered by MySQL. Currently, five temporal datatypes are available: date, time, datetime, timestamp, and year. The date column type is only for recording the date and uses the format yyyy-mm-dd. The time column type is for recording time in the format hhh:mm:ss. To record a combination of date and time, you can use the datetime column type: yyyy-mm-dd hh:mm:ss. The timestamp column is similar to datetime, but is a little limited in its range of allowable time: it starts at the Unix epoch time (i.e., 1970-01-01) and ends at the end of 2037. Finally, the year datatype is used only for recording the year in a column.
Incidentally, any function that calls for a date or a time datatype will also accept a combined datetime datatype. For more information on date and time datatypes, see Appendix A.
Validation of date strings is limited: MySQL makes sure that months range only from 0 to 12, and days range from 0 to 31. Therefore, a date such as February 30would be accepted. Version 5.0.2 of MySQL will offer more refined validation that would reject such a date.
At the end of this introduction is a listing of date and time functions, grouped by type of function. The bulk of this chapter consists of an alphabetical listing of date and time functions, with explanations of each. Many functions come with examples, along with a resulting display. For help in locating functions, see the index at the back of this book.
For the examples in this chapter, I used the scenario of a professional services firm (e.g., a law firm or an investment advisory firm) that tracks appointments and seminars in MySQL.
Date and Time Functions Grouped by Type
This section lists the functions according to their purpose: to retrieve a time, extract an element of one, or perform calculations on it.
Determining the Date and Time
CURDATE( ), CURRENT_DATE, CURTIME( ), CURRENT_TIME, CURRENT_TIMESTAMP, NOW( ),LOCALTIME( ), LOCALTIMESTAMP( ), SYSDATE( ), UNIX_TIMESTAMP( ), UTC_DATE( ), UTC_TIME( ), UTC_TIMESTAMP( )
Extracting and Formatting the Date and Time
DATE( ), DATE_FORMAT( ), DAY( ), DAYNAME( ), DAYOFMONTH( ), DAYOFWEEK( ), DAYOFYEAR( ), EXTRACT( ), GET_FORMAT( ), HOUR( ), LAST_DAY( ), MAKEDATE( ), MAKETIME( ), MINUTE( ), MONTH( ), MONTHNAME( ), QUARTER( ), SECOND( ), STR_TO_DATE( ), TIME_FORMAT( ), TIMESTAMP( ), WEEK( ), WEEKDAY( ), WEEKOFYEAR( ), YEAR( ), YEARWEEK( )
Calculating and Modifying the Date and Time
ADDDATE( ), ADDTIME( ), CONVERT_TZ( ), DATE_ADD( ), DATE_SUB( ), DATEDIFF( ), FROM_DAYS( ), FROM_UNIXTIME( ), PERIOD_ADD( ), PERIOD_DIFF( ), SEC_TO_TIME( ), SUBDATE( ), SUBTIME( ), TIME_TO_SEC( ), TO_DAYS( ), TIMEDIFF( ), TIMESTAMPADD( ), TIMESTAMPDIFF( )
Date and Time Functions in Alphabetical Order
The rest of the chapter lists each function in alphabetical order.
ADDDATE( )
ADDDATE(date,INTERVAL value type)
This function adds the given interval of time to the date or time provided. This is an alias for DATE_ADD( ); see its definition for details and interval types.
UPDATE seminars
SET seminar_date = ADDDATE(seminar_date, INTERVAL 7 DAY)
WHERE seminar_date = '2004-12-15'';
This example postpones the seminar that was scheduled for December 15, 2004 to December 22—seven days later. As of Version 4.1 of MySQL, for adding days the second argument of the function may simply be the number of days (i.e., just 7 instead of INTERVAL 7 DAY).
"About this title" may belong to another edition of this title.
Book Description Soft Cover. Condition: new. Seller Inventory # 9780596514334
Book Description Paperback. Condition: new. New. Fast Shipping and good customer service. Seller Inventory # Holz_New_0596514336
Book Description Paperback. Condition: New. In shrink wrap. Seller Inventory # 10-03003
Book Description Condition: New. Brand New! Not Overstocks or Low Quality Book Club Editions! Direct From the Publisher! We're not a giant, faceless warehouse organization! We're a small town bookstore that loves books and loves it's customers! Buy from Lakeside Books!. Seller Inventory # OTF-S-9780596514334
Book Description Paperback or Softback. Condition: New. MYSQL in a Nutshell 1.69. Book. Seller Inventory # BBS-9780596514334
Book Description Paperback. Condition: new. New. Seller Inventory # Wizard0596514336
Book Description Condition: New. Seller Inventory # ABLIING23Feb2416190070979
Book Description Condition: New. Seller Inventory # I-9780596514334
Book Description Condition: New. Seller Inventory # 5315267-n
Book Description Paperback. Condition: new. New Copy. Customer Service Guaranteed. Seller Inventory # think0596514336