码迷,mamicode.com
首页 > 数据库 > 详细

MYSQL中TIMESTAMP类型的默认值

时间:2015-11-09 12:26:08      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样。
表:
——————————————————————————————————————
t1      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
update t1 set p_c = 2 where p_c = 5;
1    2007-10-08 11:53:35
5    2007-10-08 12:00:37
3    2007-10-08 12:00:37
表:
——————————————————————————————
t1      CREATE TABLE `t2` (                                      
          `p_c` int(11) NOT NULL,                                
          `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 
        ) ENGINE=InnoDB DEFAULT CHARSET=gb2312                  
insert into t2(p_c) select 4;
update t2 set p_c = 3 where p_c = 5;
2    2007-10-08 12:00:37
5    2007-10-08 12:00:37
4    2007-10-08 12:05:19
表:
——————————————————————————————
t1      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

 

TIMESTAMP的变体
1,TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
在创建新记录和修改现有记录的时候都对这个数据列刷新

2,TIMESTAMP DEFAULT CURRENT_TIMESTAMP
在创建新记录的时候把这个字段设置为当前时间,但以后修改时,不再刷新它

3,TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
在创建新记录的时候把这个字段设置为0,以后修改时刷新它

4,TIMESTAMP DEFAULT ‘yyyy-mm-dd hh:mm:ss’ ON UPDATE CURRENT_TIMESTAMP 
在创建新记录的时候把这个字段设置为给定值,以后修改时刷新它

MYSQL中TIMESTAMP类型的默认值

标签:

原文地址:http://www.cnblogs.com/tanhao/p/4949307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!