This is very simple in MySQL, to duplicate a table without the data simply perform the command: CREATE TABLE T_NEWTABLE LIKE T_TABLE;
To also duplicate the data along with the creation of the table perform the command:
CREATE TABLE T_NEWTABLE AS ( SELECT * FROM T_TABLE )
These examples can only be performed within the same database, to duplicate across databases simply prefix the the tables with their schema:
CREATE TABLE T_NEWTABLE LIKE [SCHEMA].T_TABLE;
The examples above do create a duplicate table with or without data, however the data directory and index directory options are not copied.
If you need an exact replica of the table then you need to perform two separate statements:
CREATE TABLE T_NEWTABLE LIKE T_TABLE; INSERT INTO T_NEWTABLE SELECT * FROM T_TABLE;
While duplicating it is also possible to just duplicate a number of columns:
CREATE TABLE T_NEWTABLE AS ( SELECT COL1, COL2 FROM T_TABLE )
And even change the names of columns:
CREATE TABLE T_NEWTABLE AS ( SELECT COL1, COL2 AS NEWCOL1, NEWCOL2 FROM T_TABLE )
Last of all is also to add columns directly upon creating a duplicate:
CREATE TABLE T_NEWTABLE ( ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY ) AS ( SELECT COL1, COL2 AS NEWCOL1, NEWCOL2 FROM T_TABLE ) |