×

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

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

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.

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.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

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

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.