MySQL automatically set create_time and update_time

Transfer from: http: //www.mamicode.com/info-detail-1807313.html


DATETIME type is used when you need to include the value of the date and time information. MySQL retrieved and to 'YYYY-MM-DD HH:format DATETIME value, support range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . ( "Support" means that although earlier values might work, but they can not guarantee.)

DATE type is used when you need only a date value, not part time. MySQL retrieves and displays DATE values 'YYYY-MM-DD' format, support range is '1000-01-01' to '9999-12-31'.

TIMESTAMP column type provides a type that you can use it to automatically labeled INSERT or UPDATE operation with the current date and time.

TIME data type represents the time of day. MySQL retrieves and displays TIME values to "HH: SS: MM" format. The supported range is '00: 00: 00 'to '23: 59: 59'.


Reference table structure

  CREATE TABLE `t_baby` (
`baby_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`nickname` varchar(64) DEFAULT NULL,
`avatar` varchar(64) DEFAULT ‘‘,
`sex` int(2) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘0:未知;\\n 1:男;\\n 2:女。‘,
`birthday` datetime NOT NULL,
`creater` varchar(16) DEFAULT ‘‘,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`is_delete` bigint(20) DEFAULT NULL COMMENT ‘0‘,
`updater` varchar(16) DEFAULT NULL,
PRIMARY KEY (`baby_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10000000 DEFAULT CHARSET=utf8mb4;

When we create a business usually you need to set the table and create_time update_time but often need to set a good time in the code and then inserted into the database

mysql automatically set assigned to the current system time attribute field when the insert data field is set CURRENT_TIMESTAMP

Mysql and automatically sets the current system time is changed after a successful ON UPDATE CURRENT_TIMESTAMP field is set when update data is assigned to the attribute field

 

 

create_time set CURRENT_TIMESTAMP property

update_time attribute set ON UPDATE CURRENT_TIMESTAMP

 

We do not need to manually code these two parameters to save a certain amount of code assignment

 

MySQL automatically set create_time and update_time



Guess you like

Origin www.cnblogs.com/jpfss/p/12132139.html