Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating table in MYSQL without copying one row at a time

I want to duplicate a very large table, but I do not want to copy it row by row. Is there a way to duplicate it?

For example, you can TRUNCATE w/o deleting row/row, so i was wondering if there is something similar for copying entire tables

UPDATE: row by row insert is very painful (because of 120M rows). Anyway to avoid that?

like image 872
meow Avatar asked May 19 '10 16:05

meow


2 Answers

MySQL no longer has a reliable "copy table" functionality - many reasons for this related to how data is stored. However, the below does row-by-row insertion but is pretty simple:

CREATE TABLE `new_table` LIKE `old_table`;
INSERT INTO `new_table` (SELECT * FROM `old_table`);
like image 145
AvatarKava Avatar answered Sep 24 '22 14:09

AvatarKava


You could use INSERT INTO ... SELECT.

like image 20
pinkgothic Avatar answered Sep 23 '22 14:09

pinkgothic