How to clone a table structure in MySQL

0 min read 123 words

I have often needed to clone a table without it’s data in MySQL.

Oftentimes this has been because I have a continually growing temporary table and I just need to clear up some disk space. And it’s a lot faster and more guaranteed than trying to delete and optimise the table.

Clone a table structure without it’s data

Let’s say that your table in question is called table1:

create table table1tmp like table1;
drop table table1;
rename table table1tmp to table1;

If you run the above queries either in quick succession, or altogether in a single statement, you will notice that this is a very fast method even on very big tables.

Another article exists to clone and drop tables over here .

Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts