即使没有值更改,mysql current_timestamp列也会更新

时间:2013-02-26 19:49:54

标签: mysql timestamp sql-update

我有一个剧集表,通过导入长CSV电子表格定期更新。通常,大多数记录保持不变,但无论如何它们都包含在CSV中。为了检测哪些行实际更新,我在更新CURRENT_TIMESTAMP列“mtime”上添加了TIMESTAMP默认CURRENT_TIMESTAMP。

不幸的是,每次都为每个导入的行重置每一行的时间戳值,即使它们的值都没有实际更改。

实际表的列数多于CSV文件中包含的列数,因此在UPDATE查询中。下面是表格布局和通过导入CSV文件触发的示例UPDATE查询。任何人都可以识别可能触发TIMESTAMP列重置的内容,即使这些值没有不同吗?非常感谢!

+---------------+-----------------------+------+-----+---------------------+-----------------------------+
| Field         | Type                  | Null | Key | Default             | Extra                       |
+---------------+-----------------------+------+-----+---------------------+-----------------------------+
| id            | mediumint(7) unsigned | NO   | PRI | NULL                | auto_increment              |
| ctime         | timestamp             | NO   |     | 0000-00-00 00:00:00 |                             |
| mtime         | timestamp             | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| network_id    | mediumint(7) unsigned | NO   | MUL | 0                   |                             |
| series_id     | mediumint(6) unsigned | NO   | MUL | NULL                |                             |
| season_id     | mediumint(7) unsigned | NO   | MUL | NULL                |                             |
| num           | mediumint(7) unsigned | NO   |     | NULL                |                             |
| title         | varchar(150)          | NO   |     | NULL                |                             |
| isactive      | tinyint(1)            | NO   | MUL | 1                   |                             |
| istve         | tinyint(1)            | NO   |     | 0                   |                             |
| isest         | tinyint(1)            | NO   |     | 0                   |                             |
| ismob         | tinyint(1)            | NO   |     | 0                   |                             |
| isvod         | tinyint(1)            | NO   |     | 0                   |                             |
| tve_start     | date                  | NO   |     | 0000-00-00          |                             |
| est_start     | date                  | NO   |     | 0000-00-00          |                             |
| mob_start     | date                  | NO   |     | 0000-00-00          |                             |
| vod_start     | date                  | NO   |     | 0000-00-00          |                             |
| tve_end       | date                  | NO   |     | 0000-00-00          |                             |
| est_end       | date                  | NO   |     | 0000-00-00          |                             |
| mob_end       | date                  | NO   |     | 0000-00-00          |                             |
| vod_end       | date                  | NO   |     | 0000-00-00          |                             |
| fposter       | varchar(150)          | NO   |     |                     |                             |
| iswebisode    | tinyint(1)            | NO   |     | 0                   |                             |
| date_air      | date                  | NO   |     | 0000-00-00          |                             |
| roll_start    | date                  | NO   |     | 0000-00-00          |                             |
| roll_end      | date                  | NO   |     | 0000-00-00          |                             |
| season_start  | date                  | NO   |     | 0000-00-00          |                             |
| season_end    | date                  | NO   |     | 0000-00-00          |                             |
| premier_start | date                  | NO   |     | 0000-00-00          |                             |
| premier_end   | date                  | NO   |     | 0000-00-00          |                             |
| iscore        | tinyint(1)            | NO   |     | 1                   |                             |
| note          | varchar(150)          | NO   |     |                     |                             |
| tvn_filename  | varchar(150)          | NO   |     |                     |                             |
| tve_filename  | varchar(150)          | NO   |     |                     |                             |
| repeat_start  | date                  | NO   |     | 0000-00-00          |                             |
| repeat_end    | date                  | NO   |     | 0000-00-00          |                             |
| hasedited     | tinyint(1)            | NO   |     | 1                   |                             |
| import_key    | char(32)              | NO   |     |                     |                             |
+---------------+-----------------------+------+-----+---------------------+-----------------------------+


