MYSQL中TIMESTAMP类型的默认值

MYSQL中TIMESTAMP类型能够设定默认值,就像其余类型同样。
一、自动UPDATE 和INSERT 到当前的时间:
表:
/*DDL Information For - test.t1*/
---------------------------------

Table   Create Table                                                                        
------  -------------------------------------------------------------------------------------
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

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

二、自动INSERT 到当前时间,不过不自动UPDATE。

表:
/*DDL Information For - test.t1*/
---------------------------------

Table   Create Table                                            
------  ---------------------------------------------------------
t1      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


三、一个表中不能有两个字段默认值是当前时间,不然就会出错。不过其余的能够。
表:
/*DDL Information For - test.t1*/
---------------------------------

Table   Create Table                                                  
------  ---------------------------------------------------------------
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

本文出自 “上帝,我们不见不散!” 博客,转载请与做者联系!spa

相关文章
相关标签/搜索