在创建update_time的时候,将字段设置为timestamp类型,NOT NULL,默认值设置为current_timestamp on update current_timestamp
附上Sql语句:`update_time` timestamp NOT NULL DEFAULT current_timestamp on update current_timestamp
测试:
插入一条数据不用插入时间,数据库会自动获取当前时间
测试时创建数据库的sql:
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET FOREIGN_KEY_CHECKS=1;
这样在修改数据时,数据库就好自动维护更新的时间。