Thursday 3 January 2019

Duplicating a MySQL table, indices, and data



How do I copy or clone or duplicate the data, structure,

and indices of a MySQL table to a new one?



This is what I've found so far.



This will copy the data and the structure,
but not the indices:



create table {new_table} select * from {old_table};



This will copy the structure and indices,
but not the data:



create table {new_table} like {old_table};

Answer



To copy with indexes and triggers do these 2 queries:



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



To copy just structure and data use this one:



CREATE TABLE tbl_new AS SELECT * FROM tbl_old;


I've asked this before:



Copy a MySQL table including indexes



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...