π SQL RENAME TABLE / COLUMN β Change Table or Field Names Safely
π§² Introduction β Why Rename in SQL?
Renaming tables or columns in SQL is a common maintenance task when evolving database schemas. It helps improve clarity, align with new naming conventions, or fix legacy inconsistencies.
π― In this guide, you’ll learn:
- How to rename tables and columns across SQL engines
- Platform-specific syntax (MySQL, PostgreSQL, SQL Server)
- Best practices for safe schema refactoring
β 1. RENAME TABLE Syntax
-- MySQL
RENAME TABLE old_table TO new_table;
-- PostgreSQL
ALTER TABLE old_table RENAME TO new_table;
-- SQL Server
EXEC sp_rename 'old_table', 'new_table';
β Always test the rename in dev or staging first.
βοΈ 2. RENAME COLUMN Syntax
-- PostgreSQL / SQL Server 2016+
ALTER TABLE table_name RENAME COLUMN old_col TO new_col;
-- MySQL (requires full column definition)
ALTER TABLE table_name CHANGE old_col new_col datatype;
-- SQL Server
EXEC sp_rename 'table.old_col', 'new_col', 'COLUMN';
β Syntax varies. Be cautious with MySQLβit needs the full type.
π 3. Example β Rename Column in Products
-- PostgreSQL
ALTER TABLE products RENAME COLUMN desc TO description;
-- MySQL
ALTER TABLE products CHANGE desc description VARCHAR(255);
β Updates the column name while preserving structure and data.
β οΈ 4. Impact of Renaming
- May break views, triggers, procedures, and application queries
- Rename in source control and application code too
- Check for dependent foreign key constraints or indexes
π Best Practices
β Recommended | β Avoid This |
---|---|
Rename during low-traffic maintenance | Renaming in production without backup |
Update app and migration files | Forgetting to rename in related code |
Test on backup or staging instance | Renaming system-critical tables live |
π Summary β Recap & Next Steps
Renaming SQL tables and columns is important for schema clarity and evolution. However, it’s a potentially disruptive actionβplan, test, and communicate before deploying.
π Key Takeaways:
- Use
ALTER TABLE RENAME
orsp_rename
based on platform - Always review dependent objects (views, scripts, code)
- Include renames in migration/version control systems
βοΈ Real-World Relevance:
Used in schema refactoring, rebranding, normalization, and legacy migrations.
β‘οΈ Next: Learn ALTER TABLE
for more structural changes or explore dependency tracking tools.
β FAQ β SQL RENAME TABLE / COLUMN
β Can I rename a table in SQL?
β
Yes. Use ALTER TABLE ... RENAME TO
or RENAME TABLE
depending on DBMS.
β Can I rename a column in SQL Server?
β
Yes. Use sp_rename
for columns:
EXEC sp_rename 'table.column', 'new_name', 'COLUMN';
β Will renaming affect indexes or keys?
β No, but you should verify their dependencies.
β Is there a way to rollback a rename?
β Not automatically. Rename again or restore from backup.
Share Now :