- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL RENAME TABLE
The RENAME TABLE statement is a fundamental DDL (Data Definition Language) command used to change the name of an existing table in your database. As projects evolve, initial naming conventions might no longer fit your data structure, or you might need to swap tables during a data migration process. This command allows you to update your schema without losing any of your stored data.
Key Features of SQL RENAME TABLE
- Data Integrity: Changing the name does not affect the rows inside the table; all data remains exactly as it was.
- Metadata Preservation: Indexes, constraints (like Primary Keys or Foreign Keys), and column definitions are automatically carried over to the new name.
- Multi-Table Support: Some engines, like MySQL, allow you to rename multiple tables in a single atomic transaction.
- Database Specifics: While the concept is universal, the exact syntax varies between MySQL/MariaDB and other systems like PostgreSQL or SQL Server.
SQL RENAME TABLE Syntax
In MySQL and MariaDB, the syntax is straightforward. You specify the current table name followed by the desired new name.
RENAME TABLE old_table_name TO new_table_name;
Example: Renaming a Table
Imagine you have a table named Employees, but your HR department has requested it be changed to Staff to better reflect the company culture.
RENAME TABLE Employees TO Staff;
- This command instantly renames the
Employeestable toStaff. - Any existing records, such as employee IDs and salaries, remain untouched.
Example: Renaming Multiple Tables
A unique feature of the RENAME TABLE syntax in MySQL is the ability to rename several tables at once. This is particularly useful when performing a batch update or versioning your schema.
RENAME TABLE Orders TO CustomerOrders, Products TO Inventory;
- Changes
OrderstoCustomerOrdersandProductstoInventoryin one go. - The operation is atomic: if any single rename fails, none of the renames in the statement will take effect.
Alternative Commands for Other Databases
If you are working with databases other than MySQL or MariaDB, the RENAME TABLE command might not work. Instead, most other SQL dialects use the ALTER TABLE statement.
sp_rename is a stored procedure. Note that the table names should be wrapped in single quotes within the parentheses.
Summary
- Primary Use:
RENAME TABLEis the standard way to rename tables in MySQL and MariaDB. - Dialect Variation: Most other systems (PostgreSQL, Oracle, SQLite) use
ALTER TABLE ... RENAME TO .... - Safety: Data, indexes, and constraints are preserved, but external dependencies (like app code) must be updated manually.
- Bulk Operations: MySQL allows renaming multiple tables simultaneously, ensuring an "all or nothing" result.