Rename Table Name (Change Table Name in SQL)

  • Renaming a table means giving it a new name without changing its content. It's like changing the label on a box without changing what's inside.
  • Imagine you have a table named "Books" that contains information about different books. However, you realize that the name "Books" is not descriptive enough, and you want to change it to "Library" to better reflect its purpose.

  • Renaming the table is like giving it a new name, just like renaming a file on your computer. The table's structure, columns, and data remain the same; only the name is updated.
  • By renaming the table to "Library," you can have a more fitting and understandable name for that collection of book information.
  • So, renaming a table is a simple way to update its name without affecting its contents. It helps improve clarity, organization, and understanding within the database. Lets take one example to understand how the rename query works:


ALSO READ-👉Use  of SELECT DISTINCT query on single or multiple column with distinct count()
Select statement 

Rename Table

    For example, if you want to rename the "Staff" table to "Employees"

    Syntax for Rename statement:


Syntax
RENAME TABLE old_table_name TO new_table_name;
Lets add values in Syntax


Example
RENAME TABLE Staff TO Employees;
After using rename statement table will look like this:
Employees
ID Given_Name Last_Name
1 Rahul Sharma
2 Priya Patel


You can also use Alter statement to change the name of table or make changes in the name of column.
for this you can use his syntax:

Syntax
ALTER TABLE old_table_name RENAME TO new_table_name;

Les add value in the sysnax

Example
ALTER TABLE Staff RENAME TO Employees;

Rename column using ALTER TABLE


In SQL, renaming a column means changing its name in a table. This helps make your database structure clearer and more organized. To rename a column, you use the ALTER TABLE statement with the RENAME COLUMN clause. You provide the table name, the current column name, and the new name you want for the column. Keep in mind that renaming a column might require updating any queries or code that use the old column name. Also, be careful with constraints, indexes, and permissions when renaming columns.

change the name of table in sql



     Note that when renaming a column, you may also need to specify the datatype of the column in some database management systems, like MySQL. Make sure to replace old_table_name, new_table_name, table_name, old_column_name, new_column_name, and datatype with the actual names you want to use in your scenario.

Syntax
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
Here is a example of renaming a column:
Example
ALTER TABLE Staff RENAME COLUMN Full Name To Name;

Difference between RENAME and ALTER 

RENAME:


RENAME statement is used to rename an existing database object, such as a table or a column.
It changes the name of the object while preserving its structure and data.
The RENAME statement is focused solely on renaming the object and does not modify its properties or structure beyond the name change.

Example: RENAME TABLE old_table_name TO new_table_name;

ALTER:


ALTER statement is used to modify the structure or properties of an existing database object.
It allows you to make changes to objects like tables, columns, constraints, indexes, etc.
The ALTER statement can be used for a variety of modifications, including adding, modifying, or deleting columns, changing data types, adding or dropping constraints, and more.
It is a more comprehensive statement that provides a broader range of modification capabilities.

Example:
  ALTER TABLE table_name 
ADD COLUMN column_name datatype;

In summary, the RENAME statement is specifically for renaming database objects, while the ALTER statement is a more versatile statement used for modifying various aspects of database objects beyond just renaming.

Conclusion:

When you rename a table, you're not changing the data inside it; you're only changing how it is referred to. It's similar to when you change the name of a file on your computer, but the contents of the file remain the same.

People rename tables for various reasons. It can make the database structure easier to understand, follow specific naming conventions, or adapt to changes in the system or business. Renaming a table helps keep things organized and consistent.

Just remember that when you rename a table, you're only changing its name. The structure and data inside the table remain the same.