
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Create MySQL query to create a table from an existing table?
- How can I see the CREATE TABLE statement of an existing MySQL table?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- How can I drop an existing column from a 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 remove NOT NULL 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 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 put comments in a column of existing MySQL table?
- How can we copy data with some condition/s from existing MySQL table?
- How can we add columns with default values to an existing MySQL table?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
Advertisements