Mysql5.5中实现createTime和updateTime自动更新
2016-01-12 00:00
696 查看
MySQL 5.5中如果同一表中两个timestamp列的默认值都设置为CURRENT_TIMESTAMP,会有以下的错误提示: 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause 有下列的解决方法: 1.建表语句 create table test( id integer not null auto_increment primary key, name varchar(20) not null , created timestamp not null default '0000-00-00 00:00:00', updated timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP ); 2.插入数据方法(timestamp列为not null时,设置为null会自动更新为当前时间) mysql> insert into test(name,created) values("litao", null); mysql> select * from test; +----+-------+---------------------+---------------------+ | id | name | created | updated | +----+-------+---------------------+---------------------+ | 1 | litao | 2014-06-15 11:24:27 | 2014-06-15 11:24:27 | +----+-------+---------------------+---------------------+ 1 row in set 3.更新时 mysql> update test set name = 'lt' where id = 1; mysql> select * from test; +----+------+---------------------+---------------------+ | id | name | created | updated | +----+------+---------------------+---------------------+ | 1 | lt | 2014-06-15 11:24:27 | 2014-06-15 11:28:29 | +----+------+---------------------+---------------------+ 1 row in set
相关文章推荐
- mysql开启远程访问
- mysql ERROR 1045 (28000): Access denied for user解决方法
- mysql的查询、子查询及连接查询
- Mysql几种索引类型的区别及适用情况
- mysql 索引
- MySQL - 主从复制及读写分离原理
- MySQL数据库
- MySQL高级十三——通过索引优化SQL
- MySql从零开始(四)常见运算符
- mac 下删除mysql
- mysql中的find_in_set的使用
- mysql中的find_in_set的使用
- MySQL高级十二——索引
- 用bat批处理脚本登录mysql
- MySQL高级十一——慢查询
- 原来mysql只能显示1000条数据
- Mac 10.10 下修改 MySQL 5.6 的密码
- 警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的陷阱
- mysql 自增列 AUTO_INCREMENT
- 常用的Mysql数据库操作语句大全