重命名 Oracle 表或视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/835387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:50:15  来源:igfitidea点击:

Rename Oracle Table or View

oracletable-rename

提问by Steven

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

在 Oracle 中重命名表或视图的语法是什么?

回答by Quassnoi

ALTER TABLE mytable RENAME TO othertable

In Oracle 10galso:

Oracle 10g还:

RENAME mytable TO othertable

回答by Jeffrey Kemp

To rename a table you can use:

要重命名表,您可以使用:

RENAME mytable TO othertable;

or

或者

ALTER TABLE mytable RENAME TO othertable;

or, if owned by another schema:

或者,如果由另一个架构拥有:

ALTER TABLE owner.mytable RENAME TO othertable;

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

有趣的是,ALTER VIEW 不支持重命名视图。但是,您可以:

RENAME myview TO otherview;

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

RENAME 命令仅适用于您自己的模式的表、视图、序列和私有同义词。

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

如果视图不在您的架构中,您可以使用新名称重新编译视图,然后删除旧视图。

(tested in Oracle 10g)

(在 Oracle 10g 中测试)

回答by Pop

In order to rename a table in a different schema, try:

为了重命名不同模式中的表,请尝试:

ALTER TABLE owner.mytable RENAME TO othertable;

The rename command (as in "rename mytable to othertable") only supports renaming a table in the same schema.

rename 命令(如“ rename mytable to othertable”)仅支持重命名相同模式中的表。

回答by Maurício

One can rename indexesthe same way:

可以用同样的方式重命名索引

alter index owner.index_name rename to new_name;

回答by Wouter

Past 10g the current answer no longer works for renaming views. The only method that still works is dropping and recreating the view. The best way I can think of to do this would be:

过去 10g 当前答案不再适用于重命名视图。唯一仍然有效的方法是删除并重新创建视图。我能想到的最好的方法是:

SELECT TEXT FROM ALL_VIEWS WHERE owner='some_schema' and VIEW_NAME='some_view';

SELECT TEXT FROM ALL_VIEWS WHERE owner='some_schema' and VIEW_NAME='some_view';

Add this in front of the SQL returned

在返回的 SQL 前面加上这个

Create or replace view some_schema.new_view_name as ...

创建或替换视图 some_schema.new_view_name 为 ...

Drop the old view

删除旧视图

Drop view some_schema.some_view;

删除视图 some_schema.some_view;