Copy a MySQL table by goal: use CREATE TABLE new LIKE old for structure only, CREATE TABLE new AS SELECT ... for a fast data snapshot, and INSERT INTO new SELECT ... after a matching table for data only. Bad copies can drop indexes, defaults, keys, or rows. This guide maps each goal to a safe SQL pattern and lists what each method keeps or loses.
What copy do you need?
Pick the copy by outcome, not syntax. Need an empty clone for testing? Use structure only. Need rows but no metadata? Use a data-only snapshot. Need both the table definition and the rows? Create the shell first, then load the data. For another database, qualify the target name.
| Copy goal | Copied objects | Missing objects | Best use case | Common failure risks |
|---|---|---|---|---|
| Structure only | Columns, table definition, indexes, AUTO_INCREMENT in full-copy guidance | Rows, triggers, foreign key data load | Empty test table, template for filtered loads | Wrong charset or collation, missing privileges on source or target |
| Data only | Rows from the query result | Indexes, AUTO_INCREMENT, triggers, foreign key constraints | Quick snapshot, reporting copy, ad hoc extract | Duplicate-key conflicts, bad type matches, generated column errors |
| Structure plus data | Definition from LIKE, then rows from INSERT INTO ... SELECT | Triggers still need manual recreation | Production-like replica, backup staging, safe test clone | Locks on large tables, permission gaps, charset mismatch |
| Cross-database copy | Same methods, with qualified database and table names | Anything the chosen method does not carry | Move data between schemas or databases on the same server | Name collision, wrong target schema, insufficient CREATE and INSERT rights |
What you'll need
- MySQL or MariaDB access with CREATE, INSERT, and SELECT permissions.
- A SQL client such as phpMyAdmin or MySQL Workbench.
- The source table name and the target table name.
- Enough disk space for a second copy of the table.
- Backup access before touching production data.
How do you copy only the structure?
Use CREATE TABLE ... LIKE when you need an empty table that matches the source table definition. It copies the structure, including column definitions and indexes, but it does not copy triggers. The current AUTO_INCREMENT counter value is not reliably preserved, so check it before you rely on the next inserted value. That makes it the safest starting point for filtered loads or test tables.
- Create the empty clone. Run
CREATE TABLE new_table LIKE old_table;Use the same database, or prefix the database name if the target lives elsewhere. - Check the definition. Run
SHOW CREATE TABLE new_table;Confirm the columns, indexes, and engine settings match what you expected before loading anything. - Add rows later if needed. Load only the subset you want with
INSERT INTO new_table SELECT ... FROM old_table WHERE ...;This keeps the empty structure intact until you choose the rows.
Suppose the source table has 12 columns and a secondary index on customer_id. LIKE creates the 12-column shell with the index in place, so a later filtered insert can use the same access pattern. That matters when you want a test table that behaves like production.
How do you copy only the data?
Use CREATE TABLE ... AS SELECT when you want a fast data-only snapshot. MySQL documentation says this method creates a new table from the query result, but it does not copy indexes, triggers, or the AUTO_INCREMENT attribute, and it can change nullability and default behavior for columns. If you need constraints or keys later, create the structure first with LIKE, or use INSERT INTO ... SELECT after a matching table already exists.
- Create the snapshot or prepare a target. Run
CREATE TABLE new_table AS SELECT ... FROM old_table;for a snapshot, or create your own target table first if the structure must differ and then load it withINSERT INTO new_table SELECT ... FROM old_table;. - Load the rows. Run
INSERT INTO new_table SELECT * FROM old_table;or select specific columns if the target layout differs. - Resolve conflicts. If duplicate keys appear, adjust the target keys or add a
WHEREclause. If generated columns fail, keep those columns out of the insert list and let MySQL compute them.
A common first-timer mistake is using a data-only copy as a backup of the full table. If the table has a unique key, the insert can fail on the first duplicate row. If the table uses a different charset or collation, string comparisons can also behave differently after the copy.
How do you copy structure and data?
Use the full-copy pattern: CREATE TABLE ... LIKE first, then INSERT INTO ... SELECT. That is the standard two-step workflow for a complete copy with structure. The first step copies the table definition, and the second step copies the rows; triggers still need manual recreation.
- Create the target table. Run
CREATE TABLE copy_table LIKE source_table;This gives you the same table definition before any rows move. - Copy the rows. Run
INSERT INTO copy_table SELECT * FROM source_table;This copies rows from source to target. - Recreate triggers. If the source table had triggers, recreate them with
CREATE TRIGGERstatements. Neither method copies triggers.
Foreign key constraints need special attention here. LIKE does not copy foreign key definitions, so the new table can be missing those constraints even if the original had them. If the table uses generated columns, verify that the insert list does not try to write into a stored generated field.
How do you copy a table to another database?
You can copy to a different database by qualifying the table names with the database name. Use the source database on the right side of the select and the target database on the create or insert target.
- Create in the target schema. Run
CREATE TABLE target_db.new_table LIKE source_db.old_table; - Copy across schemas. Run
INSERT INTO target_db.new_table SELECT * FROM source_db.old_table; - Check rights first. Make sure the account can read the source schema and create and insert into the target schema. Without both, the copy stops partway through.
Say your source table is in archive_2025 and the target is in test_lab. Prefix both names in the SQL, or MySQL will create the clone in the current schema instead of the one you intended.
Common mistakes
Most copy errors come from assuming the table clone is complete when it is not. The main risks are missing indexes, missing triggers, AUTO_INCREMENT changes, and data that no longer fits the target definition. Large tables can add lock time and downtime risk when they are busy or when the copy method takes stronger locks.
- Duplicate-key conflicts: clean the target key set or filter the insert rows.
- Charset or collation mismatch: keep the target definition aligned with the source table.
- Generated columns: exclude them from manual inserts unless the schema allows explicit writes.
- Permission gaps: verify CREATE, INSERT, and SELECT on both databases.
- Locks on large tables: run during a low-traffic window if the copy touches a busy production table.
If that did not work, try this next: verify the clone in three passes. First compare row counts with SELECT COUNT(*) on both tables. Then run SHOW CREATE TABLE to confirm the definition. Finally inspect indexes with SHOW INDEX FROM table_name; so you can catch missing keys before the table goes live.
How do you copy a table safely for backup or testing?
For backup or testing, start with CREATE TABLE ... LIKE, then load only what you need with INSERT INTO ... SELECT. That gives you a controlled copy, lets you filter rows, and keeps the structure aligned with the source. For a quick data snapshot, use CREATE TABLE ... AS SELECT and accept the missing metadata.
- Pick the least risky method. Use
LIKEplusINSERT INTO ... SELECTif the copy must behave like the source. UseAS SELECTonly when metadata is not needed. - Run the copy on a quiet window. Large tables can hold locks long enough to affect writers and readers.
- Verify before promotion. Confirm row counts, table definition, and indexes before the clone replaces any workload.
MySQL and MariaDB both use these same copy patterns. The syntax is simple, but the result changes based on the goal: empty shell, fast snapshot, or full copy with rows. If the target is production-like, rebuild triggers and check foreign keys before calling it done.
Frequently asked questions
How do you copy a table in MySQL without copying the data?
Run CREATE TABLE new_table LIKE old_table;. That creates the table structure without rows. If you need proof, run SHOW CREATE TABLE new_table; right after creation and confirm the row count stays at zero.
How do you copy a table in MySQL with only the data?
Use CREATE TABLE new_table AS SELECT ... for a fast data-only snapshot, or use INSERT INTO target_table SELECT * FROM source_table; after the target table already exists. If the target has a different column order, list the columns explicitly in both parts of the statement so MySQL maps values correctly.
How do you copy a table in MySQL with structure and data?
Use the two-step pattern: CREATE TABLE copy_table LIKE source_table; then INSERT INTO copy_table SELECT * FROM source_table;. This is the full-copy workflow. Recreate triggers separately if the source table depends on them.
What is the difference between CREATE TABLE… LIKE and CREATE TABLE… AS SELECT?
LIKE copies the table structure, while AS SELECT is a fast data-only snapshot. MySQL documentation says AS SELECT does not copy indexes, triggers, or the AUTO_INCREMENT attribute, and it can change nullability and default behavior. Use LIKE when you need a table that behaves like the source.
Does CREATE TABLE… LIKE copy indexes and foreign keys?
LIKE copies indexes, but it does not copy foreign key definitions. Check the target definition with SHOW CREATE TABLE and verify that the constraints you need are present.
Does CREATE TABLE… AS SELECT copy AUTO_INCREMENT or triggers?
No. MySQL documentation says AS SELECT does not copy AUTO_INCREMENT or triggers, and it does not copy indexes. If you need those objects, create the table with LIKE first and then insert the rows.