How can we create a new MySQL table by selecting specific column/s from another existing table?

MySQLMySQLi Database

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.

Published on 19-Feb-2018 12:26:34