Create New MySQL Table by Selecting Specific Columns from Another Table



As we know that we can copy the data and structure from an existing table by CTAS script. If we want to select some specific column/s from another table then we need to mention them after SELECT. Consider the following example in which we have created a table named EMP_BACKUP1 by selecting a specific column ‘name’ from already existing table ‘Employee’ −

mysql> Create table EMP_BACKUP1 AS Select name from employee;
Query OK, 3 rows affected (0.25 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> Select * from EMP_BACKUP1;
+--------+
| name   |
+--------+
| Ram    |
| Gaurav |
| Mohan  |
+--------+
3 rows in set (0.00 sec)

We can observe that it copied only the data and structure of ‘name’ column from the ‘Employee’ table.

Updated on: 2020-01-29T05:16:32+05:30

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements