Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4157941
  • 博文数量: 240
  • 博客积分: 11504
  • 博客等级: 上将
  • 技术积分: 4277
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-28 14:24
文章分类

全部博文(240)

分类: Mysql/postgreSQL

2007-10-08 12:09:54

MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样。
1、自动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

2、自动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


3、一个表中不能有两个字段默认值是当前时间,否则就会出错。不过其他的可以。
表:
/*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

阅读(10149) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-05-24 09:16:58

谢谢了。

chinaunix网友2009-06-10 17:33:08

在MYSQL 5 中,update t1 set p_c = 2 where p_c = 2;更新,好像不算是更新,时间不会更新的,只有值更变了.才可以的.

linuxkumao2008-02-29 16:25:05

哈哈,这是我发贴问过的