Cloning a table in MySQL or MariaDB provides a quick way to test schema changes, validate queries against real data, or stage bulk updates without touching the original rows.
A practical clone is usually a two-step SQL workflow: CREATE TABLE … LIKE copies the table definition into a new, empty table, and INSERT INTO … SELECT populates it by reading from the source table.
Clones created with these statements are not perfect replicas of every related database object. FOREIGN KEY constraints and triggers are not copied by CREATE TABLE … LIKE, and large INSERT … SELECT operations can be slow, consume disk space, and generate large binary logs on replicated servers.
Steps to copy or clone a MySQL or MariaDB table:
- Open the MySQL or MariaDB client as a user with permission to create tables.
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 50 Server version: 8.0.44 MySQL Community Server - GPL Copyright (c) 2000, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Required privileges typically include SELECT on the source table and CREATE, INSERT on the destination table.
- Verify the new table structure.
mysql> DESCRIBE employees_new; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | first_name | varchar(100) | NO | | NULL | | | last_name | varchar(100) | NO | | NULL | | | department | varchar(100) | NO | | NULL | | | hired_on | date | NO | | NULL | | +------------+--------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
- Copy the data from the original table to the new table.
mysql> INSERT INTO employees_new SELECT * FROM employees; Query OK, 9 rows affected (0.00 sec) Records: 9 Duplicates: 0 Warnings: 0
On large tables, the insert can run for a long time, fill disks, and create large binary logs on replicated servers.
- Verify that the row counts match between source and destination.
mysql> SELECT (SELECT COUNT(*) FROM employees) AS src_rows, -> (SELECT COUNT(*) FROM employees_new) AS dst_rows; +----------+----------+ | src_rows | dst_rows | +----------+----------+ | 9 | 9 | +----------+----------+ 1 row in set (0.00 sec)
- Preview a few rows from the cloned table.
mysql> SELECT * FROM employees_new LIMIT 5; +----+------------+-----------+-------------+------------+ | id | first_name | last_name | department | hired_on | +----+------------+-----------+-------------+------------+ | 1 | Taylor | Nguyen | Engineering | 2020-03-15 | | 2 | Casey | Morgan | Support | 2021-07-20 | | 3 | Riley | Singh | Finance | 2019-11-02 | | 4 | Taylor | Nguyen | Engineering | 2020-03-15 | | 5 | Casey | Morgan | Support | 2021-07-20 | +----+------------+-----------+-------------+------------+ 5 rows in set (0.00 sec)
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.
