86

I want to copy a table in MySQL. What is the fastest way? Like this?

CREATE TABLE copy LIKE original;
INSERT INTO copy SELECT * FROM original;

or

CREATE TABLE copy SELECT * FROM original;
ALTER TABLE copy ADD PRIMARY KEY (id);

or is there another way?

EDIT: I'm worried about the indexes being re-created, how does mysql proceed executing these statements?

PS. can't use command-line tools like mysqldump, must be on-the-fly.

0

10 Answers 10

50

This copies the structure of the table immediately, but not the data:

CREATE TABLE copy LIKE original;

This creates all the indexes the original table had.

It works this way in mysql 5.1.39.

2
  • 3
    But, it will not copy triggers.
    – ursitesion
    Aug 12, 2014 at 7:42
  • 3
    Note, this also will not copy foreign keys constraints.
    – SlimDeluxe
    Feb 14, 2017 at 9:46
50

The fastest way using MyISAM tables while preserving indexes) and maybe other storage engines is:

CREATE TABLE copy LIKE original;
ALTER TABLE copy DISABLE KEYS;
INSERT INTO copy SELECT * FROM original;
ALTER TABLE copy ENABLE KEYS;

You want to disable your keys for your database load and then recreate the keys at the end.

Similarly, for InnoDB:

SET unique_checks=0; SET foreign_key_checks=0; 
..insert sql code here..
SET unique_checks=1; SET foreign_key_checks=1;

(As pointed out in the comments.)

4
  • 1
    Strange. INSERT with keys took 36 min, and this method took 10 + 26 min (insert + alter). 6 indexes, 28 M records, Win 7 x64, 8GB RAM. Mar 26, 2015 at 12:42
  • An ENABLE KEYS cause a "repair table". Check your processlist in which way this happen. There are two kinds of: 1. repair table with keycache 2. repair table by sorting The second one if the fastes way and will be only use if myisam_max_sort_file_size big enough. dev.mysql.com/doc/refman/5.7/en/repair-table-speed.html
    – lopo
    Nov 13, 2015 at 7:27
  • 1
    ALTER TABLE table DISABLE/ENABLE KEYS doesn't work in InnoDB, and gives below warning Table storage engine for 'table' doesn't have this option. Sep 2, 2016 at 5:33
  • 2
    for innodb use SET unique_checks=0; SET foreign_key_checks=0; insert sql code here SET unique_checks=1; SET foreign_key_checks=1;
    – tuku
    Jan 17, 2017 at 13:05
14

From the manual:

"CREATE TABLE ... SELECT does not automatically create any indexes for you. This is done intentionally to make the statement as flexible as possible. If you want to have indexes in the created table, you should specify these before the SELECT statement: "

CREATE TABLE bar (UNIQUE (n)) SELECT n FROM foo;

You can specify indices and data types (to avoid datatype conversion) in with both CREATE TABLE LIKE and CREATE TABLE SELECT. Which one is faster will depend on your setup.

0
11

To copy with indexes and triggers do these 2 queries:

CREATE TABLE newtable LIKE oldtable; 
INSERT newtable SELECT * FROM oldtable;

To copy just structure and data use this one:

CREATE TABLE tbl_new AS SELECT * FROM tbl_old;
1
  • 3
    Shouldn't be INSERT INTO instead of INSERT?
    – giordano
    Mar 5, 2019 at 14:45
6

Does create table mynewtable (select * from myoldtable) work in mysql? If so you can try it too.

0
5

Best way to copy structure and all entries from one table to another table (by creating new table) is this query...

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

Try SELECT INTO, and use a variable as a go-between.

You'll have to create the receiving table, first, to have the same structure as the source table.

Best thing is, it's internal so it's fast. You'll lose your indexes, though.

3
  • Select into is not supported by mySQL
    – Draco Ater
    May 31, 2010 at 12:19
  • @Draco Ater - Yes, it does. It just uses variables. May 31, 2010 at 12:32
  • 2
    Can you provide an example? Cause I think, that the variables will just hold the last value that was read by the select, but not all values in the table. As long as you don't use a cursor and don't want to copy tables with more than one row this solution does not work at all.
    – fancyPants
    Jun 6, 2013 at 14:23
2

if you are using MyISAM you can also copying and renaming the induvidual files . .MYD, .MYI, .FRM files in the backend

1

Maybe you could take a look at SHOW CREATE TABLE.

Steps to take:

  • Go to phpmyadmin

  • Go to SQL

Execute this query

SHOW CREATE TABLE `the_old_table`;
  • Click options Check "Full texts" and press Go.

The result is a full CREATE TABLE statement. Edit the query until you are happy.

Resource: http://dev.mysql.com/doc/refman/5.7/en/show-create-table.html

0
CREATE TABLE copy SELECT * FROM original;

Is a fast way but maybe not the quickest cause of indexes.

2
  • Please edit your answer and format it properly to make it readable.
    – Neeku
    Jun 18, 2014 at 16:07
  • 1
    This statement neither copy indexes nor triggers associated with this table.
    – ursitesion
    Aug 12, 2014 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.