This section describes the TIMESTAMP type in MYSQL.

Source: Internet
Author: User
Tags mysql commands

In MYSQL, the TIMESTAMP type is a required knowledge to learn MYSQL databases. The following describes the default values of the TIMESTAMP type in MYSQL. It is helpful for you to learn the TIMESTAMP type in MYSQL.

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 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.
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

TIMESTAMP Variant
1, timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
This data column is refreshed when a new record is created and an existing record is modified.

2, timestamp default CURRENT_TIMESTAMP
This field is set to the current time when a new record is created, but it will not be refreshed when it is modified later.

3, timestamp on update CURRENT_TIMESTAMP
Set this field to 0 when creating a new record, and refresh it later.

4, timestamp default 'yyyy-mm-dd hh: mm: ss' on update CURRENT_TIMESTAMP
Set this field as a given value when creating a new record, and refresh it later.

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.

How to modify the order of mysql Fields

How to add or delete a primary key in mysql

How to Set MySql access restrictions

Six Methods for changing the password of MySQL

Common mysql commands in linux

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.