
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we create a table from an existing MySQL table in the database?
With the help of CTAS i.e. “Create Table AS Select” script we can 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 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.
- Related Articles
- Create MySQL query to create a table from an existing table?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- How can I see the CREATE TABLE statement of an existing MySQL table?
- How can I drop an existing column from a MySQL table?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can we get a list of columns in an existing MySQL table?
- How can we delete an existing MySQL table by using PHP script?
- How to generate a “create table” command based on an existing table in MySQL?
- How can we put comments in a column of existing MySQL table?
- How can we copy data with some condition/s from existing MySQL table?
- How can I change the name of an existing column from a MySQL table?
- How can we apply UNIQUE constraint to the field of an existing MySQL table?
- How can we add columns with default values to an existing MySQL table?

Advertisements