2013-03-08 41 views
2

我想用另一个表中的数据创建表,以便将id设置为某个auto_increment起始值。使用MySQL中的另一个表中的数据填充表时,启动auto_increment值时不起作用

这是我想从另一个表中的数据来填充表:

+-----------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+-----------+--------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| city  | varchar(255) | YES |  | NULL |    | 
| state  | varchar(255) | YES |  | NULL |    | 
| state_id | int(11)  | YES |  | NULL |    | 
| city_slug | varchar(255) | YES |  | NULL |    | 
+-----------+--------------+------+-----+---------+----------------+ 

执行此查询后:

alter table table_temp auto_increment = 40499; 

如果我做一个模型插入:

insert into table_temp (city, state, state_id, city_slug) values (1, 1, 1, 1); 

它做它的预期,也就是说,ID开始于值40499

select * from table_temp; 

+-------+------+-------+----------+-----------+ 
| id | city | state | state_id | city_slug | 
+-------+------+-------+----------+-----------+ 
| 40499 | 1 | 1  |  1 | 1   | 
+-------+------+-------+----------+-----------+ 

截断表,并再次执行对AUTO_INCREMENT的ALTER TABLE查询之后,我尝试填充同一个表与另一个表中的数据:

insert into table_temp (city, state, state_id, city_slug) select city, state, state_id, city_slug from final_location; 

然而,ID与价值的默认ID开始1:

select * from table_temp limit 10; 

+----+---------+---------+----------+-------------------+ 
| id | city | state | state_id | city_slug   | 
+----+---------+---------+----------+-------------------+ 
| 1 | Abatiá | Paraná |  242 | all-cidade-abatia | 
+----+---------+---------+----------+-------------------+ 

有关如何解决此问题的任何建议?

+0

您正在处理** MySQL **吗? – Luv 2013-03-08 08:23:48

+0

按预期工作。请参阅我的sqlfiddle:http://www.sqlfiddle.com/#!2/a59d2/1 – Lars 2013-03-08 08:28:48

回答

1

我刚刚发现什么是错的,截断表后它似乎松散引用auto_increment起始值。我不得不再次执行alter table查询以便再次正确设置auto_increment值:

alter table table_temp auto_increment = 40499; 
相关问题