SunQuest
 
       MySQL
  Home arrow MySQL arrow Page 10 - Data Definition Language, Part 1
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Data Definition Language, Part 1
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    2005-01-19

    Table of Contents:
  • Data Definition Language, Part 1
  • 4.1 General Database and Table Properties
  • 4.3 Limits on Number and Size of Database Components
  • 4.4 Identifier Syntax
  • 4.5 CREATE DATABASE and DROP DATABASE
  • 4.7 DROP TABLE
  • 4.9 Creating and Dropping Indexes
  • 4.10 Column Types
  • 4.10.2 String Column Types
  • 4.10.3 Date and Time Column Types
  • 4.10.4 Column Options
  • 4.10.5 Using the AUTO_INCREMENT Column Option
  • 4.10.6 Automatic Type Conversion and Value Clipping

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Data Definition Language, Part 1 - 4.10.3 Date and Time Column Types


    (Page 10 of 13 )

    MySQL provides column types for storing different kinds of temporal information. In the following descriptions, the terms YYYY, MM, DD, hh, mm, and ss stand for a year, month, day of month, hour, minute, and second value, respectively.

    The storage requirements and ranges for the date and time datatypes are summarized in the following table:

    Type

    Storage Required

    Range

    DATE

    3 bytes

    '1000-01-01' to '9999-12-31'

    TIME

    3 bytes

    '-838:59:59' to '838:59:59'

    DATETIME

    8 bytes

    '1000-01-01 00:00:00' to '9999-12-31 23:59:59'

    TIMESTAMP

    4 bytes

    '1970-01-01 00:00:00' to mid-year 2037

    YEAR

    1 byte

    1901 to 2155 (YEAR(4)), 1970 to 2069 (YEAR(2))


    For TIMESTAMP, MySQL 4.0 displays values such as '1970-01-01 00:00:00 'as 19700101000000. In MySQL 4.1, this changes so that TIMESTAMP display format is the same as DATETIME.

    Each of these temporal datatypes also has a "zero" value that's used when you attempt to store an illegal value. The "zero" value is represented in a format appropriate for the type (such as '0000-00-00 'for DATE and '00:00:00 'for TIME).

    4.10.3.1 The DATE and TIME Column Types

    The DATE datatype represents date values in 'YYYY-MM-DD 'format. This representation corresponds to the ANSI SQL date format, also known as ISO 8601 format.

    The supported range of DATE values is '1000-01-01 'to '9999-12-31 '. You might be able to use earlier dates than that, but it's better to stay within the supported range to avoid unexpected behavior.

    MySQL always represents DATE values in ISO 8601 format when it displays them. If necessary, you can reformat DATE values into other display formats using the DATE_FORMAT() function.

    For date entry, MySQL also expects to receive dates in ISO format, or at least close to ISO format. That is, date values must be given in year-month-day order, but some deviation from strict ISO format is allowed:

    • Leading zeros on month and day values may be omitted. For example, '2000-1-1 'and '2000-01-01 'are both accepted as legal.

    • The delimiter between date parts need not be -; you can use other punctuation characters, such as /.

    • Two-digit years are converted to four-digit years. You should be aware that this conversion is done based on the rule that year values from 70 to 99 represent the years 1970 to 1999, whereas values from 00 to 69 represent the years 2000 to 2069. It's better to provide values with four-digit years to avoid problems with conversion of values for which the rule does not apply.

    Instead of attempting to load values that aren't in an acceptable format into a DATE column, you should convert them into ISO format. An alternative approach that's useful in some circumstances is to load the values into a string column and perform reformatting operations using SQL string functions to produce ISO format values that can be assigned to a DATE column.

    The TIME datatype represents time values in 'hh:mm:ss 'format. TIME values may represent elapsed time, and thus might be outside the range of time-of-day values. They may even be negative values. (The actual range of TIME values is '-838:59:59 'to '838:59:59 '.)

    MySQL represents TIME values in 'hh:mm:ss 'format when displaying them. If necessary, you can reformat TIME values into other display formats using the TIME_FORMAT() function.

    For TIME value entry, some variation on this format is allowed. For example, leading zeros on TIME parts may be omitted.

    4.10.3.2 The TIMESTAMP and DATETIME Column Types

    The DATETIME column type stores date-and-time values in 'YYYY-MM-DD hh:mm:ss 'format. It's similar to a combination of DATE and TIME values, but the TIME part represents time of day rather than elapsed time and has a range limited to '00:00:00 'to '23:59:59 '. The date part of DATETIME columns has the same range as DATE columns; combined with the TIME part, this results in a DATETIME range from '1000-01-01 00:00:00 'to '9999-12-31 23:59:59 '.

    The TIMESTAMP type, like DATETIME, stores date-and-time values, but has a different range and some special properties that make it especially suitable for tracking data modification times. TIMESTAMP also has a different display format from DATETIME prior to MySQL 4.1:

    • Until MySQL 4.1, TIMESTAMP values are represented as numbers in YYYYMMDDhhmmss format. The default display width is 14 digits, but you can specify an explicit width of any even number from 2 to 14. The display width affects only how MySQL displays TIMESTAMP values, not how it stores them. Stored values always include the full 14 digits.

    • From MySQL 4.1 on, the TIMESTAMP format is 'YYYY-MM-DD hh:mm:ss ', just like DATETIME. Display widths are not supported.

    The range of TIMESTAMP values begins at 1970-01-01 00:00:00 (GMT) and extends partway into the year 2037. TIMESTAMP values actually represent the number of seconds elapsed since the beginning of 1970 and are stored using four bytes. This provides room for sufficient seconds to represent a date in the year 2037. Note that TIMESTAMP values are stored using the server's local timezone.

    TIMESTAMP columns have the following special properties:

    • Storing NULL into a TIMESTAMP column sets it to the current date and time. Updating a TIMESTAMP column to NULL also sets it to the current date and time.

    • If you omit a TIMESTAMP column from an INSERT statement, MySQL inserts the current date and time if the column is the first TIMESTAMP column in the table, and inserts zero if it is not.

    • MySQL automatically updates the first TIMESTAMP column in a table to the current date and time when you update (change the existing data in) any other column in the table. (Setting a column to its current value doesn't count as updating it.) Only the first TIMESTAMP column is subject to automatic updating. All other TIMESTAMP columns do not change unless you update them explicitly.

    It's important to know about the automatic-update property. It's what makes TIMESTAMP columns useful for tracking record modification times, but is a source of confusion if you're not aware of it. People who choose TIMESTAMP for a column on the basis of the fact that it stores date-and-time values become dismayed and mystified when they discover that the column's values change unexpectedly.

    4.10.3.3 The YEAR Column Type

    The YEAR column type represents year-only values. You can declare such columns as YEAR(4) or YEAR(2) to obtain a four-digit or two-digit display format. If you don't specify any display width, the default is four digits.

    If you don't need a full date and the range of values you need to store falls into the YEAR range, consider using YEAR to store temporal values. It's a very space-efficient datatype because values require only one byte of storage each.

    This chapter excerpt is from MySQL Certification Guide by Paul Dubois et al. (Sams, 2004, ISBN: 0672326329 ). Check it out at your favorite bookstore today. Buy this book now.

    More MySQL Articles
    More By Sams Publishing


       · I am a student from China,my English is vervy poor.I over it at last,but I do not...
       · OKAY!!!
     

       

    MYSQL ARTICLES

    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...
    - Creating the Blog Script for a PHP/MySQL Blo...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway