Database Migration
Data Transfer
SQL
Table Copying
Database Management

Easiest way to copy a table from one database to another?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

The easiest way to copy a table depends on where the source and target databases live. If both databases are on the same server and the same database engine, a single CREATE TABLE AS SELECT or INSERT ... SELECT is usually enough. If they are on different servers or different engines, a dump-and-load or ETL approach is usually simpler and safer.

Same Server, Same Engine

If both databases are reachable in one SQL session, the job is straightforward.

To copy structure and data in one step, many engines support a form of:

sql
CREATE TABLE target_db.customers AS
SELECT *
FROM source_db.customers;

This is easy, but there is a catch: depending on the database engine, indexes, constraints, defaults, triggers, and foreign keys may not come across the way you expect.

If the target table already exists and you only want the rows, use:

sql
INSERT INTO target_db.customers
SELECT *
FROM source_db.customers;

That is usually the simplest answer when the schemas already match.

Copy Schema First When Metadata Matters

For production work, copying just the data is often not enough. You may need:

  • primary keys
  • indexes
  • constraints
  • generated columns
  • triggers

In that case, create the table structure explicitly, then copy the rows:

sql
1CREATE TABLE target_db.customers (
2    id INT PRIMARY KEY,
3    name VARCHAR(100) NOT NULL,
4    email VARCHAR(255) UNIQUE
5);
6
7INSERT INTO target_db.customers (id, name, email)
8SELECT id, name, email
9FROM source_db.customers;

This is more work up front, but it avoids the false confidence that comes from a quick copy that silently loses metadata.

Different Servers or Different Engines

If the two databases cannot be queried together directly, the easiest path is usually export and import.

For example:

  • 'mysqldump and reload for MySQL'
  • 'pg_dump and psql for PostgreSQL'
  • CSV export plus import when cross-engine compatibility matters

A CSV workflow is primitive but portable:

sql
SELECT id, name, email
FROM customers;

Export the result to CSV from the source, then import it into the destination with the engine’s bulk-load tool. It is rarely the most elegant option, but it is often the most universal.

Bulk Copy Tools Are Better for Large Tables

For large data transfers, prefer the engine’s bulk tools over row-by-row application code. Bulk operations are faster, easier to resume, and less likely to produce partial inconsistent results.

If the source table is huge, you may also need to copy in batches:

sql
1INSERT INTO target_db.orders (id, total, created_at)
2SELECT id, total, created_at
3FROM source_db.orders
4WHERE id BETWEEN 1 AND 100000;

Then continue with the next range. This is especially useful when the table is live and you want to control transaction size.

Watch for Identity Columns and Constraints

A table copy is often blocked or corrupted by details such as:

  • identity or auto-increment columns
  • foreign key dependencies
  • collation differences
  • incompatible data types
  • existing rows in the target

That is why “easiest” depends on the operational goal. A throwaway analytics copy is different from a production migration that must preserve integrity and downtime limits.

A Good Default Strategy

A solid practical sequence is:

  1. copy or define the target schema
  2. bulk-load the data
  3. recreate or validate indexes and constraints
  4. verify row counts and spot-check critical data

That is still simple enough to execute, but safer than assuming one SQL statement finishes the whole migration correctly.

Common Pitfalls

The biggest mistake is assuming CREATE TABLE AS SELECT creates a perfect clone. It usually copies data and basic column structure, not every piece of table metadata.

Another issue is using SELECT * when source and target schemas are not guaranteed to stay identical. Explicit column lists are safer for anything important.

Developers also often forget transaction size and locking impact on large tables. A single giant copy may work in testing and become disruptive in production.

Finally, verify the result. Matching row counts, null expectations, and key integrity should be part of the copy process, not an afterthought.

Summary

  • If both databases are on the same server, INSERT ... SELECT or CREATE TABLE AS SELECT is usually the easiest path.
  • For production copies, handle schema and metadata deliberately.
  • For different servers or engines, dump-and-load or CSV import/export is often simpler.
  • Use bulk operations for large tables instead of row-by-row application code.
  • Always validate rows, keys, and constraints after the copy.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.