×

SQL Cloning Tables

Cloning a Table:

To create a copy of the table. To perform the operations, without affecting the actual table.

Steps for creating a Cloning Table:

Step 1: Empty Table Creation

The syntax for Creating an empty table:

Create table new_table_name Like Old_table;

Example

Code:

Create table Student1 Like Student;

Output:

Table Created

Step 2: Inserting the data into a table

Insert into new_table_name select * from old_table

Example

Code:

Create table Student1 Like Student;

Output:

Table Created

2. Inserting the values of data into a new table i.e, Student1

Code:

Insert into Student1 select * from Student;

Output:

Rows are inserted

Display the records of the new table i.e, Student1

Code:

Select * from Student1;

Output:

Student1 Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Display the id’s of all students which are copied into Student1 Table

Code:

Select sid from Student1;

Output:

Student1 Table

sid
1
2
3
4
5
6
7
8
9
10

Display the names  of all students which are copied into Student1 Table

Code:

Select sname from Student1;

Output:

Student1 Table

sname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshita
Bindu
Nandhini
Hashish
Rahul

Display the age  of all students which are copied into Student1 Table

Code:

Select sage from Student1;

Output:

Student1 Table

sage
22
24
21
21
23
22
26
23
22
21

Display the Phonenumber  of all students which are copied into Student1 Table

Code:

Select Phonenumber from Student1;

Output:

Student1 Table

Phonenumber
9895678909
6687654634
9867546453
7675643423
6567784532
9867546231
6563412768
6785674839
9453215052
9998989898

Display the id and names of all students which are copied into Student1 Table

Code:

Select sid, sname from Student1;

Output:

Student1 Table

sidsname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshita
7Bindu
8Nandhini
9Hashish
10Rahul

Delete the copied table i.e, Student1

Delete the row of the table whose id is 3

Code:

delete from Student1 where sid = 3;

Output:

Rows deleted

Display the Student1 table

Code:

Select * from Student1;

Output:

Student1

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Delete the total table Student1;

Code:

delete from Student1

Output:

Rows deleted

Display the Student1 table

Code:

Select * from Student1;

Output:

No rows selected

Example 2:

Create or copy of Student table i.e, Student2

Code:

Create table Student2 Like Student;

Output:

Table Created

2. Inserting the values of sname data into a new table i.e, Student2

Code:

Insert into Student2 select sname from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select sname from Student2;

Output:

Student2 Table

sname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshita
Bindu
Nandhini
Hashish
Rahul

Delete all rows of the Student2 table to insert the id of the Student table

Code:

delete from Student2

Output:

Rows deleted

Display the Student1 table

Code:

Select * from Student2;

Output:

No rows selected

2. Inserting the values of sid data into a new table i.e, Student2

Code:

Insert into Student2 select sid from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select sid from Student2;

Output:

Student2 Table

sage
22
24
21
21
23
22
26
23
22
21

Delete all rows of the Student2 table to insert the sgender of the Student table

Code:

delete from Student2

Output:

Rows deleted

Display the Student2 table

Code:

Select * from Student2;

Output:

No rows selected

2. Inserting the values of sgender data into a new table i.e, Student2

Code:

Insert into Student2 select sgender from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select sid from Student2;

Output:

Student2 Table

sgender
Male
Female
Male
Male
Female
Female
Female
Female
Male
Male

Delete all rows of the Student2 table to insert the data of the Student table

Code:

delete from Student2

Output:

Rows deleted

Display the Student2 table

Code:

Select * from Student2;

Output:

No rows selected

2. Inserting the total data into new table i.e, Student2

Code:

Insert into Student2 select * from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select * from Student2;

Output:

Student2 Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Related Topics

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.