I am using mysqldump to (a) dump a table, then (b) insert it elsewhere. A problem occurs when I try to do part (b). Here is the error I get.
Duplicate entry '1' for key 'PRIMARY'
Now, hold on, hold on. I know I'm not inserting duplicate primary keys .... I checked the CREATE TABLE statement. The auto_incremement on the id field is left at the greatest value for the id column: AUTO_INCREMENT=45634650. I believe that the problem is that MySQL will give this error if you try to insert any row with an id value less than what the auto_increment value is specified at .... regardless if that id is a duplicate or not.
I tried manually removing the AUTO_INCREMENT from the TABLE CREATE statement. Same error still.
UPDATE: Here are some relevant parts of the Create table statement. Leaving out the inserts, even one of them is huge:
/*!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;
I ran into this very same issue a few months ago (posted solution on my blog). There is a known issue with MySQL 5.1.11+ where if you do a mysqldump, it also outputs the auto_increment count (as you stated in your post). You can use sed (stream edit) and parse out the auto_increment construct when generating your dump file:
mysqldump -u username -p -h <db-host> --opt <db-name> -d --single-transaction | sed 's/ AUTO_INCREMENT=[0-9]*\b//' > <filename>.sql
Are the auto_increment values set as NULL or as a particular number if they are NULL they will use the next value as specified by the table definition. If they are explicitly expressed then they shall be inserted as that value unless a duplicate exists?
I hope this help you
ALTER TABLE table2 AUTO_INCREMENT = 1;
or remove primary key on table2 and find same records.
i have about 8mb of sql code i need to run. it looks like this:
/*
MySQL Data Transfer
Source Host: 10.0.0.5
Source Database: jnetdata
Target Host: 10.0.0.5
Target Database: jnetdata
Date: 5/26/2009 12:27:33 PM
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for chavrusas
-- ----------------------------
CREATE TABLE `chavrusas` (
`id` int(11) NOT NULL auto_increment,
`date_created` datetime default NULL,
`luser_id` int(11) default NULL,
`ruser_id` int(11) default NULL,
`luser_type` varchar(50) default NULL,
`ruser_type` varchar(50) default NULL,
`SessionDay` varchar(250) default NULL,
`SessionTime` datetime default NULL,
`WeeklyReminder` tinyint(1) NOT NULL default '0',
`reminder_phone` tinyint(1) NOT NULL default '0',
`calling_card` varchar(50) default NULL,
`active` tinyint(1) NOT NULL default '0',
`notes` mediumtext,
`ended` tinyint(1) NOT NULL default '0',
`end_date` datetime default NULL,
`initiated_by_student` tinyint(1) NOT NULL default '0',
`initiated_by_volunteer` tinyint(1) NOT NULL default '0',
`student_general_reason` varchar(50) default NULL,
`volunteer_general_reason` varchar(50) default NULL,
`student_reason` varchar(250) default NULL,
`volunteer_reason` varchar(250) default NULL,
`student_nli` tinyint(1) NOT NULL default '0',
`volunteer_nli` tinyint(1) NOT NULL default '0',
`jnet_initiated` tinyint(1) default '0',
`belongs_to` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5913 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for tbluseravailability
-- ----------------------------
CREATE TABLE `tbluseravailability` (
`availability_id` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`weekday_id` int(11) NOT NULL,
`timeslot_id` int(11) NOT NULL,
PRIMARY KEY (`availability_id`)
) ENGINE=MyISAM AUTO_INCREMENT=10865 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for tblusers
-- ----------------------------
CREATE TABLE `tblusers` (
`id` int(11) NOT NULL auto_increment,
`
etc
how do i run it on http://www.wampserver.com/en/download.php
see Executing SQL Statements from a Text File
Find the mysql.exe in your wamp installation, then run
mysql.exe -u username -p password database < textfile.sql
Presuming that WAMP means Windows, Apache, MySQL, Perl/Php, you would use the MySQL command line client to apply the SQL script.
The process is documented here: https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
If Wampserver installs MySQL in the normal place, the mysql client will be found somewhere such as C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql .
i have about 8mb of sql code i need to run. it looks like this:
/*
MySQL Data Transfer
Source Host: 10.0.0.5
Source Database: jnetdata
Target Host: 10.0.0.5
Target Database: jnetdata
Date: 5/26/2009 12:27:33 PM
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for chavrusas
-- ----------------------------
CREATE TABLE `chavrusas` (
`id` int(11) NOT NULL auto_increment,
`date_created` datetime default NULL,
`luser_id` int(11) default NULL,
`ruser_id` int(11) default NULL,
`luser_type` varchar(50) default NULL,
`ruser_type` varchar(50) default NULL,
`SessionDay` varchar(250) default NULL,
`SessionTime` datetime default NULL,
`WeeklyReminder` tinyint(1) NOT NULL default '0',
`reminder_phone` tinyint(1) NOT NULL default '0',
`calling_card` varchar(50) default NULL,
`active` tinyint(1) NOT NULL default '0',
`notes` mediumtext,
`ended` tinyint(1) NOT NULL default '0',
`end_date` datetime default NULL,
`initiated_by_student` tinyint(1) NOT NULL default '0',
`initiated_by_volunteer` tinyint(1) NOT NULL default '0',
`student_general_reason` varchar(50) default NULL,
`volunteer_general_reason` varchar(50) default NULL,
`student_reason` varchar(250) default NULL,
`volunteer_reason` varchar(250) default NULL,
`student_nli` tinyint(1) NOT NULL default '0',
`volunteer_nli` tinyint(1) NOT NULL default '0',
`jnet_initiated` tinyint(1) default '0',
`belongs_to` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5913 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for tbluseravailability
-- ----------------------------
CREATE TABLE `tbluseravailability` (
`availability_id` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`weekday_id` int(11) NOT NULL,
`timeslot_id` int(11) NOT NULL,
PRIMARY KEY (`availability_id`)
) ENGINE=MyISAM AUTO_INCREMENT=10865 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for tblusers
-- ----------------------------
CREATE TABLE `tblusers` (
`id` int(11) NOT NULL auto_increment,
`
etc
how do i run it on microsoft sql 2008?
You don't, you'd have to convert the code to use SQL Server's T-SQL syntax. You could use a conversion tool though, like this one.
Run a tool to automatically convert MySQL sentences to T-SQL,
Intensive use of Find&Replace can make the work too. As an example:
CREATE TABLE `chavrusas` (
`id` int(11) NOT NULL auto_increment,
`date_created` datetime default NULL,
`luser_id` int(11) default NULL,
`ruser_id` int(11) default NULL,
`luser_type` varchar(50) default NULL,
`ruser_type` varchar(50) default NULL,
`SessionDay` varchar(250) default NULL,
`SessionTime` datetime default NULL,
`WeeklyReminder` tinyint(1) NOT NULL default '0',
`reminder_phone` tinyint(1) NOT NULL default '0',
`calling_card` varchar(50) default NULL,
`active` tinyint(1) NOT NULL default '0',
`notes` mediumtext,
`ended` tinyint(1) NOT NULL default '0',
`end_date` datetime default NULL,
`initiated_by_student` tinyint(1) NOT NULL default '0',
`initiated_by_volunteer` tinyint(1) NOT NULL default '0',
`student_general_reason` varchar(50) default NULL,
`volunteer_general_reason` varchar(50) default NULL,
`student_reason` varchar(250) default NULL,
`volunteer_reason` varchar(250) default NULL,
`student_nli` tinyint(1) NOT NULL default '0',
`volunteer_nli` tinyint(1) NOT NULL default '0',
`jnet_initiated` tinyint(1) default '0',
`belongs_to` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5913 DEFAULT CHARSET=latin1;
Find: \s`
Replace with [
Find: `\s
Replace with: ]
Find: PRIMARY KEY (id)
Replace with: CONSTRAINT PK_[SOME IDENTIFIER] PRIMARY KEY ([$1])
Find: ENGINE=MyISAM AUTO_INCREMENT=5913 DEFAULT CHARSET=latin1;
Replace with: ;
a few more Find&Replace and you'll get this script, T-SQL compliant:
CREATE TABLE [chavrusas] (
[id] INT IDENTITY(1,1) NOT NULL ,
[date_created] datetime NULL,
[luser_id] INT NULL,
[ruser_id] INT NULL,
[luser_type] varchar(50) NULL,
[ruser_type] varchar(50) NULL,
[SessionDay] varchar(250) NULL,
[SessionTime] datetime NULL,
[WeeklyReminder] INT NOT NULL,
[reminder_phone] INT NOT NULL,
[calling_card] varchar(50) NULL,
[active] INT NOT NULL,
[notes] TEXT,
[ended] INT NOT NULL,
[end_date] datetime NULL,
[initiated_by_student] INT NOT NULL,
[initiated_by_volunteer] INT NOT NULL,
[student_general_reason] varchar(50) NULL,
[volunteer_general_reason] varchar(50) NULL,
[student_reason] varchar(250) NULL,
[volunteer_reason] varchar(250) NULL,
[student_nli] INT NOT NULL,
[vvolunteer_nli] INT NOT NULL,
[jnet_initiated] INT,
[belongs_to] varchar(50) NULL,
CONSTRAINT PK_chavrusas PRIMARY KEY ([id])
)
At a glance, the code snippet you provided looks acceptable for SQL Server. I would suggest using one of the many tools available for executing SQL against an existing SQL Server instance (SQL Server Management Studio, Query Analyzer, etc.). Paste the code into a new query window (or open up the associated file with the query) and parse it to see if you uncover any errors. Once you tweak the code to work with SQL Server 2008 (assuming that's necessary), you should just be able to execute it to create the tables and such.
EDIT: I tested your code against SQL Server 2005 and there were some problems that would be annoying like replacing ` with ', fixing table/column references surrounded by apostrophes, etc.. Automated tool would be your best approach given the prevelance of errors.