仅在插入新行时设置 current_timestamp
set current_timestamp only on inserting a new row
列 date
是 timestamp
- 默认值 - CURRENT_TIMESTAMP
我选择这个是因为想要在 inserting
新行上显示当前日期时间。
但是每次更新另一列时它都会改变。
有没有办法让 current_timestamp
仅在插入新行时 而不是 在更新另一列时?
来自 the documentation :
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).
因此,您要避免 my_date_col
列在 UPDATE 时自动更新,您希望明确保留其值,例如:
UPDATE my_table SET my_col1 = 'foo', my_col2 = 'bar', my_date_col = my_date_col
WHERE ...
TIMESTAMP
列在每次更新时自动更新。 DATETIME
列没有。
你想要:
your_column DATETIME DEFAULT CURRENT_TIMESTAMP
这让您可以在插入时指定一个默认值,就像您对任何列所做的那样,但是因为它不再是 TIMESTAMP
它不会继续在每个未来更新自己 UPDATE
.
当你运行
SHOW FULL COLUMNS FROM `moufa`; -- where `moufa` is the name of the table in the example
你会看到这样的东西
+-------+-------------+-----------+------+-----+---------------------+-------------------------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+-------------+-----------+------+-----+---------------------+-------------------------------+---------------------------------+---------+
| id | smallint(6) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| ts | timestamp | NULL | NO | | current_timestamp() | on update current_timestamp() | select,insert,update,references | |
+-------+-------------+-----------+------+-----+---------------------+-------------------------------+---------------------------------+---------+
问题出在 Extra
的 ts
字段上。检查你的 table 运行
SHOW CREATE TABLE `moufa`;
像GMB这样的最常见案例已自动更新专栏。
现在为了避免更多类似的情况,您应该明确定义列的默认值。
DROP TABLE IF EXISTS `moufa`; -- just for the example
CREATE TABLE `moufa`(
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`)
);
或者不是删除并重新创建 table
ALTER TABLE `moufa`
MODIFY COLUMN `ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
列 date
是 timestamp
- 默认值 - CURRENT_TIMESTAMP
我选择这个是因为想要在 inserting
新行上显示当前日期时间。
但是每次更新另一列时它都会改变。
有没有办法让 current_timestamp
仅在插入新行时 而不是 在更新另一列时?
来自 the documentation :
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).
因此,您要避免 my_date_col
列在 UPDATE 时自动更新,您希望明确保留其值,例如:
UPDATE my_table SET my_col1 = 'foo', my_col2 = 'bar', my_date_col = my_date_col
WHERE ...
TIMESTAMP
列在每次更新时自动更新。 DATETIME
列没有。
你想要:
your_column DATETIME DEFAULT CURRENT_TIMESTAMP
这让您可以在插入时指定一个默认值,就像您对任何列所做的那样,但是因为它不再是 TIMESTAMP
它不会继续在每个未来更新自己 UPDATE
.
当你运行
SHOW FULL COLUMNS FROM `moufa`; -- where `moufa` is the name of the table in the example
你会看到这样的东西
+-------+-------------+-----------+------+-----+---------------------+-------------------------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+-------------+-----------+------+-----+---------------------+-------------------------------+---------------------------------+---------+
| id | smallint(6) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| ts | timestamp | NULL | NO | | current_timestamp() | on update current_timestamp() | select,insert,update,references | |
+-------+-------------+-----------+------+-----+---------------------+-------------------------------+---------------------------------+---------+
问题出在 Extra
的 ts
字段上。检查你的 table 运行
SHOW CREATE TABLE `moufa`;
像GMB这样的最常见案例已自动更新专栏。
现在为了避免更多类似的情况,您应该明确定义列的默认值。
DROP TABLE IF EXISTS `moufa`; -- just for the example
CREATE TABLE `moufa`(
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`)
);
或者不是删除并重新创建 table
ALTER TABLE `moufa`
MODIFY COLUMN `ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP;