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
sid | sname | sage | sgender | Phonenumber |
1 | Abhinav | 22 | Male | 9895678909 |
2 | Ramya | 24 | Female | 6687654634 |
3 | Preetham | 21 | Male | 9867546453 |
4 | Nethranand | 21 | Male | 7675643423 |
5 | Naveen | 23 | Male | 6567784532 |
6 | Harshita | 22 | Female | 9867546231 |
7 | Bindu | 26 | Female | 6563412768 |
8 | Nandhini | 23 | Female | 6785674839 |
9 | Hashish | 22 | Male | 9453215052 |
10 | Rahul | 21 | Male | 9998989898 |
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
sid | sname |
1 | Abhinav |
2 | Ramya |
3 | Preetham |
4 | Nethranand |
5 | Naveen |
6 | Harshita |
7 | Bindu |
8 | Nandhini |
9 | Hashish |
10 | Rahul |
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
sid | sname | sage | sgender | Phonenumber |
1 | Abhinav | 22 | Male | 9895678909 |
2 | Ramya | 24 | Female | 6687654634 |
4 | Nethranand | 21 | Male | 7675643423 |
5 | Naveen | 23 | Male | 6567784532 |
6 | Harshita | 22 | Female | 9867546231 |
7 | Bindu | 26 | Female | 6563412768 |
8 | Nandhini | 23 | Female | 6785674839 |
9 | Hashish | 22 | Male | 9453215052 |
10 | Rahul | 21 | Male | 9998989898 |
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
sid | sname | sage | sgender | Phonenumber |
1 | Abhinav | 22 | Male | 9895678909 |
2 | Ramya | 24 | Female | 6687654634 |
3 | Preetham | 21 | Male | 9867546453 |
4 | Nethranand | 21 | Male | 7675643423 |
5 | Naveen | 23 | Male | 6567784532 |
6 | Harshita | 22 | Female | 9867546231 |
7 | Bindu | 26 | Female | 6563412768 |
8 | Nandhini | 23 | Female | 6785674839 |
9 | Hashish | 22 | Male | 9453215052 |
10 | Rahul | 21 | Male | 9998989898 |