Duplicate a table’s structure and data from one MariaDB database to another with CREATE TABLE … LIKE followed by INSERT INTO … SELECT.
Developers often need to migrate data from staging to production, seed test environments, or archive historical data. Copying a table keeps column definitions, indexes, and rows intact, saving time compared with manual rebuilds.
Step 1 clones the table structure with CREATE TABLE target_db.table LIKE source_db.table;. Step 2 moves data with INSERT INTO target_db.table SELECT * FROM source_db.table;. Run both statements in the same session to ensure identical schemas.
Run CREATE TABLE target_db.table LIKE source_db.table;. The new table is empty but inherits columns, data types, primary keys, indexes, and constraints—ideal when you plan to load different data later.
You can wrap both steps in a single stored procedure or script, but MariaDB requires the INSERT step to move rows. Use START TRANSACTION to keep the operation atomic and avoid partial copies.
Yes. Add a WHERE clause inside the INSERT … SELECT statement. Example: INSERT INTO prod.Orders SELECT * FROM staging.Orders WHERE order_date >= CURDATE() - INTERVAL 30 DAY; to move only recent orders.
CREATE TABLE … LIKE keeps AUTO_INCREMENT definitions, and INSERT … SELECT transfers actual id values. The target table’s AUTO_INCREMENT counter automatically advances to the highest inserted id.
Use LOCK TABLES source_db.table READ, target_db.table WRITE when copying large, live tables to prevent writes that could create inconsistencies.
Copy during low-traffic windows, copy in batches with LIMIT to reduce lock time, and monitor disk space in the target database. Always test in a non-production environment first.
Yes. It duplicates primary keys, unique keys, indexes, AUTO_INCREMENT settings, and column defaults, but not foreign key references to other databases.
Use mysqldump --no-create-db --tables source_db.table | mysql -h target_host target_db, or employ MariaDB replication. The in-database method works only inside the same server.
Yes. In-server copies avoid disk I/O and parsing overhead, making them significantly quicker for most workloads.