> 데이터 베이스 > MySQL 튜토리얼 > MySQL中同时存在创建和上次更新时间戳字段解决方法浅析_MySQL

MySQL中同时存在创建和上次更新时间戳字段解决方法浅析_MySQL

WBOY
풀어 주다: 2016-06-01 13:02:23
원래의
1176명이 탐색했습니다.

在写这篇文章之前,明确我的MySQL版本。

mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.5.29-log | +------------+ 1 row in set (0.00 sec)
로그인 후 복사
第一个,测试通过。  
로그인 후 복사
CREATE TABLE temp
(
	id INT(11) PRIMARY KEY AUTO_INCREMENT,
	name VARCHAR(10),
	updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
로그인 후 복사
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
);
로그인 후 복사
第一种,created_at使用DEFAULT CURRENT_TIMESTAMP或者DEFAULT now(),updated_at使用触发器。
CREATE TABLE temp
(
	id INT(11) PRIMARY KEY AUTO_INCREMENT,
	name VARCHAR(10),
	created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
	updated_at timestamp NULL
);
로그인 후 복사
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_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)
로그인 후 복사
<strong>第二种</strong>,created_at使用触发器,updated_at使用DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP或者DEFAULT now() ON UPDATE now();
로그인 후 복사

具体解决方法如下:

CREATE TABLE temp
(
	id INT(11) PRIMARY KEY AUTO_INCREMENT,
	name VARCHAR(10),
	created_at timestamp NULL,
	updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
로그인 후 복사
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> INSERT INTO temp(name,created_at,updated_at) VALUES(&#39;robin&#39;,now(),now()); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO temp(name,created_at,updated_at) VALUES(&#39;wentasy&#39;,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=&#39;robinwen&#39; 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 &#39;0000-00-00 00:00:00&#39;,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 &#39;0000-00-00 00:00:00&#39;, updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
로그인 후 복사
mysql> INSERT INTO temp(name,created_at,updated_at) VALUES(&#39;robin&#39;,now(),now()); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO temp(name,created_at,updated_at) VALUES(&#39;wentasy&#39;,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=&#39;robinwen&#39; 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)
로그인 후 복사
<strong>第四种</strong>,更换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(&#39;robin&#39;); Query OK, 1 row affected (0.07 sec) mysql> INSERT INTO temp(name) VALUES(&#39;wentasy&#39;); 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=&#39;robinwen&#39; 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)
로그인 후 복사
总结
로그인 후 복사

Good Luck!

Robin

2014年9月1日

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