MYSQL中TIMESTAMP类型的使用
2010-10-03 23:08
417 查看
MYSQL中TIMESTAMP类型的默认值
MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样。
1、自动UPDATE 和INSERT 到当前的时间:
表:
---------------------------------
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 = 5;
数据:
1 2007-10-08 11:53:35
5 2007-10-08 12:00:37
3 2007-10-08 12:00:37
2、自动INSERT 到当前时间,不过不自动UPDATE。
表:
---------------------------------
Table Create Table
------ ---------------------------------------------------------
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;
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
5 2007-10-08 12:00:37
4 2007-10-08 12:05:19
3、一个表中不能有两个字段默认值是当前时间,否则就会出错。不过其他的可以。
表:
---------------------------------
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
一般在数据库结构设计中,用datetime替换timestamp字段,用程序来实现时间的创建和修改,这样可以控制的更加准确,好处是,不至于你修改该表的任何数据,此时间会随之改变。
MYSQL中TIMESTAMP类型可以设定默认值,就像其他类型一样。
1、自动UPDATE 和INSERT 到当前的时间:
表:
---------------------------------
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 = 5;
数据:
1 2007-10-08 11:53:35
5 2007-10-08 12:00:37
3 2007-10-08 12:00:37
2、自动INSERT 到当前时间,不过不自动UPDATE。
表:
---------------------------------
Table Create Table
------ ---------------------------------------------------------
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;
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
5 2007-10-08 12:00:37
4 2007-10-08 12:05:19
3、一个表中不能有两个字段默认值是当前时间,否则就会出错。不过其他的可以。
表:
---------------------------------
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
一般在数据库结构设计中,用datetime替换timestamp字段,用程序来实现时间的创建和修改,这样可以控制的更加准确,好处是,不至于你修改该表的任何数据,此时间会随之改变。
相关文章推荐
- mysql中DATETIME、DATE 和 TIMESTAMP 类型数据使用
- 针对Mysql 使用EF Code First时 TimeStamp/RowVersion 类型 的解决办法
- MySQL5.0的三种数据类型:DATETIME, DATE和TIMESTAMP的使用
- MySql中的Timestamp类型字段的使用
- mysql的timestamp类型使用注意
- MySQL中TIMESTAMP类型的使用说明
- mysql字段使用timestamp类型和默认值CURRENT_TIMESTAMP表示默认为时间,类似mssql的getdate()功能;字段自增
- mysql的timestamp类型在django中使用
- mysql中DATETIME、DATE 和 TIMESTAMP 类型数据使用
- mysql中DATETIME、DATE 和 TIMESTAMP 类型数据使用
- TIMESTAMP类型字段在SQL Server和MySQL中的含义和使用
- [MySQL]索引类型总结和使用技巧以及注意事项
- MYSQL中TIMESTAMP类型的默认值
- MySQL之聚合函数和日期数据类型的使用
- MySQL:记录更新时,timestamp类型自动更新时间
- mysql中的TIMESTAMP类型时间范围
- MYSQL中TIMESTAMP类型的默认值
- Mysql的timestamp类型,自动记录数据的更新时间
- MySQL中DATETIME、DATE和TIMESTAMP类型的区别
- mysql 的 find_in_set函数使用方法,【根据用户选择的文章分类类型去匹配文章数据】