Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the default value of TIMESTAMP type in MYSQL

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report