MySQL database to automatically add time and time settings automatically update the time

Description:

MySQL field, set the time fields are automatically added to create time and automatically update the time settings,
set the field type is: timestamp default is set to current_timestamp (),
update time field field types: timestamp default is set to current_timestamp () ON UPDATE current_timestamp ()

SQL syntax and examples

create table tb_name(
  join_time timestamp NULL DEFAULT current_timestamp(),
  update_time timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp())
CREATE TABLE mytb (
  join timestamp NULL DEFAULT current_timestamp(),
  update timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  name varchar(12) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

Data insertion verification
mysql> insert into mytb(name) value('xiaoming');

Update data verification
mysql> update mytb set name="小花";
for verification
MySQL database to automatically add time and time settings automatically update the time

Guess you like

Origin blog.51cto.com/maoxiaoxiong/2406477