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

相關文章
相關標籤/搜索