0

我有一张通过导入长 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 ;
4

1 回答 1

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 时间戳。

于 2017-09-01T05:56:36.357 回答