MySQL - Default a date (timestamp) field to now

MySQL DATE syntax FAQ: How do I set a MySQL DATE field to default to "now", i.e., the current time?

Setting the date to "now"

Unfortunately you can‘t default a MySQL DATE field to "now", but you can get the "now" behavior with a MySQL TIMESTAMP field. The syntax to create a MySQL TIMESTAMP "now" field is:

last_changed timestamp not null default now(),

  

where last_changed is the name of my field, timestamp is the type of field, it can‘t be null, and the date/time default is now().

Now when you do a MySQL INSERT, just skip this field in your MySQL INSERT statement, and this field will default to the current date/time.

Example

To be clear about how this works, here‘s a complete example of how to default a MySQL timestamp field to "now":

mysql> create table test (foo int, ts timestamp default now());
Query OK, 0 rows affected (0.20 sec)

mysql> desc test;
+-------+-----------+------+-----+-------------------+-------+
| Field | Type      | Null | Key | Default           | Extra |
+-------+-----------+------+-----+-------------------+-------+
| foo   | int(11)   | YES  |     | NULL              |       |
| ts    | timestamp | NO   |     | CURRENT_TIMESTAMP |       |
+-------+-----------+------+-----+-------------------+-------+
2 rows in set (0.02 sec)

mysql> insert into test (foo) values(1);
Query OK, 1 row affected (0.03 sec)

mysql> select * from test;
+------+---------------------+
| foo  | ts                  |
+------+---------------------+
|    1 | 2010-12-15 14:20:59 |
+------+---------------------+
1 row in set (0.02 sec)

  

Summary

I hope this MySQL timestamp example has been helpful. As usual, if you have any questions or comments, just leave a note below.

时间: 2024-08-25 21:34:08

MySQL - Default a date (timestamp) field to now的相关文章

jackson/fastjson、mybatis、mysql date/datatime/timestamp、java Date/Timestamp关系详解

jackson/fastjson序列化/反序列化: 默认情况下,jackson/fastjson将java Date/Timestamp类型序列化为时间戳,也就是1970年1月1日0点以来的毫秒数.如果要显示为用户友好表示: Jackson 可以: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(sdf)或者:@JsonFormat(locale

mysql建表报timestampn Error : Invalid default value for 'timestamp'

环境 :mysql 5.7.16 系统:centos 6.8 64 百度查询到 ,因为是版本的问题导致. 解决方案: 1.打开/etc/my.cnf 配置文件 在最后添加如下: explicit_defaults_for_timestamp = true sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 保存,重启mysql,测试OK. mysql建

oracle、mysql时区设置对timestamp的不同影响

因最近国际去Oracle上MySQL,这就不可避免的涉及到时区和timestamp问题.做一下实验,总结一下. Oracle 首先看下oracle concepts对timestamp的定义: The TIMESTAMP data type is an extension of the DATE data type. It stores fractional seconds in addition to the information stored in the DATE data type.

【MySQL】探究之TIMESTAMP

背景 之前有业务反馈表中start_time,end_time时间字段随着时间的推移被自动更新,这可不是业务意愿,说的严重点是要出故障的. MySQL中有DATE,DATETIME,TIMESTAMP时间类型 看看官方文档怎么说 The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. Th

mysql5.7下的timestampn Error : Invalid default value for 'timestamp'

表格创建是爆了个错 Error : Invalid default value for 'timestamp' 参考:http://www.jb51.net/article/71107.htm 这版本导致的. mysql配置文件 mv /usr/local/mysql/suport-file/default-my.cnf /etc/my.cnf explicit_defaults_for_timestamp=1 http://www.shangxueba.com/jingyan/1609295.

MySQL 5.6 中 TIMESTAMP 的变化

在MySQL 5.6.6之前,TIMESTAMP的默认行为: TIMESTAMP列如果没有明确声明NULL属性,默认为NOT NULL.(而其他数据类型,如果没有显示声明为NOT NULL,则允许NULL值.)设置TIMESTAMP的列值为NULL,会自动存储为当前timestamp. 表中的第一个TIMESTAMP列,如果没有声明NULL属性.DEFAULT或者 ON UPDATE,会自动分配 DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMEST

关于datetime,date,timestamp,year,time时间类型小结

关于datetime,date,timestamp,year,time时间类型 datetime占用8个字节 日期范围:”1000-01-01 00:00:00” 到”9999-12-31 23:59:59” date占用3个字节 可以显示范围:”1000-01-01” 到”9999-12-31” timestamp占用4个字节 timestamp显示结果跟datetime一样,固定格式:”YYYY-MM-DD HH:MM:SS” 不同的是: 1,timestamp占用4个字节,显示的范围”19

mysql解决datetime与timestamp精确到毫秒的问题

CREATE TABLE `tab1` ( `tab1_id` VARCHAR(11) DEFAULT NULL, `create` TIMESTAMP(3) NULL DEFAULT NULL, `create2` DATETIME(3) DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8 SELECT * FROM tab1; TIMESTAMP(3)与 DATETIME(3)意思是保留3为毫秒数 TIMESTAMP(6)与 DATETIME(

String和Date,Timestamp之间的转换

一.String与Date(java.util.Date)互转 1.1 String -> Date Java代码 String dateStr = “2010/05/04 12:34:23″; Date date = new Date(); //注意format的格式要与日期String的格式相匹配 DateFormat sdf = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); try { date = sdf.parse(dateStr); Sys