×

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 DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

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.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 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 Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

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.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 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 INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.