Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100031 Viewers
56 Video lessons · 113097 Viewers
71 Video lessons · 81944 Viewers
131 Video lessons · 39317 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.