Introduction
Hola ninjas! Let us continue with our topic.
In this article, we will be learning how to Clone tables.
First, we will learn what a clone table is and what it can do. Then we will know how to clone a table and its different types. Subsequently, we will learn about the commands. Then we will learn about the syntax of Cone table commands accordingly and see how to use them in a real-time environment.
We will also see some suitable examples of commands, making it easier to use them.
Recommended topics, Coalesce in SQL and Tcl Commands in SQL
Clone Table
There may come a moment in database operations when you need to clone or replicate an existing table to a new table owing to column and attribute similarities, either to execute a test without impacting the original table or for other personal reasons.
Source: https://giphy.com/explore/clone
So, if we just encountered a circumstance in which we needed to develop a new set of tables for the new functionality we're implementing. This table contains a lot of columns and is highly comparable to another table that handles a similar collection of data for a particular feature.
Because the existing table and the new table are so similar, I decided to clone the old table to make the new table. It's simple to perform with SQL since you can execute several procedures to meet your cloning needs. This tutorial will teach you how to replicate and clone existing SQL tables.
Types of cloning :
- Simple cloning
- Shallow cloning
- Deep cloning
Simple Cloning
The first approach is called Simple Cloning, and it creates a table from another table without taking any column properties or indexes into consideration.
So, if I have a table called employee, I can simply build a table called employees without worrying about the field characteristics and indexes in the employee table.
Code:
create table employeesA select * from employee; |
Shallow cloning
Shallow cloning is often used to produce a replica of an existing table's data structure and column characteristics without copying the contents. This will only result in creating an empty table based on the structure of the original table.
Code:
create table employeesA like employee; employeeA: New table employee: Existing table |
Deep cloning
Deep cloning differs from Simple Cloning and is comparable to Shallow Cloning in that, as the name indicates, it generates a deep copy of the original table.
This indicates that the new table inherits all of the previous table's column and index characteristics. This is quite beneficial if you want to keep the existing table's indexes and characteristics.
To do this, we'll need to construct an empty table based on the original database's structure and characteristics, then pick data from the old table and put it into the new table.
Code:
create table employeeA like employee; Insert into employeeA select * from employee; |