Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Export data, and ignore primary key. Then import using auto increment

I have a table with primary key in auto-increment column "id" and a column "data"

Contents of table:

id | data
1  | aaaaa
2  | bbbbb
3  | whatever :P
5  | dang
99 | hello

As you can see there are 5 rows. the id's are 1,2,3,5,99 (there are NO id = 4 or id = 6 to 98).

How can i export this table, and then import it in another table (same structure of course) and the id's will get the values 1,2,3,4,5 instead of 1,2,3,5,99.

(or import them in a table that has already data, and the imported ids will take whatever values are from that table's auto_increment value and go on?)

Note: If there is a solution specific for PhpMyAdmin its welcome.

-Sorry for bad english! (Ops Feel free to edit)

like image 520
Sharky Avatar asked Dec 12 '22 04:12

Sharky


2 Answers

Create a table with the same layout. Then:

INSERT INTO new_table (data) SELECT data FROM old_table;

This will copy only the data fields into the new table. Since the id column is auto_increment, it will add ids counting from one upwards.

like image 84
Mantriur Avatar answered Feb 02 '23 01:02

Mantriur


INSERT INTO newTable (data) SELECT data FROM oldTable;
like image 21
Travesty3 Avatar answered Feb 02 '23 00:02

Travesty3