user1016403 user1016403 - 1 year ago 107
SQL Question

Create table in MySQL that matches another table?

I am using MySQL. I have a table called EMP, and now I need create one more table (EMP_TWO) with same skeleton, same columns, and same constraints.Please help me how to achieve it.


Answer Source

To create a new table based on another tables structure / constraints use :

CREATE TABLE new_table LIKE old_table;     

To copy the data across, if required, use

INSERT INTO new_table SELECT * FROM old_table;  

Create table docs

Beware of the notes on the LIKE option :

Use LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:

CREATE TABLE new_table LIKE original_table; The copy is created using the same version of the table storage format as the original table. The SELECT privilege is required on the original table.

LIKE works only for base tables, not for views.

CREATE TABLE ... LIKE does not preserve any DATA DIRECTORY or INDEX DIRECTORY table options that were specified for the original table, or any foreign key definitions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download