
#Mysql rename table update
Update all references to the table − After renaming a table, any stored procedures, views, triggers, or other database objects that reference the table will need to be updated to use the new name of the table. Renaming these tables can cause issues with the functioning of the database system, so it is generally not recommended to rename system tables. Here are some general rules to keep in mind when renaming tables in SQL −Īvoid renaming system tables − System tables are tables that are created and used by the database management system itself. When renaming tables in SQL, there are some rules and best practices that should be followed to ensure that the renaming process goes smoothly and does not cause any unintended consequences or issues. Rules to be followed while Renaming Tables Note − We have renamed the table to Workers if the user tries to get the details by using the old table name, it will throw an error showing that the table does not exist. Following is the query to display the records in the Workers table − We can verify whether the changes are reflected in a table by retrieving its contents using the SELECT statement. So, we are going to rename this table from Customers to Workers, using the following query −Ĭaution: Changing any part of an object name could break scripts and stored procedures. Now we know that we have an existing table ‘CUSTOMERS’ in our database. INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) Now insert values into this table using the INSERT statement as follows − In this example, let us first try to create a table with the name ‘CUSTOMERS’ which contains the personal details of customers including their name, age, address and salary etc. Before executing this statement, it is important to make sure that the table is not locked and there are no active transactions. Here, we must ensure that old table name is present in the database and that new table name does not already exist. Syntaxįollowing is the basic syntax to rename a table in SQL −ĮXEC sp_rename 'old_table_name', 'new_table_name' The sp_rename is a system stored procedure (set of pre-built subroutines that perform tasks within the database) in SQL that can be used to rename various database objects including tables, columns, indexes, and constraints. However, it does give you access to a stored procedure called sp_rename that enables you to rename a table. There isn't a query in SQL Server that can rename a table directly.


For instance, in a business organization, there would arise a need to rename the current table to a new name in order to better reflect new circumstances, since business requirements change frequently.

In some cases, users and database administrators desire to rename a table in an SQL database to give it a name that is more appropriate in a certain scenario.
