Current_timestamp data types in MySQL

Source: Internet
Author: User
Tags datetime

Cases

1. MySQL Gets the current timestamp function: Current_timestamp, Current_timestamp ()

The code is as follows Copy Code

Mysql> Select Current_timestamp, Current_timestamp ();

+---------------------+---------------------+
| Current_timestamp | Current_timestamp () |
+---------------------+---------------------+
| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
+---------------------+---------------------+


cases

The timestamp type in MySQL can set default values, just like any other type.

1, Automatic Update and insert to the current time:
Table:
———————————
Table Create Table
——————————————————————————————-

The code is as follows Copy Code
T1 CREATE TABLE ' T1 ' (
' P_c ' int (one) is not NULL,
' P_time ' timestamp not NULL DEFAULT current_timestamp on UPDATE current_timestamp
) Engine=innodb DEFAULT charset=gb2312


Data:

The code is as follows Copy Code
1 2007-10-08 11:53:35
2 2007-10-08 11:54:00
INSERT into T1 (p_c) Select 3;
Update T1 Set P_c = 2 where p_c = 5;

Data:

The code is as follows Copy Code
1 2007-10-08 11:53:35
5 2007-10-08 12:00:37
3 2007-10-08 12:00:37

2, automatic insert to the current time, but not automatic update.

Summarize

1. Record time stamp on first insert
2. Update time stamp for each update
3. Record only brief time, such as date
In the first and second cases, it is more convenient to use timestamp than DateTime. There are three timestamp default values:

No
NULL
Not NULL DEFAULT Current_timestamp
Not NULL DEFAULT current_timestamp on UPDATE current_timestamp
which

Current_timestamp-When this record is updated, the timestamp does not change
Current_timestamp on Update Current_timestamp-when this record is updated, the timestamp will change to the latest time
When you set the Current_timestamp defaults, you do not need to maintain the field to insert records or update records, which are maintained entirely by MySQL, just like the AutoIncrement field, but you will need to manually construct the time update if you use the DateTime field set above , like now () and so on.

For current_timestamp on update current_timestamp, there is a special case where MySQL ignores the update operation if you update the field value (update), so the timestamp does not change. In addition, if there are multiple current_timestamp on update current_timestamp columns, then MySQL will only update the first one.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.