DATETIME: A combination of date and time. The supported range is '2017-01-01 00:00:00 'to '2017-12-31 23:59:59 '. MySQL displays DATETIME values in 'yyyy-MM-DD HH: MM: ss' format, but allows values to be assigned to DATETIME columns using strings or numbers.
TIMESTAMP [(M)]: TIMESTAMP. The range is '2017-01-01 00:00:00 'to July 22, 1970.
The TIMESTAMP column is used to record the date and time during INSERT or UPDATE operations. If you do not assign a value, the first TIMESTAMP column in the table is automatically set to the date and time of the last operation. You can also assign a NULL value to set the TIMESTAMP column to the current date and time. ,
Example:
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:
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 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:
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:
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
Returns a string in 'yyyy-MM-DD HH: MM: ss' format with a fixed display width of 19 characters. To obtain the numeric value, add + 0 to the TIMESTAMP column.