Summary -
In this topic, we described about the below sections -
Why to create a table from another table structure?
If we want to use the same table structure for different table, we can use the copy of the existing table. Most of the cases, the requirement to create a table with an existing table structure can happen when creating a backup table for the existing table.
How to create a table from another table structure?
We can create a new table as a copy of existing table by using CREATE TABLE AS Statement. New table gets same columns and structure of existing table.
We can create new table with all columns or specific columns of the existing table.
Syntax for creating with all columns -
CREATE TABLE new_table_name AS
SELECT *
FROM existing_table_name
WHERE condition;
Syntax for creating with specific columns-
CREATE TABLE new_table_name AS
SELECT column1, column2,.., columnN
FROM existing_table_name
WHERE condition;
- New_table_name - Specifies the new/target table name
- Existing_table_name - Specifies the source table name
- column1, column2, …, columnN - Specifies the column names
- Condition - Specifies condition to get the selected rows from the source.
Examples -
There are various ways to create new table using existing table the queries are as follows -
Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.
employee_details -
emp_id | ename | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 60000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Scenario - To get all columns from existing table to new table
We can create a new table which is exactly same as old table with all the columns and all the rows from it. The syntax for the same as follows -
Syntax -
CREATE TABLE new_table_name AS SELECT * FROM old_table_name;
In the below example, we are creating an employee_details_backup table using employee_details table with all columns and all the data from it.
CREATE TABLE employee_details_backup AS SELECT * FROM employee_details;
After successful execution of the above query, employee_details_backup table gets created. It has all the columns and rows from the source table employee_details. The new table looks like below -
employee_details_backup -
emp_id | ename | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 60000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
If we created a table like this, there will be no difference between them except table name.
Scenario - To get specified records from existing table
We can create a new table which is exactly same as old table with all the columns. We can also specify the condition to copy matched rows only. The syntax for the same as follows -
Syntax -
CREATE TABLE new_table_name AS SELECT * FROM old_table_name
WHERE condition;
In the below example, we are creating an employee_details_backup table using employee_details table with all columns and only rows that are having department 1000.
CREATE TABLE employee_details_backup AS SELECT * FROM employee_details
WHERE dept_id = 1000;
After successful execution of the above query, employee_details_backup table gets created. It has all the columns but only with department 1000 rows from the source table employee_details. The new table looks like below -
employee_details_backup -
emp_id | ename | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 60000.00 | 1000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
Scenario - To get the specific columns from existing table
We can create a new table which is exactly same as old table with the specified columns (not all). The syntax for the same as follows -
Syntax -
CREATE TABLE new_table_name AS SELECT column1, column2, …, columnN
FROM old_table_name;
In the below example, we are creating an employee_details_backup table using employee_details table with emp_id, emp_name, designation, and salary columns and all the rows from it.
CREATE TABLE employee_details_backup AS SELECT emp_id, emp_name,
designation,salary FROM employee_details;
After successful execution of the above query, employee_details_backup table gets created. It has only emp_id, emp_name, designation, salary columns but have the data for the specific rows from the source table employee_details. The new table looks like below -
employee_details_backup -
emp_id | ename | designation | salary |
---|---|---|---|
001 | Employee1 | Director | 60000.00 |
002 | Employee2 | Director | 40000.00 |
003 | Employee3 | Manager | 27000.00 |
004 | Employee4 | Manager | 25000.00 |
005 | Employee5 | Analyst | 20000.00 |
006 | Employee6 | Analyst | 18000.00 |
007 | Employee7 | Clerk | 15000.00 |
008 | Employee8 | Salesman | 14000.00 |
009 | Employee9 | Salesman | 13000.00 |
Scenario - To get only structure of the existing table
We can create a new table which is exactly same as old table with all columns but no data from the source table. i.e. we are copying only structure without data. The syntax for the same as follows -
Syntax -
CREATE TABLE new_table_name AS SELECT * FROM old_table_name WHERE 1=2;
In the below example, we are creating an employee_details_backup table using employee_details table with all columns but not the data from it. i.e. we are copying only structure without data.
CREATE TABLE employee_details_backup AS
SELECT * FROM employee_details WHERE 1=2;
After successful execution of the above query, employee_details_backup table gets created. It has all the columns from source table but not the data. The new table looks like below -
employee_details_backup –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|