


The specified table or view is renamed to the new name. The table-name or view-name must identify a table or view that exists at the current server, but must not identify a catalog table or a declared temporary table. We will discuss this in more detail in the following examples. Description TABLE table-name or view-name Identifies the table or view that will be renamed. In addition, you must manually adjust other database objects such as views, stored procedures, triggers, foreign key constraints, etc., that reference to the table.
#Sql rename table code
If the name of the table changes, so the application code that refers to the table name needs to be changed as well. For a complete reference of all the data. The data type specifies what type of data the column can hold. ADD DateOfBirth date Notice that the new column, 'DateOfBirth', is of type date and is going to hold a date. We use the following SQL statement: ALTER TABLE Persons. For example, you should investigate which applications are using the table. Now we want to add a column named 'DateOfBirth' in the 'Persons' table. In terms of security, any existing privileges that we granted to the old table must be manually migrated to the new table.īefore renaming a table, you should evaluate the impact thoroughly. Note that you cannot use the RENAME TABLE statement to rename a temporary table, but you can use the ALTER TABLE statement to rename a temporary table. In addition to the tables, we can use the RENAME TABLE statement to rename views.īefore we execute the RENAME TABLE statement, we must ensure that there is no active transactions or locked tables.

If the new table new_table_name does exist, the statement will fail. The old table ( old_table_name) must exist, and the new table ( new_table_name) must not. RENAME TABLE old_table_name TO new_table_name Code language: SQL (Structured Query Language) ( sql )
