英文:
can I safely assign a column to the value of an automatically updated column?
问题
如果列active
从False更改为True,我还想将activated_at
更改为与updated_at
相同的值。查询将使用新的post-on update
值还是旧值,或者行为未定义?
英文:
my updated_at
column is set to current timestame ON UPDATE
.
If the column active
was False and is changed to True, I also want to change activated_at
to be the same value as updated_at
.
will the query
UPDATE mytable
SET active=TRUE, activated_at=mytable.updated_at
WHERE active=FALSE;
use the new post-on update
value of updated_at
, or the old one, or is the behavior undefined?
答案1
得分: 1
I suppose a simple test will probably get the answer faster for you than asking.
create table mytable(active bool, activated_at datetime, updated_at timestamp);
insert mytable values
(false,'2023-01-01 11:11:11',default),
(true,'2023-01-03 12:11:11',default),
(false,'2023-01-05 12:21:11',default);
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 0 | 2023-01-01 11:11:11 | 2023-05-17 01:45:11 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:45:11 |
| 0 | 2023-01-05 12:21:11 | 2023-05-17 01:45:11 |
+--------+---------------------+---------------------+
Let's try your query:
UPDATE mytable
SET active=TRUE, activated_at=mytable.updated_at
WHERE active=FALSE;
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 1 | 2023-05-17 01:45:11 | 2023-05-17 01:47:28 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:45:11 |
| 1 | 2023-05-17 01:45:11 | 2023-05-17 01:47:28 |
+--------+---------------------+---------------------+
As demonstrated above, the updated rows are using the OLD values before the timestamp auto-update kicks in. To circumvent that, we can simply set the value to the current datetime:
-- Let's truncate/insert the table and do it all over again.
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 0 | 2023-01-01 11:11:11 | 2023-05-17 01:54:31 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:54:31 |
| 0 | 2023-01-05 12:21:11 | 2023-05-17 01:54:31 |
+--------+---------------------+---------------------+
Here is the slightly modified query:
UPDATE mytable
SET active=TRUE, activated_at=current_timestamp()
WHERE active=FALSE;
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 1 | 2023-05-17 01:56:11 | 2023-05-17 01:56:11 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:54:31 |
| 1 | 2023-05-17 01:56:11 | 2023-05-17 01:56:11 |
+--------+---------------------+---------------------+
That's it.
英文:
I suppose a simple test will probably get the answer faster for you than asking.
create table mytable(active bool,activated_at datetime,updated_at timestamp);
insert mytable values
(false,'2023-01-01 11:11:11',default),
(true,'2023-01-03 12:11:11',default),
(false,'2023-01-05 12:21:11',default);
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 0 | 2023-01-01 11:11:11 | 2023-05-17 01:45:11 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:45:11 |
| 0 | 2023-01-05 12:21:11 | 2023-05-17 01:45:11 |
+--------+---------------------+---------------------+
Let's try your query:
UPDATE mytable
SET active=TRUE, activated_at=mytable.updated_at
WHERE active=FALSE;
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 1 | 2023-05-17 01:45:11 | 2023-05-17 01:47:28 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:45:11 |
| 1 | 2023-05-17 01:45:11 | 2023-05-17 01:47:28 |
+--------+---------------------+---------------------+
As demonstrated above, the updated rows are using the OLD values before the timestamp auto-update kicks in. To circumvent that, we can simply set the value to the current datetime:
-- Let's truncate/insert the table and do it all over again.
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 0 | 2023-01-01 11:11:11 | 2023-05-17 01:54:31 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:54:31 |
| 0 | 2023-01-05 12:21:11 | 2023-05-17 01:54:31 |
+--------+---------------------+---------------------+
Here is the slightly modified query:
UPDATE mytable
SET active=TRUE, activated_at=current_timestamp()
WHERE active=FALSE;
select * from mytable;
+--------+---------------------+---------------------+
| active | activated_at | updated_at |
+--------+---------------------+---------------------+
| 1 | 2023-05-17 01:56:11 | 2023-05-17 01:56:11 |
| 1 | 2023-01-03 12:11:11 | 2023-05-17 01:54:31 |
| 1 | 2023-05-17 01:56:11 | 2023-05-17 01:56:11 |
+--------+---------------------+---------------------+
That's it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论