开发者

MySQL, phpMyAdmin: TIMESTAMP Always Executes NOW Function

There's something very irritating going on with my TIMESTAMPS ...

I have a "createdat", "deletedat" and "updatedat" column in my Table... I've set my deletedat and updated at to NULL and DEFA开发者_开发技巧ULT NULL ... however, when a new record is added, the NOW() function is always executed for deletedat and updatedat instead of just leaving it as NULL.

So I end up with: 00:00:00 ...

Why isn't it just defaulting to NULL?

Here's my Table:

MySQL, phpMyAdmin: TIMESTAMP Always Executes NOW Function

Here's when Inserting (notice the NOW function is selected):

MySQL, phpMyAdmin: TIMESTAMP Always Executes NOW Function

The following SQL is executed:

INSERT INTO `MYTABLE_DEV`.`messages` (`id`, `fromUserId`, `toUserId`, `subject`, `body`, `createdat`, `updatedat`, `deletedat`) VALUES (NULL, '1', '3', 'Message', 'This is another message.', CURRENT_TIMESTAMP, NOW(), NOW());


You are executing NOW() instead of setting null. Use this query:

INSERT INTO `MYTABLE_DEV`.`messages` (`id`, `fromUserId`, `toUserId`, `subject`, `body`) VALUES (NULL, '1', '3', 'Message', 'This is another message.');

or with all fields...

INSERT INTO `MYTABLE_DEV`.`messages` (`id`, `fromUserId`, `toUserId`, `subject`, `body`, `createdat`, `updatedat`, `deletedat`) VALUES (NULL, '1', '3', 'Message', 'This is another message.', CURRENT_TIMESTAMP, NULL, NULL);


This is expected behaviour.

Unlike other databases, in MySQL TIMESTAMP columns are ALWAYS updated with now() whenever the row is updated. This is a deliberate feature of the TIMESTAMP datatype.

Edit: Note that I am talking here about TIMESTAMP, not TIMESTAMP DEFAULT NULL or any other variations.

What you want is a DATETIME datatype - they behave as normal columns.

Here's some test SQL to show its behaviour:

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 |
+------+---------------------+---------------------+


Let it be here (little patch upcoming in 3.5.2 which compleatly solves this problem): the bug report


This might be a bug/feature in phpMyAdmin. Have a look at this bug report, and especially the comment added at 2010-06-13 11:06:47 UTC. The interesting part:

You can question however, the default value of NOW() in the selectionbox when doing an INSERT/UPDATE, when the default value of the timestamp field is set to NULL. This is maybe not what you would suspect when setting the default to NULL.

If this is true, I guess that's just the way phpMyAdmin works. I don't use phpMyAdmin myself, so I have no personal experience from this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