Copy Table in SQL
? How to Copy a Table in SQL
Copying a table means creating a new table with the same structure and optionally copying data from the original table.
1. Copy Table Structure Only
Creates a new table with the same columns but no data.
Syntax (common in many DBMS):
CREATE TABLE new_table ASSELECT * FROM original_table WHERE 1=0;WHERE 1=0ensures no rows are copied, just the structure.
2. Copy Table Structure and Data
Creates a new table with the same structure and all data from the original table.
CREATE TABLE new_table ASSELECT * FROM original_table;3. Copy Table in MySQL
Structure only:
CREATE TABLE new_table LIKE original_table;Structure + Data:
CREATE TABLE new_table LIKE original_table;INSERT INTO new_table SELECT * FROM original_table;4. Copy Table in SQL Server
Structure + Data:
SELECT * INTO new_table FROM original_table;This creates
new_tablewith the same structure and copies all data.
5. Copy Table in PostgreSQL
Structure + Data:
CREATE TABLE new_table AS TABLE original_table;Structure only:
CREATE TABLE new_table (LIKE original_table INCLUDING ALL);Notes
Indexes, constraints, and triggers are usually not copied with these methods — you may need to recreate them manually.
Always check your specific database's syntax and capabilities.
If you want, I can provide a specific example for your database system!