php 2008-7-11 16:52
MYSQL中TIMESTAMP类型的默认值
MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样。
[color=#080ff0]1、自动UPDATE 和INSERT 到当前的时间:[/color]
表:
/*DDL Information For - test.t1*/
---------------------------------
Table Create Table
------ -------------------------------------------------------------------------------------
t1 CREATE TABLE `t1` (
`p_c` int(11) NOT NULL,
[color=#ff1200]`p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP [/color]
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
数据:
1 2007-10-08 11:53:35
[color=#ff1200]2 2007-10-08 11:54:00[/color]
insert into t1(p_c) select 3;
update t1 set p_c = 2 where p_c = 2;
数据:
1 2007-10-08 11:53:35
[color=#ff1200]2 2007-10-08 12:00:37[/color]
[color=#ff1200]3 2007-10-08 12:00:37
[color=#080ff0]2、自动INSERT 到当前时间,不过不自动UPDATE。
[color=#000000]表:
/*DDL Information For - test.t1*/
---------------------------------
Table Create Table
------ ---------------------------------------------------------
t1 CREATE TABLE `t1` (
`p_c` int(11) NOT NULL,
[color=#ff1200] `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP [/color]
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
[/color][/color][color=#000000]数据:[/color]
[color=#000000]insert into t1(p_c) select 4;[/color]
[color=#000000]update t1 set p_c = 3 where p_c = 3;[/color]
[color=#000000]1 2007-10-08 11:53:35[/color]
[color=#000000]2 2007-10-08 12:00:37[/color]
[color=#ff1200]3 2007-10-08 12:00:37[/color]
[color=#ff1200]4 2007-10-08 12:05:19
[color=#080ff0]3、一个表中不能有两个字段默认值是当前时间,否则就会出错。不过其他的可以。
[color=#000000]表:[/color]
[color=#000000]/*DDL Information For - test.t1*/[/color]
[color=#000000]---------------------------------[/color]
[color=#000000]Table Create Table [/color]
[color=#000000]------ ---------------------------------------------------------------[/color]
[color=#000000]t1 CREATE TABLE `t1` ( [/color]
[color=#000000] `p_c` int(11) NOT NULL, [/color]
[color=#ff1200] `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, [/color]
[color=#ff1200] `p_timew2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' [/color]
[color=#000000] ) ENGINE=InnoDB DEFAULT CHARSET=gb2312 [/color]
[color=#000000]数据:
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
[/color]
[/color][/color][/color]