MySQL中同时存在创建和上次更新时间戳字段解决方法浅析;MYSQL日期 字符串 时间戳互转
2017-03-12 15:47
821 查看
因为欣赏所以转载,原文地址 http://www.2cto.com/database/201409/330251.html
http://www.cnblogs.com/jhy-ocean/p/5560857.html
在写这篇文章之前,明确我的MySQL版本。
?
?
第一种,created_at使用DEFAULT CURRENT_TIMESTAMP或者DEFAULT now(),updated_at使用触发器。
?
mysql> INSERT INTO temp(name,created_at,updated_at) VALUES('robin',now(),now()); Query OK, 1 row affected (0.03 sec) mysql> INSERT INTO temp(name,created_at,updated_at)
VALUES('wentasy',now(),now()); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM temp; +----+---------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+---------+---------------------+---------------------+
| 1 | robin | 2014-09-01 14:00:39 | 2014-09-01 14:00:39 | | 2 | wentasy | 2014-09-01 14:01:11 | 2014-09-01 14:01:11 | +----+---------+---------------------+---------------------+ 2 rows in set (0.00 sec)
具体解决方法如下:
?
delimiter | DROP TRIGGER IF EXISTS tri_temp_created_at; CREATE TRIGGER tri_temp_created_at BEFORE INSERT ON temp FOR EACH ROW BEGIN IF new.created_at IS NULL
THEN SET new.created_at=now(); END IF; END; | delimiter ;
我们可以看下MySQL 5.5和5.6帮助文档对于这个问题的解释。
From the MySQL 5.5 documentation:
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value
for another column.
Changes in MySQL 5.6.5:
Previously, at most one TIMESTAMP column per table could be automatically initialized or updated to the current date and time. This restriction has been lifted. Any TIMESTAMP column definition can have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used with DATETIME column definitions. For more information, see Automatic Initialization and Updating for TIMESTAMP and DATETIME.
我们确定下MySQL的版本。
总结
平时比较常用的时间、字符串、时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法;本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串、date转时间戳、字符串转date、字符串转时间戳、时间戳转date,时间戳转字符串)用法,方便日后查看;
date_format(date, format) 函数,MySQL日期格式化函数date_format()
unix_timestamp() 函数
str_to_date(str, format) 函数
from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime
select date_format(now(), '%Y-%m-%d');
#结果:2016-01-05
select unix_timestamp(now());
#结果:1452001082
select str_to_date('2016-01-02', '%Y-%m-%d %H');
#结果:2016-01-02 00:00:00
select unix_timestamp('2016-01-02');
#结果:1451664000
select from_unixtime(1451997924);
#结果:2016-01-05 20:45:24
select from_unixtime(1451997924,'%Y-%d');
//结果:2016-01-05 20:45:24
MySQL日期格式化(format)取值范围。
http://www.cnblogs.com/jhy-ocean/p/5560857.html
在写这篇文章之前,明确我的MySQL版本。
mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.5.29-log | +------------+ 1 row in set (0.00 sec)
第一个,测试通过。
?
?
VALUES('wentasy',now(),now()); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM temp; +----+---------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+---------+---------------------+---------------------+
| 1 | robin | 2014-09-01 14:00:39 | 2014-09-01 14:00:39 | | 2 | wentasy | 2014-09-01 14:01:11 | 2014-09-01 14:01:11 | +----+---------+---------------------+---------------------+ 2 rows in set (0.00 sec)
delimiter | DROP TRIGGER IF EXISTS tri_temp_updated_at; CREATE TRIGGER tri_temp_updated_at BEFORE UPDATE ON temp FOR EACH ROW BEGIN SET NEW.updated_at = now(); END; | delimiter ;
mysql> UPDATE temp SET name='robinwen' WHERE id=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 #可以看到已经记录了第一条数据的更新时间 mysql> SELECT * FROM temp; +----+----------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+----------+---------------------+---------------------+ | 1 | robinwen | 2014-09-01 14:00:39 | 2014-09-01 14:03:05 | | 2 | wentasy | 2014-09-01 14:01:11 | 2014-09-01 14:01:11 | +----+----------+---------------------+---------------------+ 2 rows in set (0.00 sec)
第二种,created_at使用触发器,updated_at使用DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP或者DEFAULT now() ON UPDATE now();
具体解决方法如下:
?
THEN SET new.created_at=now(); END IF; END; | delimiter ;
mysql> INSERT INTO temp(name,created_at,updated_at) VALUES('robin',now(),now()); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO temp(name,created_at,updated_at) VALUES('wentasy',now(),now()); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM temp; +----+---------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+---------+---------------------+---------------------+ | 1 | robin | 2014-09-01 14:08:36 | 2014-09-01 14:08:36 | | 2 | wentasy | 2014-09-01 14:08:44 | 2014-09-01 14:08:44 | +----+---------+---------------------+---------------------+ 2 rows in set (0.00 sec)
mysql> UPDATE temp SET name='robinwen' WHERE id=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 #可以看到已经记录了第一条数据的更新时间 mysql> SELECT * FROM temp; +----+----------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+----------+---------------------+---------------------+ | 1 | robinwen | 2014-09-01 14:08:36 | 2014-09-01 14:09:09 | | 2 | wentasy | 2014-09-01 14:08:44 | 2014-09-01 14:08:44 | +----+----------+---------------------+---------------------+ 2 rows in set (0.00 sec)
第三种,created_at指定timestamp DEFAULT '0000-00-00 00:00:00',updated_at指定DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP或者timestamp DEFAULT now() ON UPDATE now(); CREATE TABLE temp ( id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10), created_at timestamp NULL DEFAULT '0000-00-00 00:00:00', updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
mysql> INSERT INTO temp(name,created_at,updated_at) VALUES('robin',now(),now()); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO temp(name,created_at,updated_at) VALUES('wentasy',now(),now()); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM temp; +----+---------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+---------+---------------------+---------------------+ | 1 | robin | 2014-09-01 14:10:43 | 2014-09-01 14:10:43 | | 2 | wentasy | 2014-09-01 14:10:57 | 2014-09-01 14:10:57 | +----+---------+---------------------+---------------------+ 2 rows in set (0.00 sec)
mysql> UPDATE temp SET name='robinwen' WHERE id=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 #可以看到已经记录了第一条数据的更新时间 mysql> SELECT * FROM temp; +----+----------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+----------+---------------------+---------------------+ | 1 | robinwen | 2014-09-01 14:10:43 | 2014-09-01 14:11:24 | | 2 | wentasy | 2014-09-01 14:10:57 | 2014-09-01 14:10:57 | +----+----------+---------------------+---------------------+ 2 rows in set (0.00 sec)
第四种,更换MySQL版本,MySQL 5.6已经去除了此限制。
我们可以看下MySQL 5.5和5.6帮助文档对于这个问题的解释。
From the MySQL 5.5 documentation:
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value
for another column.
Changes in MySQL 5.6.5:
Previously, at most one TIMESTAMP column per table could be automatically initialized or updated to the current date and time. This restriction has been lifted. Any TIMESTAMP column definition can have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE
CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used with DATETIME column definitions. For more information, see Automatic Initialization and Updating for TIMESTAMP and DATETIME.
我们确定下MySQL的版本。
mysql> SELECT VERSION(); +---------------------------------------+ | VERSION() | +---------------------------------------+ | 5.6.20-enterprise-commercial-advanced | +---------------------------------------+ 1 row in set (0.00 sec) CREATE TABLE temp ( id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10), created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); Query OK, 0 rows affected (0.28 sec)
mysql> INSERT INTO temp(name) VALUES('robin'); Query OK, 1 row affected (0.07 sec) mysql> INSERT INTO temp(name) VALUES('wentasy'); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM temp; +----+---------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+---------+---------------------+---------------------+ | 1 | robin | 2014-09-01 15:05:57 | 2014-09-01 15:05:57 | | 2 | wentasy | 2014-09-01 15:06:02 | 2014-09-01 15:06:02 | +----+---------+---------------------+---------------------+ 2 rows in set (0.01 sec) mysql> UPDATE temp SET name='robinwen' WHERE id=1; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 #可以看到已经记录了第一条数据的更新时间 mysql> SELECT * FROM temp; +----+----------+---------------------+---------------------+ | id | name | created_at | updated_at | +----+----------+---------------------+---------------------+ | 1 | robinwen | 2014-09-01 15:05:57 | 2014-09-01 15:06:45 | | 2 | wentasy | 2014-09-01 15:06:02 | 2014-09-01 15:06:02 | +----+----------+---------------------+---------------------+ 2 rows in set (0.00 sec)
总结
平时比较常用的时间、字符串、时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法;本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串、date转时间戳、字符串转date、字符串转时间戳、时间戳转date,时间戳转字符串)用法,方便日后查看;
涉及的函数
date_format(date, format) 函数,MySQL日期格式化函数date_format()unix_timestamp() 函数
str_to_date(str, format) 函数
from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime
时间转字符串
select date_format(now(), '%Y-%m-%d'); #结果:2016-01-05
时间转时间戳
select unix_timestamp(now()); #结果:1452001082
字符串转时间
select str_to_date('2016-01-02', '%Y-%m-%d %H'); #结果:2016-01-02 00:00:00
字符串转时间戳
select unix_timestamp('2016-01-02'); #结果:1451664000
时间戳转时间
select from_unixtime(1451997924); #结果:2016-01-05 20:45:24
时间戳转字符串
select from_unixtime(1451997924,'%Y-%d'); //结果:2016-01-05 20:45:24
附表
MySQL日期格式化(format)取值范围。值 | 含义 | |
---|---|---|
秒 | %S、%s | 两位数字形式的秒( 00,01, ..., 59) |
分 | %I、%i | 两位数字形式的分( 00,01, ..., 59) |
小时 | %H | 24小时制,两位数形式小时(00,01, ...,23) |
%h | 12小时制,两位数形式小时(00,01, ...,12) | |
%k | 24小时制,数形式小时(0,1, ...,23) | |
%l | 12小时制,数形式小时(0,1, ...,12) | |
%T | 24小时制,时间形式(HH:mm:ss) | |
%r | 12小时制,时间形式(hh:mm:ss AM 或 PM) | |
%p | AM上午或PM下午 | |
周 | %W | 一周中每一天的名称(Sunday,Monday, ...,Saturday) |
%a | 一周中每一天名称的缩写(Sun,Mon, ...,Sat) | |
%w | 以数字形式标识周(0=Sunday,1=Monday, ...,6=Saturday) | |
%U | 数字表示周数,星期天为周中第一天 | |
%u | 数字表示周数,星期一为周中第一天 | |
天 | %d | 两位数字表示月中天数(01,02, ...,31) |
%e | 数字表示月中天数(1,2, ...,31) | |
%D | 英文后缀表示月中天数(1st,2nd,3rd ...) | |
%j | 以三位数字表示年中天数(001,002, ...,366) | |
月 | %M | 英文月名(January,February, ...,December) |
%b | 英文缩写月名(Jan,Feb, ...,Dec) | |
%m | 两位数字表示月份(01,02, ...,12) | |
%c | 数字表示月份(1,2, ...,12) | |
年 | %Y | 四位数字表示的年份(2015,2016...) |
%y | 两位数字表示的年份(15,16...) | |
文字输出 | %文字 | 直接输出文字内容 |
相关文章推荐
- MySQL中同时存在创建和上次更新时间戳字段解决方法浅析
- MySQL中同时存在创建和上次更新时间戳字段解决方法浅析
- MySQL中同一时候存在创建和上次更新时间戳字段解决方法浅析
- Java获取数据库日期字段,后面有.0的解决方法。 |浏览:2416|更新:2013-06-27 17:22|标签:java 我们一般获取数据库字符串,在得到行后,就会用getString获取字符串
- mysql创建存储过程(根据时间字段来更新数据)
- MySQL中不能创建自增字段的解决方法
- mysql 创建表时 日期字段默认值为当前时间
- .net中更新已存在表字段的解决方法
- MySQL中日期类型数据的使用:如何让字段保存记录的创建时间和最后修改时间
- MySQL--创建时间和更新时间字段
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- MySQL--操作简记(创建计算字段,数据处理函数(文本,时间日期,数值))
- mysql新建表datetime类型字段不能设置当前默认时间解决方法
- mysql创建一个改变固定字段才更新时间的时间戳timestamp
- mysql创建与更新时间字段
- MySQL中不能创建自增字段的解决方法
- mysql更新表中日期字段时间
- mysql创建自动更新时间字段
- JDBC操作MySQL5日期类型字段的问题解决方法
- mysql jsp 解决乱码 创建数据库 数据表 方法