Это ожидаемое поведение.
В отличие от других баз данных, в MySQL TIMESTAMP
столбцы ВСЕГДА обновляются с now()
при каждом обновлении строки.Это преднамеренная особенность типа данных TIMESTAMP.
Редактировать: Обратите внимание, что я говорю здесь о TIMESTAMP
, , а не TIMESTAMP DEFAULT NULL
или любых других вариантах.
Вам нужен DATETIME
тип данных - они ведут себя как обычные столбцы.
Вот некоторый тестовый SQL, чтобы показать его поведение:
create table timestamp_datatype (id int, dt datetime, ts timestamp);
-- test 1: leaving ts to default - you get now()
insert into timestamp_datatype (id, dt) values (1, '2011-01-01 01:01:01');
-- test 2: trying to give ts a value - this works
insert into timestamp_datatype (id, dt, ts) values (2, '2011-01-01 01:01:01', '2011-01-01 01:01:01');
-- test 3: specifying null for ts - this doesn't work - you get now()
insert into timestamp_datatype (id, dt, ts) values (3, '2011-01-01 01:01:01', null);
-- test 4: updating the row - ts is updated too
insert into timestamp_datatype (id, dt, ts) values (4, '2011-01-01 01:01:01', '2011-01-01 01:01:01');
update timestamp_datatype set dt = now() where id = 4; -- ts is updated to now()
select * from timestamp_datatype;
+------+---------------------+---------------------+
| id | dt | ts |
+------+---------------------+---------------------+
| 1 | 2011-01-01 01:01:01 | 2011-07-05 09:50:24 |
| 2 | 2011-01-01 01:01:01 | 2011-01-01 01:01:01 |
| 3 | 2011-01-01 01:01:01 | 2011-07-05 09:50:24 |
| 4 | 2011-07-05 09:50:24 | 2011-07-05 09:50:24 |
+------+---------------------+---------------------+