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:
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:
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.