Updated March 6, 2023
Introduction to Oracle rename table
Oracle provides the rename table facility to the user, in which we can rename the existing table name as per our requirement. When we rename the table name at that time oracle automatically transfers all rights such as indexes, constraints, and grants on the old table name to the new table name. at the same additionally, oracle invalidates all objects that depend on that specified rename table name such as views, functions, procedure, and synonyms. For renaming table names we can use two different types of command such as rename table name command and alter table name command.
Syntax
rename old specified table name to new specified table name;
Explanation
In the above syntax, we use rename table command to change the existing table name, view, sequence, or synonym, here the old specified table name means existing table name, to is the keyword for reference purpose and the new specified table name is used for a new name for exiting table name. The newly renamed name must not already be used for another.
How to rename a table in Oracle?
Now let’s see how we can rename the table in oracle as follows.
Basically, there are two ways to rename the table first by using rename command and the second is that by using alter table name command. Both commands are very simple. We can easily rename the table name but we must have some privilege to rename the table name. We either have a database owner or table owner then and then we are able to rename the table name. Another main point is that we need an existing table to perform the rename command. Rename the command syntax we already discussed in the above point. Table names must be unique and if there is any dependence on that table and we need to perform the rename command at that time we can rename the table name but cannot rename synonyms that depend on that table.
Examples
Now let’s see the different examples of oracle rename tables for better understanding as follows.
First, we need an existing table to perform the rename table command, so let’s create a table by using the following statement as follows.
create table product(
product_id int,
product_name varchar(255) not null,
product_cost decimal(15,2) not null,
primary key (product_id));
Explanation
In the above example, we used a create table to create a new table name as a product, here the product is the new table and we created it with different attributes as shown in the above statement. When we execute the above query then the final output we illustrate by using the following snapshot.
Now we are able to perform the rename statement as follows.
rename product to new_product;
Explanation
In the above example, we use a rename statement to rename the existing table name, here the product is the old table name that we need to rename, and to is the keyword for referencing the new name, new_product is the new renamed name of the existing table. When we execute the above query then the final output we illustrate by using the following snapshot.
Now let’s see another way to rename the table name as follows.
Syntax
alter table specified table name rename to new table name;
Explanation
In above syntax we use alter table command to rename the table name, here specified table name means existing table name, rename to is the keywords used to rename the table name after that we mentioned the new table name as shown in above syntax.
Example
In this example, we used an already created table name as a new_product.
alter table new_product rename to product_demo;
Explanation
In the above example, we use the alter table command to rename the table name, here the new_product is the existing table name that we need to rename and product_demo is the new name of the existing table that we renamed. When we execute the above query then the final output we illustrate by using the following snapshot.
- Now insert some records into the table by using the insert into the statement and then perform the rename table command as follows.
After inserting records in the product_demo table, use a select statement to see the records.
select * from product_demo;
When we execute the above query then the final output we illustrate by using the following snapshot.
Now perform the rename command to check if it transfers all constraints to the renamed table or not as follows.
rename product_demo to product_sample;
After executing the above query, now see the content of the new rename table as follows.
select * from product_sample;
When we execute the above query then the final output we illustrate by using the following snapshot.
Explanation
Now see the difference before the rename table and after the rename table. The below screenshot shows the inserted records of product_demo as below.
Rules and regulation for rename table
Now let’s see the different rules and regulations for rename table statements as follows.
- The new name of the specified table must be unique that means the name of the table does not already exist otherwise it will get an error like the name is already used.
- When we need to rename the table name we must have the privilege to rename the table that means we must either be the database owner or the table owner.
- All data definition language-related queries we can execute in auto-commit mode, which means once we rename the table name then we are not able to revert it back.
- If the table consists of a view or foreign key that references the specified table and we need to rename that table at that time it shows the error message. On the other hand, if there is any constraint or triggers on that table and we attempt to rename that table then it will also show the error message.
- If there is any open cursor and that references the specified table in this situation the rename statement is not working.
Conclusion
We hope from this article you have understood about the Oracle rename table. From this article, we have learned the basic syntax of rename tables and we also see different examples of rename tables. From this article, we learned how and when we use the Oracle rename table.
Recommended Articles
This is a guide to Oracle rename table. Here we discuss the basic syntax of rename tables and we also see different examples of rename tables. You may also have a look at the following articles to learn more –