mysqldump + auto_increment导致主键错误

时间:2012-01-05 03:42:41

标签: mysql primary-key mysqldump auto-increment duplicates

我使用mysqldump来(a)转储表,然后(b)将其插入其他地方。当我尝试做()部分时出现问题。这是我得到的错误。

键'PRIMARY'重复输入'1'

现在,坚持,坚持下去。我知道我没有插入重复的主键....我检查了CREATE TABLE语句。 id字段的auto_incremement保留为id列的最大值:AUTO_INCREMENT = 45634650。我相信问题是,如果你试图插入id值小于auto_increment值指定的任何行,那么MySQL会给出这个错误....无论该id是否重复。

我尝试从TABLE CREATE语句中手动删除AUTO_INCREMENT。同样的错误。

更新:以下是Create table语句的一些相关部分。离开插件,即使其中一个是巨大的:

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
/*Table structure for table `WC_ip` */

DROP TABLE IF EXISTS `WC_ip`;

CREATE TABLE `WC_ip` (
  `id` int(11) NOT NULL AUTO_INCREMENT,

etc etc .. =)

  `activity_date` date DEFAULT NULL,
  UNIQUE KEY `userid_game_installAge` (`userid`,`game`,`install_age`),
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=45750081 DEFAULT CHARSET=latin1;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

INSERT INTO `bi_user_daily` VALUES (1,1512489,'WC','1','GR','100001229652633','asfdasdf','http://www.facebook.com/asfdasdfasdffgdf','notif','gghgf','Secondary Paid Direct','2012-01-02',0,0,0.00,0,0,0,0,194,0,0,1325491229,'Google Chrome','16.0.912.63','windows','GR','178.146.208.153',0,1325491253,24,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'incomplete',91,2,1914,2,1,3.23,0,NULL,NULL,NULL,NULL,0,'2012-01-03','2012-01-03 07:06:57','2012-01-02'),

(2,1512490,'WC','1','TW','453','asdf','http://www.facebook.com/profile.php?id=blachdfhjdfkh','dfdsfg','','Primary Paid','2012-01-02',0,0,0.00,0,0,0,0,394,0,0,1325491233,'Google Chrome','16.0.912.63','windows','TW','114.41.159.234',0,1325491258,25,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'complete',120,2,2302,2,1,6.57,0,NULL,NULL,NULL,NULL,0,'2012-01-03','2012-01-03 07:06:57','2012-01-02'),

(3,1512491,'WC','1','','1590571243','','','notif','asfd','Secondary Paid Direct','2012-01-02',0,0,0.00,0,0,0,0,0,0,0,34535,'Mozilla Firefox','9.0.1','windows','-','10.54.48.31',1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,1,0.00,0,NULL,NULL,NULL,NULL,0,'2012-01-03','2012-01-03 07:06:57','2012-01-02')

