1. <legend id='EhpYD'><style id='EhpYD'><dir id='EhpYD'><q id='EhpYD'></q></dir></style></legend>

    <small id='EhpYD'></small><noframes id='EhpYD'>

    • <bdo id='EhpYD'></bdo><ul id='EhpYD'></ul>
    <tfoot id='EhpYD'></tfoot>

      <i id='EhpYD'><tr id='EhpYD'><dt id='EhpYD'><q id='EhpYD'><span id='EhpYD'><b id='EhpYD'><form id='EhpYD'><ins id='EhpYD'></ins><ul id='EhpYD'></ul><sub id='EhpYD'></sub></form><legend id='EhpYD'></legend><bdo id='EhpYD'><pre id='EhpYD'><center id='EhpYD'></center></pre></bdo></b><th id='EhpYD'></th></span></q></dt></tr></i><div id='EhpYD'><tfoot id='EhpYD'></tfoot><dl id='EhpYD'><fieldset id='EhpYD'></fieldset></dl></div>

      MySQL CURRENT_TIMESTAMP 字段在每次更新时更新

      MySQL CURRENT_TIMESTAMP field updates on every update(MySQL CURRENT_TIMESTAMP 字段在每次更新时更新)

            <tfoot id='dR5ep'></tfoot>
          • <small id='dR5ep'></small><noframes id='dR5ep'>

            <legend id='dR5ep'><style id='dR5ep'><dir id='dR5ep'><q id='dR5ep'></q></dir></style></legend>
            <i id='dR5ep'><tr id='dR5ep'><dt id='dR5ep'><q id='dR5ep'><span id='dR5ep'><b id='dR5ep'><form id='dR5ep'><ins id='dR5ep'></ins><ul id='dR5ep'></ul><sub id='dR5ep'></sub></form><legend id='dR5ep'></legend><bdo id='dR5ep'><pre id='dR5ep'><center id='dR5ep'></center></pre></bdo></b><th id='dR5ep'></th></span></q></dt></tr></i><div id='dR5ep'><tfoot id='dR5ep'></tfoot><dl id='dR5ep'><fieldset id='dR5ep'></fieldset></dl></div>
              <bdo id='dR5ep'></bdo><ul id='dR5ep'></ul>

                  <tbody id='dR5ep'></tbody>

                本文介绍了MySQL CURRENT_TIMESTAMP 字段在每次更新时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                解释这个问题的最好方法是举个例子.

                我有一张桌子:

                CREATE TABLE `example` (`id` int(11) NOT NULL AUTO_INCREMENT,`data` varchar(255) 默认为空,`created` 时间戳 NOT NULL DEFAULT CURRENT_TIMESTAMP,`updated` 日期时间 DEFAULT NULL,主键(`id`)) 引擎=MyISAM 默认字符集=utf8

                结果:

                <上一页>编号 |数据 |创建 |更新(空)|(NULL) |(NULL) |(空值)

                然后我插入一些数据:

                INSERT INTO 示例 (`数据`) 价值观 ('abc123')

                结果:

                <上一页>编号 |数据 |创建 |更新1 |abc123 |2013-01-16 13:12:16 |(空值)

                然后我更新

                UPDATE 示例 SET`数据` = 'def456',`更新`=现在()哪里 id = 1

                结果:

                <上一页>编号 |数据 |创建 |更新1 |def456 |2013-01-16 13:16:24 |2013-01-16 13:14:26

                问题:注意 created 字段如何更新,并且正确保存更新字段的时间略有不同.我已经在同一个数据库上设置了这个示例表和其他类似的表,没有这个问题,所以我完全被它弄糊涂了.

                解决方案

                这很可能是问题表表是如何被意外创建的:

                CREATE TABLE `example` (`id` INT(11) NOT NULL AUTO_INCREMENT,`data` VARCHAR(255) 默认为空,`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,`更新的`日期时间默认为空,主键(`id`)) 引擎=MYISAM 默认字符集=utf8;

                也许有人使用 3rd 方软件创建它?

                ON UPDATE CURRENT_TIMESTAMP 将破坏创建日期.所以要解决问题像这样使用ALTER TABLE:

                ALTER TABLE 示例 CHANGE created created TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

                这将消除每次更新时对 created 字段的不必要覆盖.

                The best way to explain this problem is with an example.

                I have a table:

                CREATE TABLE `example` (
                  `id` int(11) NOT NULL AUTO_INCREMENT,
                  `data` varchar(255) DEFAULT NULL,
                  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                  `updated` datetime DEFAULT NULL,
                  PRIMARY KEY (`id`)
                ) ENGINE=MyISAM DEFAULT CHARSET=utf8
                

                Result:

                   id  |  data  |       created       |       updated
                 (NULL)| (NULL) |       (NULL)        |       (NULL)
                

                Then I insert some data:

                INSERT INTO example (
                  `data`
                ) VALUES (
                  'abc123'
                )
                

                Result:

                  id  |  data  |       created       |       updated
                   1  | abc123 | 2013-01-16 13:12:16 |       (NULL)
                

                And then I update

                UPDATE example SET 
                  `data` = 'def456',
                  `updated` = NOW()
                WHERE id = 1
                

                Result:

                  id  |  data  |       created       |       updated
                   1  | def456 | 2013-01-16 13:16:24 | 2013-01-16 13:14:26
                

                The problem: Notice how the created field also updates and has a slightly different time to correctly saved updated field. I have set up this example table and others similarly on the same database without this problem, so I'm completely baffled by it.

                解决方案

                This is likely how the problem table table was accidentally created:

                CREATE TABLE `example` (
                  `id` INT(11) NOT NULL AUTO_INCREMENT,
                  `data` VARCHAR(255) DEFAULT NULL,
                  `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                  `updated` DATETIME DEFAULT NULL,
                  PRIMARY KEY (`id`)
                ) ENGINE=MYISAM DEFAULT CHARSET=utf8;
                

                Perhaps someone used 3rd party software to create it?

                An ON UPDATE CURRENT_TIMESTAMP is going to destroy the create date. So to solve the problem use ALTER TABLE like so:

                ALTER TABLE example CHANGE created created TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
                

                This will get rid of the unwanted overwrite of the created field on every update.

                这篇关于MySQL CURRENT_TIMESTAMP 字段在每次更新时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Converting string to MySQL timestamp format in php(在php中将字符串转换为MySQL时间戳格式)
                datetime to timestamp(日期时间到时间戳)
                PHP timestamp date to user timezone(PHP时间戳日期到用户时区)
                Converting TIMESTAMP to unix time in PHP?(在 PHP 中将 TIMESTAMP 转换为 unix 时间?)
                Convert ISO 8601 to unixtimestamp(将 ISO 8601 转换为 unixtimestamp)
                Finding days between 2 unix timestamps in php(在php中查找2个unix时间戳之间的天数)

                    <small id='iJvgY'></small><noframes id='iJvgY'>

                  • <i id='iJvgY'><tr id='iJvgY'><dt id='iJvgY'><q id='iJvgY'><span id='iJvgY'><b id='iJvgY'><form id='iJvgY'><ins id='iJvgY'></ins><ul id='iJvgY'></ul><sub id='iJvgY'></sub></form><legend id='iJvgY'></legend><bdo id='iJvgY'><pre id='iJvgY'><center id='iJvgY'></center></pre></bdo></b><th id='iJvgY'></th></span></q></dt></tr></i><div id='iJvgY'><tfoot id='iJvgY'></tfoot><dl id='iJvgY'><fieldset id='iJvgY'></fieldset></dl></div>

                        <tbody id='iJvgY'></tbody>
                    1. <legend id='iJvgY'><style id='iJvgY'><dir id='iJvgY'><q id='iJvgY'></q></dir></style></legend>
                        <tfoot id='iJvgY'></tfoot>
                          <bdo id='iJvgY'></bdo><ul id='iJvgY'></ul>