Shallow Cloning

Shallow cloning is the method in which the clone table gets the same structure as the original table but it does not inherits or copy the data from the original table.

In other words, we will have the empty table including indices such as primary key, unique key, and auto_increment.

Syntax:

CREATE TABLE clone_table  LIKE original_table;

let’s, see the properties of the shallow clone table “STUDENT_SHALLOW_CLONE” using describe command.

Output:

Shallow clone of student table (Original Table)

SELECT * FROM STUDENT_SHALLOW_CLONE;

Let us INSERT the data into our newly Created table “STUDENT_SHALLOW_CLONE”

INSERT INTO STUDENT_SHALLOW_CLONE(name, roll_no)
VALUES ('Ritwik Dalmia', 'S100');
INSERT INTO STUDENT_SHALLOW_CLONE(name, roll_no)
VALUES ( 'Rohan Singh', 'S200');
INSERT INTO STUDENT_SHALLOW_CLONE( name, roll_no)
VALUES ( 'Mohan Singh', 'S300');

Output:

working of the shallow clone method

Explanation: You can able to see that all the properties such as indices and auto_increment command are inherited in this method as compare to simple cloning method.

SQL Cloning or Copying a Table

Cloning or copying a table in SQL is a common task encountered in database management. Whether you’re creating backups, performing testing, or need to duplicate a table structure for various purposes, knowing how to effectively clone or copy a table is essential. In this article, we’ll explore different methods and good practices for achieving this in SQL.

Cloning tables is an operation in SQL that allows us to make a copy of an existing table. The clone table can be just the structure of the original table without any data or an exact copy of the original table.

Note: This Article will be following the MySQL Syntax but cloning operations can be done in other Relational Database Management systems (RDBMS) such as Postgre SQL, and Microsoft SQL Server, and syntax may follow as per their document.

Similar Reads

What is Cloning Table in SQL

SQL Cloning is an operation that means making a copy of a table. It’s like taking a photocopy of a document. This lets you create a new table that’s exactly like the original one (current table), including both the structure (like column names and types) and the data inside it. Or, We can just copy the structure without any data. The new table created this way is called a clone table. It’s a way to duplicate a table quickly and easily....

Real Life Scenario Example

Imagine you’re developing an app for a bookstore or a library management system where you have a database to store information about books. Now, let’s say you’re introducing a new feature or conducting tests on the existing book table. Moreover, you want to avoid any risk of changes or altering the original book table. In this scenario, you can create a clone table in SQL....

Why should we use the SQL Cloning

Cloning a table in SQL means making a duplicate copy of an existing table. It’s like making a backup so that you can experiment or work with the data without affecting the original table. This saves you the time and effort of creating a completely new table and re-entering all the same data....

Different Methods of SQL Table Clone

There are three different methods to create a clone table in SQL:...

1. Simple Cloning

In this method, the clone table only inherits the basic structure, default values, and NULL settings but it does not inherit the indices and AUTO_INCREMENT....

2. Shallow Cloning

Shallow cloning is the method in which the clone table gets the same structure as the original table but it does not inherits or copy the data from the original table....

3. Deep Cloning

This method is widely used for creating the clone tables in SQL as it inherits all the properties of original table including indices such as primary key, unique, and auto_increment as well as inherits the existing data from the original table....

Conclusion

Cloning is a useful method in SQL for creating a copy of an existing table. There are three main methods of cloning a table: simple cloning, shallow cloning, and deep cloning. Simple cloning only copies the basic structure of the table, while shallow cloning copies the structure without any data. Deep cloning, on the other hand, copies all properties of the original table, including indices like primary key, unique, and auto-increment, as well as any existing data. Each cloning method has its uses and benefits depending on the situation. Knowing the differences between these cloning methods can help you choose the appropriate method for your needs....