Как переименовать таблицу в oracle
Перейти к содержимому

Как переименовать таблицу в oracle

  • автор:

Как переименовать таблицу в oracle

You cannot roll back a RENAME statement.

Use the RENAME statement to rename a table, view, sequence, or private synonym.

Oracle Database automatically transfers integrity constraints, indexes, and grants on the old object to the new object.

Oracle Database invalidates all objects that depend on the renamed object, such as views, synonyms, and stored procedures and functions that refer to a renamed table.

The object must be in your own schema.

Specify the name of an existing table, view, sequence, or private synonym.

Specify the new name to be given to the existing object. The new name must not already be used by another schema object in the same namespace and must follow the rules for naming schema objects.

Restrictions on Renaming Objects

Renaming objects is subject to the following restrictions:

You cannot rename a public synonym. Instead, drop the public synonym and then re-create the public synonym with the new name.

You cannot rename a type synonym that has any dependent tables or dependent valid user-defined object types.

Renaming a Database Object: Example

The following example uses a copy of the sample table hr.departments . To change the name of table departments_new to emp_departments , issue the following statement:

You cannot use this statement directly to rename columns. However, you can rename a column using the ALTER TABLE . rename_column_clause .

Another way to rename a column is to use the RENAME statement together with the CREATE TABLE statement with AS subquery . This method is useful if you are changing the structure of a table rather than only renaming a column. The following statements re-create the sample table hr.job_history , renaming a column from department_id to dept_id :

Any integrity constraints defined on table job_history will be lost in the preceding example. You will have to redefine them on the new job_history table using an ALTER TABLE statement.

ALTER TABLE ОПЕРАТОР

В этом учебном пособии вы узнаете, как использовать в Oracle/PLSQL оператор ALTER TABLE, чтобы добавить столбец, изменить столбец, удалить столбец, переименовать столбец или переименовать таблицу (с синтаксисом, примерами и практическими упражнениями).

Описание

Оператор Oracle/PLSQL ALTER TABLE используется для добавления, изменения или удаления столбца в таблице. Оператор Oracle/PLSQL ALTER TABLE также используется для переименования таблиц.

Добавить столбец в таблицу

Синтаксис

Синтаксис ALTER TABLE для добавления столбца в таблицу Oracle:

Пример

Рассмотрим на примере, как добавить столбец в таблицу Oracle с помощью оператора ALTER TABLE.
Например:

Oracle rename table

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

Hadoop, Data Science, Statistics & others

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.

Oracle rename table output 1

Now we are able to perform the rename statement as follows.

Python TutorialMachine LearningAWSArtificial Intelligence

TableauR ProgrammingPowerBIDeep Learning

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.

Oracle rename table output 2

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.

Oracle rename table output 3

  • 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.

output 4

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.

output 5

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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 –

Rename Oracle Table or View

What is the syntax to rename a table or view in Oracle?

5 Answers 5

In Oracle 10g also:

To rename a table you can use:

or, if owned by another schema:

Interestingly, ALTER VIEW does not support renaming a view. You can, however:

The RENAME command works for tables, views, sequences and private synonyms, for your own schema only.

If the view is not in your schema, you can recompile the view with the new name and then drop the old view.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *