一张表中的两个 MySQL 时间戳列
我想创建一个表,其中有一列用于“创建”,另一列用于“更新”。 “创建”列将在插入时设置并且永远不会更改。每次更新行时,“已更新”列都会更改。我不想在后续的 INSERT 或 UPDATE 语句中弄乱这些列中的任何一个。那么,如果我从这样的内容开始,我的 CREATE TABLE 语句应该是什么样子呢?
CREATE TABLE IF NOT EXISTS `mydb`.`mytable` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`updated` TIMESTAMP,
`created` TIMESTAMP,
`deleted` TINYINT DEFAULT 0,
`notes` TEXT DEFAULT '',
`description` VARCHAR(100)
) TYPE=innodb;
我似乎在创建具有两个 TIMESTAMP 列的表时遇到问题。我不关心这些列是 TIMESTAMP 还是 DATETIME 或其他什么,我只是希望它们由 MySQL 填充,而无需插入或更新语句的显式指令。
我希望能够执行这样的插入:
INSERT INTO `mydb`.`mytable` (notes,description) VALUES ('some note','some description');
和这样的更新:
UPDATE `mydb`.`mytable` SET notes=CONCAT(notes,'some more notes') WHERE id=1;
都不需要在插入或更新语句中显式设置“创建”列或设置(或重置)“更新”列。
I would like to create a table that has both a column for "created" and another for "updated". The column "created" will be set at insert and never change. The column "updated" will change every time a row is updated. I don't want to mess with either of these columns in the subsequent INSERT or UPDATE statements. So what should my CREATE TABLE statement look like if I start with something like this?
CREATE TABLE IF NOT EXISTS `mydb`.`mytable` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`updated` TIMESTAMP,
`created` TIMESTAMP,
`deleted` TINYINT DEFAULT 0,
`notes` TEXT DEFAULT '',
`description` VARCHAR(100)
) TYPE=innodb;
I seem to be having trouble creating a table with two TIMESTAMP columns. I don't care if the columns are TIMESTAMP or DATETIME or whatever, I just want them to be populated by MySQL without explicit instructions from the insert or update statements.
I would like to be able to do inserts like this:
INSERT INTO `mydb`.`mytable` (notes,description) VALUES ('some note','some description');
and updates like this:
UPDATE `mydb`.`mytable` SET notes=CONCAT(notes,'some more notes') WHERE id=1;
both without having to explicitly set the "created" column or set (or reset) the "updated" column in the insert or update statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这个来创建您的表:
请注意,这
将允许自动更新此字段。
并在插入记录之前将其设置为触发器:
然后您可以使用它来插入:
并更新您的记录
并且您创建和更新的字段将被适当设置。
Try this one to create your table:
Note that
will allow to update this field automatically.
And set this one for a trigger before inserting records:
Then you can use this to insert:
and to update your record
And your created and updated fields will be set appropiately.
新闻快报:在 mysql 中,每次更新行中的任何其他列时,
TIMESTAMP
列总是使用now()
进行更新 - 这是一个这种数据类型的特意特征。另一方面,DATETIME 没有这种奇怪的行为 - 这是完全正常的。
答案:
创建
必须是DATETIME
,但由于这个bug,你还需要一个触发器,像这样:News flash: In mysql,
TIMESTAMP
columns are always updated withnow()
every time any other column in the row is updated - this is a deliberate feature of this datatype.DATETIME
on the other hand does not have this weird behaviour - it's completely normal.The answer:
created
must beDATETIME
, but due to this bug, you also need a trigger, like this:试试这个:
编辑:使用触发器。
Try this:
Edit: Use a trigger.
另一种方法是更改时间戳列的顺序
或
设置第一列默认值,如下所示
参考
alternative is to change the order of timestamp column
OR
set first column DEFAULT value like this
Reference
不幸的是,MySQL 不允许在一张表中拥有两个 TIMESTAMP 列。我将使用 ON UPDATE CURRENT_TIMESTAMP 来更新列,并使用 NOW() 函数手动创建集。
Unfortunately MySQL doesn't let you have two TIMESTAMP columns in one table. I would use ON UPDATE CURRENT_TIMESTAMP for the updated column and set created manually using the NOW() function.