🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In this chapter, we will learn how to rename a table in SQL using the RENAME TABLE statement. Renaming a table can be necessary when you want to give the table a more meaningful name or if you are reorganizing your database schema. This chapter will guide you through the syntax and provide examples to help you understand how to rename tables effectively.
Syntax for Renaming a Table
The RENAME TABLE statement is used to rename an existing table in a database. This operation changes the name of the table but retains all its data, structure, and indexes.
Basic Syntax
RENAME TABLE old_table_name TO new_table_name;
old_table_name: The current name of the table you want to rename.new_table_name: The new name you want to assign to the table.
Example
Let's assume we have a table named employees that we want to rename to staff.
RENAME TABLE employees TO staff;
Viewing the Table Structure
You can view the structure of a table using the DESCRIBE statement. This is useful to verify the structure before and after renaming the table.
Syntax
DESCRIBE table_name;
Example
Before renaming:
DESCRIBE employees;
After renaming:
DESCRIBE staff;
Output Example
Before Renaming
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| id | int(11) | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | YES | NULL | ||
| last_name | varchar(50) | YES | NULL | ||
| varchar(100) | YES | UNI | NULL |
After Renaming
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| id | int(11) | NO | PRI | NULL | auto_increment |
| first_name | varchar(50) | YES | NULL | ||
| last_name | varchar(50) | YES | NULL | ||
| varchar(100) | YES | UNI | NULL |
Using ALTER TABLE to Rename a Table (MySQL and MariaDB)
In some databases like MySQL and MariaDB, you can also use the ALTER TABLE statement to rename a table.
Syntax
ALTER TABLE old_table_name RENAME TO new_table_name;
Example
ALTER TABLE employees RENAME TO staff;
Conclusion
Renaming a table is a straightforward process in SQL. Using the RENAME TABLE or ALTER TABLE statements, you can easily change the name of a table while retaining its data and structure. This operation is useful when you need to update table names to better reflect their contents or purpose.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment