Different Methods of SQL Table Clone
There are three different methods to create a clone table in SQL:
- Simple Cloning
- Deep Cloning
- Shallow Cloning
Let us CREATE a table with the name STUDENT to apply all three methods of SQL cloning.
CREATE TABLE STUDENT(
student_id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
roll_no varchar(255) NOT NULL UNIQUE,
PRIMARY KEY (student_id)
) ;
Let us INSERT the data into our newly Created table “STUDENT”
INSERT INTO STUDENT(student_id, name, roll_no)
VALUES (1, 'Ritwik Dalmia', 'S100');
INSERT INTO STUDENT(student_id, name, roll_no)
VALUES (2, 'Rohan Singh', 'S200');
INSERT INTO STUDENT(student_id, name, roll_no)
VALUES (3, 'Mohan Singh', 'S300');
Let us fetch the data we have inserted into the “STUDENT” table
SELECT * from STUDENT;
The above MySQL code is used to create a table called “STUDENT” which has three columns student_id, name, and roll_no where student_id is defined as PRIMARY KEY with AUTO_INCREMENT command and roll_no is defined as UNIQUE KEY.
After creating the table we used INSERT OPERATION to insert the three entries in the “STUDENT” Table. Finally, we have used SELECT OPERATION to fetch the data to see the output.
Output:
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.