Get the App
SLTechnology News&Howtos  ›  Database  › 

How to set the default value of TIMESTAMP type in MYSQL

Shulou Source: shulou.com Published: 2022-05-31 19:23:05 10月12日 Update

This article introduces how to set the default value of TIMESTAMP type in MYSQL, the content is very detailed, interested friends can refer to it, I hope it can be helpful to you.

You can set default values for TIMESTAMP types in MYSQL, just like other types.

1. Automatic UPDATE and INSERT to the current time:

Table:

-

Table Create Table

T1 CREATE TABLE `t1` (

`pc` int (11) NOT NULL

`ptime` 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 (pamphc) select 3

Update T1 set Prunc = 2 where Prunc = 5

Data:

1 2007-10-08 11:53:35

5 2007-10-08 12:00:37

3 2007-10-08 12:00:37

2. Automatically INSERT to the current time, but not automatically UPDATE.

Table:

-

Table Create Table

-

T1 CREATE TABLE `t2` (

`pc` int (11) NOT NULL

`ptime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

) ENGINE=InnoDB DEFAULT CHARSET=gb2312

Data:

Insert into T2 (Prunc) select 4

Update T2 set Prunc = 3 where Prunc = 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. You cannot have two fields in a table whose default value is the current time, otherwise an error will occur. But others can.

Table:

-

Table Create Table

T1 CREATE TABLE `t1` (

`pc` int (11) NOT NULL

`ptime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

`ptimew2` timestamp NOT NULL DEFAULT '0000-00-0000: 0000'

) ENGINE=InnoDB DEFAULT CHARSET=gb2312

Data:

1 2007-10-08 11:53:35 0000-00-0000: 00:00

2 2007-10-08 12:00:37 0000-00-0000: 00:00

3 2007-10-08 12:00:37 0000-00-0000: 00:00

4 2007-10-08 12:05:19 0000-00-0000: 00:00

Variants of TIMESTAMP

1,TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Refresh this data column when you create a new record and modify an existing record

2,TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Set this field to the current time when you create a new record, but do not refresh it when you modify it later

3,TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Set this field to 0 when you create a new record, and refresh it later when you modify it

4The timestamp DEFAULT 'yyyy-mm-dd hh:mm:ss'ON UPDATE CURRENT_TIMESTAMP

Set this field to the given value when you create a new record, and refresh it later when you modify it

On how to set the default value of TIMESTAMP type in MYSQL is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Data type field new record time time content more help yes two interests variants guys buddies articles knowledge articles references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker NVidia MariaDB OPPO Reno