CREATE TABLE `bi_user_daily` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userid` bigint(20) DEFAULT NULL,
  `game` varchar(45) DEFAULT NULL,
  `main.install_today` varchar(45) DEFAULT NULL,
  `main.country` text,
  `main.fbid` varchar(50) DEFAULT NULL,
  `main.photo` text,
  `main.main.url` text,
  `main.fromstr` varchar(50) DEFAULT NULL,
  `main.sourcestr` varchar(50) DEFAULT NULL,
  `main.user_type` varchar(50) DEFAULT NULL,
  `main.install_date_pst` date DEFAULT NULL,
  `main.install_hour_pst` int(11) DEFAULT NULL,
  `main.revenue_primary` int(11) DEFAULT NULL,
  `main.revenue_secondary` decimal(10,2) DEFAULT NULL,
  `main.trans_primary` int(11) DEFAULT NULL,
  `main.trans_secondary` int(11) DEFAULT NULL,
  `main.direct_installs_generated` int(11) DEFAULT NULL,
  `main.indirect_installs_generated` int(11) DEFAULT NULL,
  `main.timeplayed_total` int(11) DEFAULT NULL,
  `main.gift_sent_total` int(11) DEFAULT NULL,
  `main.monetized` int(11) DEFAULT NULL,
  `install.usercreate_time` int(11) DEFAULT NULL,
  `install.browser` varchar(75) DEFAULT NULL,
  `install.browser_version` varchar(50) DEFAULT NULL,
  `install.os` varchar(50) DEFAULT NULL,
  `install.ip_address_country` varchar(50) DEFAULT NULL,
  `install.ip_address` varchar(50) DEFAULT NULL,
  `install.noload` int(11) DEFAULT NULL,
  `install.f.reate_time` int(11) DEFAULT NULL,
  `install.load_duration` int(11) DEFAULT NULL,
  `l.f.time_saved` int(11) DEFAULT NULL,
  `l.f.playerLevel` int(11) DEFAULT NULL,
  `l.f.daysSinceInstall` int(11) DEFAULT NULL,
  `l.f.resource1` int(11) DEFAULT NULL,
  `l.f.resource2` int(11) DEFAULT NULL,
  `l.f.resource3` int(11) DEFAULT NULL,
  `l.f.resource4` int(11) DEFAULT NULL,
  `l.f.f.epairTime` int(11) DEFAULT NULL,
  `l.f.dockRepairTime` int(11) DEFAULT NULL,
  `l.f.fleetRepairTime` int(11) DEFAULT NULL,
  `l.f.maxResearchTime` int(11) DEFAULT NULL,
  `l.f.shipBuildTime` int(11) DEFAULT NULL,
  `l.f.buildingUpgradeTime` int(11) DEFAULT NULL,
  `l.f.numf.Uncovered` int(11) DEFAULT NULL,
  `l.f.attackablef.Uncovered` int(11) DEFAULT NULL,
  `l.f.numTerrainsUncovered` int(11) DEFAULT NULL,
  `l.f.numFleets` int(11) DEFAULT NULL,
  `l.f.battlesWon` int(11) DEFAULT NULL,
  `l.f.battlesTotal` int(11) DEFAULT NULL,
  `l.f.numf.ttacks` int(11) DEFAULT NULL,
  `l.f.numf.efends` int(11) DEFAULT NULL,
  `l.f.totalResourcesCombat` int(11) DEFAULT NULL,
  `l.f.totalResourcesProduced` int(11) DEFAULT NULL,
  `l.f.bubbleTimeLeft` int(11) DEFAULT NULL,
  `l.f.FBCSpend` int(11) DEFAULT NULL,
  `l.f.numFriends` int(11) DEFAULT NULL,
  `l.f.numInstallsGenerated` int(11) DEFAULT NULL,
  `l.f.numGiftsRecd` int(11) DEFAULT NULL,
  `l.f.numChatMessagesSent` int(11) DEFAULT NULL,
  `l.f.numMissionsCompleted` int(11) DEFAULT NULL,
  `l.f.numAdvancedResearched` int(11) DEFAULT NULL,
  `l.f.numWeaponsResearched` int(11) DEFAULT NULL,
  `l.f.numArmorsResearched` int(11) DEFAULT NULL,
  `l.f.numHullsResearched` int(11) DEFAULT NULL,
  `l.summary.tutorial_status` varchar(50) DEFAULT NULL,
  `l.summary.tutorial_stage` int(11) DEFAULT NULL,
  `l.summary.user_level` int(11) DEFAULT NULL,
  `l.summary.f.points` int(11) DEFAULT NULL,
  `l.summary.f.loads` int(11) DEFAULT NULL,
  `key_metrics.logged_in` int(11) DEFAULT NULL,
  `key_metrics.timeplayed` decimal(10,2) DEFAULT NULL,
  `key_metrics.revenue` int(11) DEFAULT NULL,
  `virality.secondary_direct_install` int(11) DEFAULT NULL,
  `virality.secondary_indirect_install` int(11) DEFAULT NULL,
  `monetization.secondary_direct_revenue` decimal(10,2) DEFAULT NULL,
  `monetization.secondary_indirect_revenue` decimal(10,2) DEFAULT NULL,
  `install_age` int(11) DEFAULT NULL,
  `record_created_date` date DEFAULT NULL,
  `record_created_datetime` datetime DEFAULT NULL,
  `activity_date` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `userid_game_installAge` (`userid`,`game`,`install_age`)
) ENGINE=MyISAM AUTO_INCREMENT=45634650 DEFAULT CHARSET=latin1;

3 个答案:

答案 0 :(得分:4)

几个月前我遇到了同样的问题(在blog上发布了解决方案)。有一个known issue与MySQL 5.1.11+,如果你做一个mysqldump,它也输出auto_increment计数(如你在帖子中所述)。您可以使用sed(流编辑)并在生成转储文件时解析auto_increment构造:

mysqldump -u username -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*\b//' > <filename>.sql

答案 1 :(得分:0)

auto_increment值是设置为NULL还是特定数字(如果它们为NULL),它们将使用表定义指定的下一个值。如果它们被明确表达,那么除非存在重复,否则它们应作为该值插入?

答案 2 :(得分:0)

我希望这可以帮到你

ALTER TABLE table2 AUTO_INCREMENT = 1;

或删除table2上的主键并找到相同的记录。