March 30, 2010

How to create Mysql table with an existing table's structure ?

Copying a Mysql table structure or replicating a table is very simple. A combination of "CREATE" and "SELECT" is used to do that .

Syntax:
create table newtable ( select * from existingtable)


Example -1 : Creating an exact copy if an existing table including data.
mysql> create table newaddressbook ( select * from oldaddressbook);


Example -2 : Creating a new table with same table structure of an existing table (columns) but without data.
mysql> create table newaddressbook ( select * from oldaddressbook limit 0);

No comments: