mysql中timestamp的自动生成与更新
2016-12-27 23:39
651 查看
转自:mysql中timestamp的自动生成与更新
MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样。
1、自动UPDATE 和INSERT 到当前的时间:
表:
---------------------------------
Table Create Table
------ --------------------------
数据:
1 2007-10-08 11:53:35
2 2007-10-08 11:54:00
数据:
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
3 2007-10-08 12:00:37
2、自动INSERT 到当前时间,不过不自动UPDATE。
表:
---------------------------------
Table Create Table
------ ---------------------------
数据:
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
3 2007-10-08 12:00:37
4 2007-10-08 12:05:19
3、一个表中不能有两个字段默认值是当前时间,否则就会出错。不过其他的可以。
表:
---------------------------------
Table Create Table
------ --------------------------
数据:
1 2007-10-08 11:53:35 0000-00-00 00:00:00
2 2007-10-08 12:00:37 0000-00-00 00:00:00
3 2007-10-08 12:00:37 0000-00-00 00:00:00
4 2007-10-08 12:05:19 0000-00-00 00:00:00
MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样。
1、自动UPDATE 和INSERT 到当前的时间:
表:
---------------------------------
Table Create 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
数据:
1 2007-10-08 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 = 2;
数据:
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
3 2007-10-08 12:00:37
2、自动INSERT 到当前时间,不过不自动UPDATE。
表:
---------------------------------
Table Create Table
------ ---------------------------
CREATE TABLE `t1` ( `p_c` int(11) NOT NULL, `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=gb2312
数据:
insert into t1(p_c) select 4; update t1 set p_c = 3 where p_c = 3;
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
3 2007-10-08 12:00:37
4 2007-10-08 12:05:19
3、一个表中不能有两个字段默认值是当前时间,否则就会出错。不过其他的可以。
表:
---------------------------------
Table Create Table
------ --------------------------
CREATE TABLE `t1` ( `p_c` int(11) NOT NULL, `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `p_timew2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ) ENGINE=InnoDB DEFAULT CHARSET=gb2312
数据:
1 2007-10-08 11:53:35 0000-00-00 00:00:00
2 2007-10-08 12:00:37 0000-00-00 00:00:00
3 2007-10-08 12:00:37 0000-00-00 00:00:00
4 2007-10-08 12:05:19 0000-00-00 00:00:00
相关文章推荐
- MySQL中的datetime与timestamp比较
- mac mysql5.7重置root密码
- mysql修改表名,列名,列类型,添加表列,删除表列
- MYSQL常用命令总结
- mysql 无法启动
- MySQL入门教程
- mysqldump 导出中文乱码
- MySQL锁的用法之行级锁
- mysql索引
- Mysql-分页查询优化
- mysql数据库优化策略
- Mysql-binlog日志
- mysql主主复制 两个数据库版本不一样 怎么解决
- 关于mysql数据库的备份和还原
- elk 5.1 用于mysql slow 日志
- MYSQL学习笔记-常见表约束
- Mysql问题汇总
- Python查询Mysql, sqlite时返回字典结构的代码
- MySQL索引学习笔记
- MySQL主主复制以及常见的一些问题