Explain Codes LogoExplain Codes Logo

I want to copy table content from one database and insert it into another database table

sql
data-transfer
database-migration
sql-commands
Nikita BarsukovbyNikita Barsukov·Oct 5, 2024
TLDR

Let's copy-paste in SQL! Here's your line of code to do the magic:

INSERT INTO TargetDB.TargetTable SELECT * FROM SourceDB.SourceTable;

Replace TargetDB, SourceDB, as well as TargetTable and SourceTable accordingly. This statement gracefully whisks the data between the two tables on the same server.

Before you press the big red button

Confirm the existence of both your source and destination tables. Run these reality check commands:

/* Remember Dory from Finding Nemo: "Just keep swimming, or better, check for SourceTable" */ SELECT * FROM information_schema.tables WHERE table_schema = 'SourceDB' AND table_name = 'SourceTable'; /* Checking for TargetTable: "Are we there yet?" */ SELECT * FROM information_schema.tables WHERE table_schema = 'TargetDB' AND table_name = 'TargetTable';

Existence really is a big deal when transferring data, you don't want to spill your data into the void.

Remember your permissions and backups

Make sure you have the rights to play around with the source and destination. It's like your VIP pass to access the data. And don't forget, backups are your best friends in times of disaster:

/* Your data feels more secure with backup, like granny's secret cookie recipe */ BACKUP DATABASE SourceDB TO DISK = '/path/SourceDB.bak'; BACKUP DATABASE TargetDB TO DISK = '/path/TargetDB.bak';

Big data and complex migrations

If the data feels like moving mountains, consider power tools built for such tasks. Oracle's Data Pump and SQL Server's SSIS are for those who love flexing their database muscles.

Live data: Handle with care

Are you dealing with a live database? Tread lightly! Move stealthily during off-peak hours and consider replication or logging mechanisms to maintain data sync once you're done.

Sail across different servers

Moving data across different servers? Use the mysqldump command for MySQL:

mysqldump -u user -p SourceDB SourceTable | mysql -u user -p -h other-server TargetDB

I am Groot! Or wait, maybe just 'root'

Make sure you're logged in as a user with Beyoncé-level rights (root rights) to execute the transfer.

Table structure: Carbon copy

Making a clone of the table's structure? Here's how:

CREATE TABLE TargetDB.TargetTable LIKE SourceDB.SourceTable;

So nice, you did it twice? Automate!

If you find yourself repeating this task, it's automation time. Create a script or use scheduler tools to save your sanity!

Types, constraints: Don't get tied down

Data types and constraints can tie you down during table copies. Verify column compatibility and handle nuances, like auto-increment fields, with care.

The aftermath of the data transfer

Congratulations! The transfer is done. Now, validate the target database:

/* Checking for all data in TargetTable, like counting your money after payday */ SELECT COUNT(*) FROM TargetDB.TargetTable;

Handling the bulk

For heavier datasets, monitor the process and consider breaking the insertion into chunks. Batch it out, baby!