Use of Timestamp type in MySQL

Source: Internet
Author: User

Default Value of Timestamp type in MySQL

You can set the default value for the timestamp type in MySQL, just like other types.
1. Automatic update and insert to current time:
Table:
---------------------------------
Table CREATE TABLE
------ Success -------------------------------------------------------------------------------------------
T1 create table 't1 '(
'P _ C' int (11) not null,
'P _ time' timestamp not null default current_timestamp on update current_timestamp
) Engine = InnoDB default charset = gb2312
Data:
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:
1 11:53:35
5 2007-10-08 12:00:37
3 12:00:37

2. Automatic insert to the current time, but not automatic update.
Table:
---------------------------------
Table CREATE TABLE
---------------------------------------------------------------
T1 create table 't2 '(
'P _ C' int (11) not null,
'P _ time' timestamp not null default current_timestamp
) Engine = InnoDB default charset = gb2312
Data:
Insert into T2 (P_c) Select 4;
Update T2 set P_c = 3 where P_c = 5;
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
5 2007-10-08 12:00:37
4 2007-10-08 12:05:19

3. A table cannot have two fields. The default value is the current time. Otherwise, an error occurs. But others can.
Table:
---------------------------------
Table CREATE TABLE
---------------------------------------------------------------------
T1 create table 't1 '(
'P _ C' int (11) not null,
'P _ time' timestamp not null default current_timestamp,
'P _ timew2 'timestamp not null default '2017-00-00 00:00:00'
) Engine = InnoDB default charset = gb2312
Data:
1 2007-10-08 11:53:35 0000-00-00 00:00:00
2 12:00:37 0000-00-00 00:00:00
3 2007-10-08 12:00:37 0000-00-00 00:00:00
4 12:05:19 0000-00-00 00:00:00

Generally, in the database structure design, the timestamp field is replaced with datetime, and the time creation and modification are implemented using a program. This can be more accurate and the benefit is that, if you do not modify any data in the table, the time will change accordingly.

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.