1

MySQL is giving me a hard time to update a timestamp column

mysql> describe payments;
+---------+------------------+------+-----+-------------------+----------------+
| Field   | Type             | Null | Key | Default           | Extra          |
+---------+------------------+------+-----+-------------------+----------------+
| id      | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| user_id | int(10) unsigned | NO   |     | NULL              |                |
| cat_id  | int(10) unsigned | NO   |     | NULL              |                |
| amount  | int(11)          | NO   |     | NULL              |                |
| time    | timestamp        | NO   |     | CURRENT_TIMESTAMP |                |
+---------+------------------+------+-----+-------------------+----------------+
5 rows in set (0.00 sec)

I would like to set the time of record 1 to Jan 1st 2040

mysql> select * from payments;
+----+---------+--------+--------+---------------------+
| id | user_id | cat_id | amount | time                |
+----+---------+--------+--------+---------------------+
|  1 |       1 |      4 |     40 | 0000-00-00 00:00:00 |
|  2 |       1 |      3 |     30 | 2030-01-01 00:00:00 |
+----+---------+--------+--------+---------------------+
2 rows in set (0.00 sec)

But it gives me an out of range warning

mysql> update payments set time='2040-01-01 00:00:00' where id=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> show warnings;
+---------+------+-----------------------------------------------+
| Level   | Code | Message                                       |
+---------+------+-----------------------------------------------+
| Warning | 1264 | Out of range value for column 'time' at row 1 |
+---------+------+-----------------------------------------------+
1 row in set (0.00 sec)

Am I missing something obvious?

MonoThreaded
  • 11,429
  • 12
  • 71
  • 102

3 Answers3

3

Here is the answer you're looking for:

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

Source: http://dev.mysql.com/doc/refman/5.0/en/datetime.html

The date you're trying to set is to high.

Joël Salamin
  • 3,538
  • 3
  • 22
  • 33
2

I think you need to change the datatype of your column from Timestamp to Datetime as for the limitation of Timestamp to take the maximum data as '2038-01-19 03:14:07' UTC..

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

You need to escape the column "time" since it's a keyword in MySql.

`time`

See this answer: i can't use 'time' word in mysql query?

Community
  • 1
  • 1
ShayneStatzell
  • 122
  • 1
  • 11