MySQL:- Date Types & Sizes

Background

Playing around more with MySQL and wanted to see why a certain implementation specification was chosen over another.

I really struggled to make sense of things.

Date Types & Sizes

There are a couple of types that one can use for date & time in MySQL.

Inclusive:-

  1. time
  2. date
  3. datetime
  4. timestamp

Code


/*
 * Declare & Get datetime values 
*/
set @time := CURTIME();
set @today := CURDATE();
set @now := NOW();
set @ts := CURRENT_TIMESTAMP();

/*
 * Display Values 
*/
select 
          "time" as "datatype"
        , @time as "value"
        , length(@time) as "length"

union all

select 
          "date" as "datatype"
        , @today as "value"
        , length(@today) as "length"

union all

select 
          "datetime" as "datatype"
        , @now as "value"
        , length(@now) as "length"

union all

select 
          "timestamp" as "datatype"
        , @ts as "value"
        , length(@ts) as "length"
        

Output

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s