views:

50

answers:

4

I have this table

CREATE TABLE `zipcodes2` (
  `ID` int(11) NOT NULL auto_increment,
  `zipcode` int(6) NOT NULL default '0',
  `State` char(3) NOT NULL default '',
  `zip_name` varchar(255) NOT NULL default '',
  `CityAliasName` varchar(255) NOT NULL default '',
  `latitude` float(26,7) NOT NULL default '0.0000000',
  `longitude` float(36,7) NOT NULL default '0.0000000',
  `AreaCode` smallint(3) NOT NULL default '0',
  `County` varchar(255) NOT NULL default '',
  `TimeZone` smallint(3) NOT NULL default '0',
  `DayLightSavings` char(2) NOT NULL default '',
  PRIMARY KEY  (`zipcode`,`CityAliasName`,`ID`),
  KEY `CityAliasName` (`CityAliasName`),
  KEY `State` (`State`),
  KEY `index_by_zipcode` (`zipcode`),
  KEY `index_by_state` (`State`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

And I'm trying to insert into this table from another table. The problem is the auto_increment field is not incrementing. What am I doing wrong?

insert into zipcodes2 (ID,zipcode,state,zip_name,CityAliasName,latitude,longitude, AreaCode, County, TimeZone, DayLightSavings)
select distinct null,ZipCode as zipcode,State as state,City as zip_name, CityAliasName, Latitude as latitude, Longitude as longitude, AreaCode
, County, TimeZone, DayLightSavings
from zip_codes_all  
where state NOT in ('PR', 'AE', 'PW', 'MP', 'GU', 'FM', 'AS','AP', 'AA','FM','GA','GU','FM')
limit 0,10;
A: 

Try removing the 'ID' from the query:

insert into zipcodes2 (zipcode,state,zip_name,CityAliasName,latitude,longitude, AreaCode, County, TimeZone, DayLightSavings)
select distinct ZipCode as zipcode,State as state,City as zip_name, CityAliasName, Latitude as latitude, Longitude as longitude, AreaCode, County, TimeZone, DayLightSavings
from zip_codes_all  
where state NOT in ('PR', 'AE', 'PW', 'MP', 'GU', 'FM', 'AS','AP', 'AA','FM','GA','GU','FM')
limit 0,10;
Jhong
A: 

Try remove the ID.

Cooze
A: 

The problem was,

PRIMARY KEY (zipcode,CityAliasName,ID)

It should have been,

PRIMARY KEY (ID)

Once I changed that, AND removed the ID field from the insert, I got the results I was expecting.

isuelt
A: 

Yes, primary key definition is a key. As mysql docs says:

For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

Probably your id got non-unique values before fixing primary key.