How to RENAME TABLE in MariaDB

Galaxy Glossary

How do I rename a table in MariaDB without losing data?

RENAME TABLE changes the name of one or more tables in a single, atomic operation.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

What does RENAME TABLE do in MariaDB?

RENAME TABLE instantly changes a table’s name without copying data. When multiple tables are listed, the operation is atomic—either all names change or none.

How do I rename a single table?

Issue RENAME TABLE old_name TO new_name;. The statement requires ALTER privilege on the table and CREATE, DROP on the database.

Example: rename Customers to Clients

RENAME TABLE Customers TO Clients;

How can I rename several tables at once?

Chain pairs with commas: RENAME TABLE t1 TO t1_old, t2 TO t2_old;. If any pair fails, MariaDB rolls back all changes.

Batch ecommerce example

RENAME TABLE Orders TO Orders_2023, OrderItems TO OrderItems_2023;

Can I move a table to another database?

Yes. Prefix the new name with the target schema: RENAME TABLE shop.Orders TO archive.Orders_2023;. The table’s definition and data relocate.

What happens to foreign keys and views?

Constraints automatically update. Views, triggers, and stored procedures using the old name break; refresh or alter them after the rename.

Best practices for safe renames

Create a backup beforehand, schedule during low-traffic windows, and update application code via feature flags to avoid runtime errors.

Why How to RENAME TABLE in MariaDB is important

How to RENAME TABLE in MariaDB Example Usage


-- Move and rename an ecommerce table while archiving
RENAME TABLE Orders TO archive.Orders_2023;

How to RENAME TABLE in MariaDB Syntax


RENAME TABLE
    old_table_name TO new_table_name
  [, old_table_name2 TO new_table_name2] ...;

-- Cross-database move
RENAME TABLE current_db.old_name TO other_db.new_name;

Common Mistakes

Frequently Asked Questions (FAQs)

Does RENAME TABLE lock the table?

Yes, an exclusive metadata lock is taken but usually released in milliseconds because no data is copied.

Is the operation transactional?

For InnoDB tables, all listed renames succeed or fail together, ensuring consistency.

Can I undo a rename?

You can run RENAME TABLE new_name TO old_name; as long as the original name is still free.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.