Reputation: 159
I have a column which contains a timestamp in my MySQL database. No matter how many times I update my records, my timestamps are not updated. Is there anything I should do to make it auto update?
Searching other questions here in Stackoverflow, most of them say the opposite, it's really very confusing! For example, this one, Should I use field 'datetime' or 'timestamp'? (Look at the top rated answer)
Thank you
Here is my table structure
+----------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+-------------------+----------------+
| Num | int(11) | NO | PRI | NULL | auto_increment |
| mem_name | varchar(50) | NO | | NULL | |
| mem_id | int(11) | NO | | NULL | |
| car_number | varchar(11) | NO | | NULL | |
| firstcall_time | timestamp | NO | | CURRENT_TIMESTAMP | |
| tow_time | datetime | NO | | NULL | |
+----------------+-------------+------+-----+-------------------+----------------+
6 rows in set (0.00 sec)
One Record:
+-----+----------+--------+------------+---------------------+---------------------+
| Num | mem_name | mem_id | car_number | firstcall_time | tow_time |
+-----+----------+--------+------------+---------------------+---------------------+
| 1 | Jakey | 54564 | SHA34345 | 2013-02-19 16:14:12 | 2013-02-19 16:32:45 |
+-----+----------+--------+------------+---------------------+---------------------+
1 row in set (0.00 sec)
After Updating:
+-----+----------+--------+------------+---------------------+---------------------+
| Num | mem_name | mem_id | car_number | firstcall_time | tow_time |
+-----+----------+--------+------------+---------------------+---------------------+
| 1 | Tommy | 54564 | SHA34345 | 2013-02-19 16:14:12 | 2013-02-19 16:32:45 |
+-----+----------+--------+------------+---------------------+---------------------+
1 row in set (0.00 sec)
Upvotes: 0
Views: 9677
Reputation: 22071
Add On UPDATE CURRENT_TIMESTAMP
MySQL Query:
ALTER TABLE `table_name` CHANGE `date_field` `date_field` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Hope it helps !
Upvotes: 5
Reputation: 8553
The main reason that your timestamp is not updating beacuse you have not added ON UPDATE CURRENT_TIMESTAMP
clause in your table creation statement.
Change it with alter table command and modify your column
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP,
the column has the current timestamp for its default value and
is automatically updated to the current timestamp.
You must add On UPDATE CURRENT_TIMESTAMP
CREATE TABLE t1 (
id int, ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
Edit - Table modifying command
alter table <tablename> modify <columnName> TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
Refer http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
Upvotes: 7