2013-02-26 66 views
0

我有一个通过导入长CSV电子表格而定期更新的情节表。通常,大多数记录保持不变,但是它们都包含在CSV中。为了检测哪些行实际更新,我在更新CURRENT_TIMESTAMP列“mtime”时添加了TIMESTAMP默认的CURRENT_TIMESTAMP。即使没有值更改,mysql 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 ; 
+0

请提供表格创建查询 – 2013-02-26 19:52:08

+0

可能行正在更新,您没有注意到它,因为少数行NewValue是相同的OldValue – 2013-02-26 19:52:48

+0

@ Fathah Rehman P - 我为您添加了CREATE TABLE查询!谢谢。 – snucky 2013-02-26 20:01:40

回答

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。

相关问题