How To Clone Tables in SQL. Learn how to create a clone of your... | by Oyetoke Tobi Emmanuel | Towards Data Science

In database operations, there may be a time when you need to clone or duplicate an existing table to a new table due to their similarities in columns and attributes either for performing a test without affecting the original table or other personal reasons.
I bumped in this situation whereby I need to create a new set of tables for a new feature we are integrating. This table has quite a lot of columns and very similar to an existing table handling another set of similar data of a particular feature.
Since this existing table and the new one are quite similar, my quick solution to this is to clone the existing table to create the new table.
In SQL it's quite easy to do this as you can easily run a couple of commands to best suit your cloning needs.
In this article, I will be showing you how to duplicate and clone existing tables in SQL.
The first method is called Simple Cloning and as its name implies it create a table from another table without taking into account any column attributes and indexes.
CREATE TABLE new_table SELECT * FROM original_table;
So if I have a table called users, I can easily create another table called adminUsers without caring about the users table column attributes and indexes.
The below SQL command creates a simple copy of the users table.
CREATE TABLE adminUsers SELECT * FROM users;
Use this in order to quickly clone any table that only includes the structure and data of the original table.
Shallow cloning is mostly used to create a copy of an existing table data structure and column attributes without the data being copied. This will only create an empty table base on the structure of the original table.
CREATE TABLE new_table LIKE original_table;
The following command would create an empty table base on the original table.
CREATE TABLE adminUsers LIKE users;
Use this if you only want the data structure and column attributes of the original table
Deep cloning is quite different from Simple Cloning and similar to Shallow cloning but with data, and as its name implies it creates a deep copy of the original table.
This means the new table gets to have all the attributes of each column and indexes of the existing table. This quite useful if you want to maintain the indexes and attributes of the existing table.
To achieve this, we’ll have to create an empty table base on the structure and attributes of the original table, then select data from the original table and insert into the new table.
CREATE TABLE new_table LIKE original_table;INSERT INTO new_table SELECT * FROM original_table;
To easily clone our original and also have its data copied:
CREATE TABLE adminUsers LIKE users;INSERT INTO adminUsers SELECT * FROM adminUsers;
Another cool thing now is, let's say you don't want all the data from the existing table and only want some data, based on some conditions then fine-tuning your SELECT queries are your best bet.
For instance, we have users with userType="admin" in our users table and we want to only copy those users only to our new table, you can easily do that like below:
INSERT INTO adminUsers SELECT * FROM adminUsers where userType="admin";
Cool right? I know.
If you are new to SQL below are some useful resources: