Alana Storm
Alana Storm

Reputation: 166076

Copying a Table in MySQL and including the Primary Key

I have an ancient code snippet I use for copying tables in MySQL.

CREATE TABLE new_table (select * from old_table);

This works great, with one exception. It doesn't copy the primary key or other table indexes.

Is there any way to copy a table in MySQL AND include the indexes/primary key??

Upvotes: 4

Views: 6471

Answers (2)

cdnicoll
cdnicoll

Reputation: 584

Theres one of two ways. To see how a table is built you can use

SHOW CREATE TABLE old_table

You could also (I think you'll have to test it), run this:

CREATE TABLE new_table LIKE old_table;
INSERT INTO new_table SELECT * FROM old_table;

Upvotes: 11

hsz
hsz

Reputation: 152226

You can use this

CREATE TABLE new_table LIKE old_table;

but it does not provide duplicating data.

Upvotes: 2

Related Questions