What is the concept of CTAS (CREATE TABLE AS SELECTED) in MySQL?

MySQLMySQLi Database

CTAS i.e. “Create Table AS Select” script is used to create a table from an existing table. It copies the table structure as well as data from the existing table. Consider the following example in which we have created a table named EMP_BACKUP from an already existing table named ‘Employee’

mysql> Select * from Employee;

+------+--------+
| Id   | Name   |
+------+--------+
| 100  | Ram    |
| 200  | Gaurav |
| 300  | Mohan  |
+------+--------+

3 rows in set (0.00 sec)

The query above shows the data in table ’Employee’ and the query below will create the table named ‘EMP_BACKUP’ by copying the structure as well as data from the ‘Employee’ table.

mysql> Create table EMP_BACKUP AS SELECT * from EMPLOYEE;
Query OK, 3 rows affected (0.15 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> Select * from EMP_BACKUP;

+------+--------+
| Id   | Name   |
+------+--------+
| 100  | Ram    |
| 200  | Gaurav |
| 300  | Mohan  |
+------+--------+

3 rows in set (0.00 sec)

We can observe that it copied all the data and structure of the ‘Employee’ table.

raja
Published on 19-Feb-2018 10:32:00
Advertisements