MySQL
Database Management
Rename Database
SQL Commands
Schema Name Change

How to rename a MySQL database (change schema name)

Codemia Knowledge Hub ·

MySQL has no RENAME DATABASE command in modern versions, so renaming a schema is always a migration: create the new schema, move or import the objects, verify, then drop the old one. Which method fits depends on how much downtime you can accept, how many objects there are, and whether objects reference each other across schemas.

Why there is no rename command

A direct database rename historically caused too many integrity and metadata problems across storage engines and server versions. MySQL now expects administrators to use explicit migration steps, so plan the rename as a controlled cutover.

Method 1: dump and restore

For most environments this is the clearest and safest approach.

1) Create the target schema

sql
CREATE DATABASE new_schema CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

2) Export the old schema

bash
mysqldump -u app_user -p --routines --triggers --events old_schema > old_schema.sql

3) Import into the new schema

bash
mysql -u app_user -p new_schema < old_schema.sql

4) Check counts and key objects

sql
SELECT COUNT(*) FROM new_schema.some_table;
SHOW TRIGGERS FROM new_schema;
SHOW PROCEDURE STATUS WHERE Db = 'new_schema';

5) Re-point the application, then drop the old schema

sql
DROP DATABASE old_schema;

With the right dump options (--routines --triggers --events), this preserves object definitions.

Method 2: move tables with RENAME TABLE

For a faster move inside the same server, create the new schema and rename tables into it one by one:

sql
CREATE DATABASE new_schema;
RENAME TABLE old_schema.orders TO new_schema.orders;
RENAME TABLE old_schema.customers TO new_schema.customers;

This moves table data quickly, with caveats:

  • Views, routines and events are not migrated by table renames. Moving tables and assuming the rest came along is a common mistake.
  • Stored routine definitions can contain schema-qualified references that still point to the old schema.
  • Foreign keys and view dependencies need careful checking.

For complex databases, dump and restore is usually less error-prone.

Generating the rename statements

For many tables, generate the SQL from metadata:

sql
1SELECT CONCAT(
2  'RENAME TABLE `old_schema`.`', table_name,
3  '` TO `new_schema`.`', table_name, '`;'
4) AS stmt
5FROM information_schema.tables
6WHERE table_schema = 'old_schema'
7  AND table_type = 'BASE TABLE';

Review the generated statements before running them. Do not run metadata-generated SQL blindly in production.

Privileges and application cutover

The new schema needs its own grants, and incomplete grants are a frequent problem:

sql
GRANT SELECT, INSERT, UPDATE, DELETE ON new_schema.* TO 'app_user'@'%';
FLUSH PRIVILEGES;

Then update application configuration:

  • the schema name in connection strings
  • migration tool config
  • reporting jobs and ETL scripts

Run read and write smoke tests against the new schema before deleting the old one.

Keeping data consistent during the move

If writes continue during the migration, the old and new schemas diverge. Pick one strategy:

  • a short maintenance window and full cutover
  • replication-style dual writes during the transition
  • a read-only freeze before the final export

For most teams, a planned maintenance window is the simplest and safest.

Checklist before dropping the old schema

  1. Table counts match expected values.
  2. Critical queries run from the app against the new schema.
  3. Routines, triggers and events exist and compile.
  4. Privileges and monitoring dashboards are updated.
  5. A backup exists for rollback.

Dropping the old schema before the application is fully verified removes your rollback option.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design