Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Date and time types

From: MySQL Essential Training

Video: Date and time types

MySQL supports a complete set of date and time types. And it's just going to have a few columns in it.

Date and time types

MySQL supports a complete set of date and time types. These include date, time, a standalone year type, a combined datetime type, and a timestamp type that's automatically updated when a row is created or updated. It's worth noting that two digit year support is deprecated as of MySQL 5.6.6. It still works according to these rules. But the documentation warns that this functionality will be removed in a future release.

Keep in mind that two digit years are never a good idea, and if you're using them today, you'll be well served by updating your systems to use four digit years. My sql stores dates and times in standard sql format. Standard sql format for dates and times is designed for convenient sorting and searching. Different countries and locals have different localized formats for dates and times. Some with the year to the left some with the year to the right. Some in odd mixed up orders like the month day year format common in the US.

Standard SQL format has the most significant parts to the left and the least significant parts to the right. This makes sorting and searching much easier so you may even find it useful in other contexts in spreadsheets and file names. MySQL has comprehensive time zone support that's based on the intensive time zone database maintained by the internet assigned numbers authority. The IANA time zone database is a standard set of international time zone rules names and locations.

If you're using a Mac or Unix based My SQL installation you have this database installed, if using windows it's probably not installed by default. Please see the movie installing the time zone database on windows in chapter one. So lets take a look at a simple use for a time stamp and we'll use the scratch. Database here and I'll start by creating a table, I'll always start with a drop table. If exists because we'll be doing this over and over and I'll create the table.

And it's just going to have a few columns in it. And we'll insert a few rows into it. And then we'll put in a describe so we can see what it looks like, and a show create table so we can see what the defaults are, and a select from. Go ahead and run it. I am missing parenthesis there, aren't I? There we go. So there's our create table. we have this column stamp which is of the time stamp type. And, the time stamp type, of course, is designed to automatically create a time stamp on every insert and update and so, you'll notice here in the.

Describe. This is our Describe here. Describe test. Then it says default is CURRENT_TIMESTAMP and on update is CURRENT_TIMESTAMP. Current timestamp is a constant that expands to. The current date and time, and so that's the way that that's actually implemented and if we look here in our show create table we see not in all default current time stamp on update current time stamp. And there is our rows and of course they all have the same time stamp because this entire query took a 103 milliseconds.

Now, if I come back in here, and I'm just going to replace all of this for now with an update. We'll leave the select there at the bottom. And I'm just going to say UPDATE test SET note =, and will give it a. Another artist, where ID=2; and so that'll just replace the second here instead of on Henri Matisse it will say Jackson Pollock and it will give us a new time stamp there.

And so when I press go, you notice Jackson Pollock here now has a different time. This says 35:59 rather than 33:39 and I didn't update that column. That happened automatically. Because of the time stamp type. So the time stamp type is a very handy, it's very common, it's a quick and easy way to apply a time stamp of a time and date to a row of data. So I'm going to go ahead and drop this table now. And I want to explain a little bit about time zone support.

MySQL uses a set of variables to control some system behaviors including time zone support. So if we type in here. Show variables, and that's again, a mysql specific statement that will show the mysql variables table, and I can even use like with it. I can say like, and percent in single quotes, percent time zone like this. And if I say select now, like that with parenthesis, and press go, we see that our timezone is set to UTC, system timezone is MST on this system, and of course it'll be different on every system depending on how your system timezone is set.

So yours probably will not say MST, it's. Possible if you're in the Mountain Standard Time Zone and it doesn't have one of its aliases or equivalents. But it will say something. And then time zone itself is set to UTC, and that's actually done in the SID code, in its initialization code. And so this now variable is showing us the current time stamp, which is in UTC. So it's o three o'clock in the morning. On March 12, 2014 in the UTC time zone.

Now, if you're time zone variable does not say UTC, that probably means you don't have time zone support installed on your system. There's a movie in chapter one that'll show you how to install it. Now I can change my local time zone by setting the time zone variable and this is the sort of thing you would do in an application if you wanted to set it to U to C. Or to some local time zone. You can say set time zone with the underscore like that equals and give it a time zone. So if I wanted to make this US eastern and than I'll just go ahead and copy and paste these show variables and select now like that.

And when I say Go now we have the first two which is in UTC and then after that I set the time zone to eastern and now, now shows the time zone in the eastern time zone which of course is several hours earlier, looks like four hours earlier, than UTC. So it's the day before and it's 23:39 instead of 03:39. So, the time and date types in MySQL have good support for storing and displaying time and date information including comprehensive support for time zones based on the extensive IANA time zone database.

Show transcript

This video is part of

Image for MySQL Essential Training
MySQL Essential Training

60 video lessons · 10261 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed MySQL Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.