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.