dco*_*bus 5 mysql database timestamp phpmyadmin
我的TIMESTAMPS正在发生一些非常恼人的事......
我在我的表中有一个"createdat","deletedat"和"updatedat"列...我已经设置了我的deletedat并将其更新为NULL和DEFAULT NULL ...但是,当添加新记录时,NOW( )函数总是针对deletedat和updatedat执行,而不是仅仅将其保留为NULL.
所以我最终得到:00:00:00 ...
为什么它不是默认为NULL?
这是我的表:

这是插入时(注意选择NOW功能):

执行以下SQL:
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());
Run Code Online (Sandbox Code Playgroud)
这是预期的行为。
与其他数据库不同,MySQL 中的TIMESTAMP列总是在行更新时更新now()。这是 TIMESTAMP 数据类型特意设计的一个特性。
编辑:请注意,我在这里谈论的是TIMESTAMP、not 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 |
+------+---------------------+---------------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23045 次 |
| 最近记录: |