Thursday, 8 January 2015

MYSQL-To create a new table based on another tables structure / constraints



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;

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.

0 comments:

Post a Comment