UPDATE `episode` SET  `network_id`='25', `series_id`='321', `season_id`='6887', `num`='10', `title`='EPISODE 810', `isvod`='1', `vod_start`='2011-09-05', `vod_end`='2011-10-31', `ismob`='1', `mob_start`='2011-09-05', `isest`='1', `est_start`='2011-09-05', `note`='', `date_air`='2011-09-04', `roll_start`='0000-00-00', `roll_end`='0000-00-00', `premier_start`='2011-09-04', `premier_end`='2011-10-09', `tvn_filename`='', `tve_filename`=''   WHERE `id`='2821'



CREATE TABLE IF NOT EXISTS `episode` (
  `id` mediumint(7) unsigned NOT NULL AUTO_INCREMENT,
  `ctime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `network_id` mediumint(7) unsigned NOT NULL DEFAULT '0',
  `series_id` mediumint(6) unsigned NOT NULL,
  `season_id` mediumint(7) unsigned NOT NULL,
  `num` mediumint(7) unsigned NOT NULL,
  `title` varchar(150) COLLATE latin1_general_ci NOT NULL,
  `isactive` tinyint(1) NOT NULL DEFAULT '1',
  `istve` tinyint(1) NOT NULL DEFAULT '0',
  `isest` tinyint(1) NOT NULL DEFAULT '0',
  `ismob` tinyint(1) NOT NULL DEFAULT '0',
  `isvod` tinyint(1) NOT NULL DEFAULT '0',
  `tve_start` date NOT NULL DEFAULT '0000-00-00',
  `est_start` date NOT NULL DEFAULT '0000-00-00',
  `mob_start` date NOT NULL DEFAULT '0000-00-00',
  `vod_start` date NOT NULL DEFAULT '0000-00-00',
  `tve_end` date NOT NULL DEFAULT '0000-00-00',
  `est_end` date NOT NULL DEFAULT '0000-00-00',
  `mob_end` date NOT NULL DEFAULT '0000-00-00',
  `vod_end` date NOT NULL DEFAULT '0000-00-00',
  `fposter` varchar(150) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `iswebisode` tinyint(1) NOT NULL DEFAULT '0',
  `date_air` date NOT NULL DEFAULT '0000-00-00',
  `roll_start` date NOT NULL DEFAULT '0000-00-00',
  `roll_end` date NOT NULL DEFAULT '0000-00-00',
  `season_start` date NOT NULL DEFAULT '0000-00-00',
  `season_end` date NOT NULL DEFAULT '0000-00-00',
  `premier_start` date NOT NULL DEFAULT '0000-00-00',
  `premier_end` date NOT NULL DEFAULT '0000-00-00',
  `iscore` tinyint(1) NOT NULL DEFAULT '1',
  `note` varchar(150) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `tvn_filename` varchar(150) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `tve_filename` varchar(150) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  `repeat_start` date NOT NULL DEFAULT '0000-00-00',
  `repeat_end` date NOT NULL DEFAULT '0000-00-00',
  `hasedited` tinyint(1) NOT NULL DEFAULT '1',
  `import_key` char(32) COLLATE latin1_general_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `season_id` (`season_id`),
  KEY `idx_series` (`isactive`,`season_id`),
  KEY `idx_master_cron` (`isactive`,`hasedited`),
  KEY `network_id` (`network_id`),
  KEY `series_id` (`series_id`),
  KEY `season_id_2` (`season_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci ;

1 个答案:

答案 0 :(得分:0)

这是预期的行为。 MySQL文档中没有任何内容表明只有在提供新值时才会发生更新,从而触发ON UPDATE处理程序。

您没有说明您的导入方法是什么,因此这可能不适用。您确实显示了工具生成的更新语句。如果您对它有任何控制权,或者它可以选择自己编写:通过在where子句中添加更多检查来进行可选更新:

UPDATE `episode` SET  `network_id`='25', `series_id`='321', ... 
 WHERE `id`='2821' AND `network_id`<>'25' AND `series_id`<>'321' ...

这样,如果值相同,则WHERE子句不会与现有行匹配,不会发生更新,也不会触发时间戳ON UPDATE。