04 / 13

How to rename a table?

MySQL provides two different ways to rename an MySQL table. We can use either the RENAME TABLE or ALTER TABLE statement.

Query:
Renaming multiple tables
Difficulty: 6/10
Topics: DDL Locks, Zero-Downtime Migrations, Schema Refactoring

Scenario Questions

0-2 years experience
  1. 1

    We have a table called 'usr' and we want to rename it to 'users' to match our style guide. How would you write the SQL to do this safely, and what happens to any active SELECT queries running on 'usr' at the exact moment you execute the rename?

  2. 2

    Imagine you need to rename a table in production. You run the rename command, but you realize there's a background cron job running every minute that inserts data into the old table name. What error will that cron job throw, and how could we have structured the deployment to prevent that failure?

2-5 years experience
  1. 1

    We need to rename our 'orders' table to 'customer_orders' as part of a refactor. However, this table is referenced by several foreign keys in other tables, and there are a couple of database views pointing to it. If you just run a standard 'RENAME TABLE', what breaks, and how do you coordinate this change to avoid application downtime?

  2. 2

    You are deploying a backend service update where a table name changes. The rolling deployment takes about 5 minutes to update all application pods. If you rename the table first, the old pods break; if you deploy the code first, the new pods break. How do you solve this 'chicken-and-egg' deployment problem using MySQL features like views or aliases?

5-8 years experience
  1. 1

    We have a 500GB 'transactions' table under heavy write load (thousands of QPS). We need to rename it to 'ledger_entries'. While a rename is a fast metadata-only operation, it requires an exclusive Metadata Lock (MDL). How could this lock cause a catastrophic cascading outage under high load, and how would you safely execute or script this rename?

  2. 2

    We are using a master-replica setup with row-based replication. We need to rename a heavily queried table. What are the implications of running a 'RENAME TABLE' on the primary database in terms of replication lag, replica read availability, and lock propagation to the replicas?

8+ years experience
  1. 1

    We are refactoring a legacy monolith into microservices. The core 'accounts' table needs to be renamed to 'billing_profiles' and its schema modified, but 5 different engineering teams own services that query this database directly. How would you design a multi-phase migration strategy over several sprints to rename this table with zero downtime and zero coordination-lock between the teams' deployment schedules?

  2. 2

    Our team uses online schema change tools like 'gh-ost' or 'pt-online-schema-change' for large tables. These tools rely on renaming tables at the very end of their process to swap the shadow table with the real table. Can you walk me through the failure modes of this final 'swap' phase under extreme write loads, and how you would architect our CI/CD database migration pipeline to safely handle or roll back a failed swap?

Follow-up Questions

  • How does MySQL handle metadata locks (MDL) during a rename if there is a long-running SELECT query active?
  • What is the difference in locking behavior and atomicity between 'ALTER TABLE RENAME' and 'RENAME TABLE'?
  • How do triggers, foreign keys, and stored procedures behave when their underlying table is renamed?