Monday, 23 April 2012

Create Duplicate Table in MySQL

MySQL provides many methods to create duplicate table with its data or without its data. The following is a simple example to create a duplicate table along-with data of the original table:
CREATE TABLE new_table_name SELECT * FROM old_table_name; 
An important point to note is that this query will not create column attributes and indexes as defined in the original table.

If you also want to have such attributes, this can be done with another easy way. Below queries will create a copy of the original table with all its constraints and attributes and also insert entire data from the original table to the new on:
CREATE TABLE new_table_name LIKE old_table_name;
INSERT INTO new_table_name SELECT * FROM old_table_name;
 

No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...