ALTER VIEW语句修改视图的名字。

目前无法更改视图执行的SELECT语句,你必须删除现有视图并创建新视图。 此外,目前无法重命名其他视图所依赖的视图,但在未来可能会添加此功能(see this issue)。

所需权限

用户需要拥有视图的DROP权限和当前数据库的CREATE权限。

概要

参数

Parameter Description
IF EXISTS 仅当视图view_name存在时重命名,如果不存在则返回错误。
view_name 需要重命名的视图名. 使用以下命令查找视图名:

SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';
name 视图的新name , 它必须在数据库中唯一,且遵循identifier rules.

举例

> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';
+---------------+-------------------+--------------------+------------+---------+
| TABLE_CATALOG |   TABLE_SCHEMA    |     TABLE_NAME     | TABLE_TYPE | VERSION |
+---------------+-------------------+--------------------+------------+---------+
| def           | bank              | user_accounts      | VIEW       |       2 |
| def           | bank              | user_emails        | VIEW       |       1 |
+---------------+-------------------+--------------------+------------+---------+
(2 rows)

> ALTER VIEW bank.user_emails RENAME TO bank.user_email_addresses;
RENAME VIEW
> SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';
+---------------+-------------------+----------------------+------------+---------+
| TABLE_CATALOG |   TABLE_SCHEMA    |      TABLE_NAME      | TABLE_TYPE | VERSION |
+---------------+-------------------+----------------------+------------+---------+
| def           | bank              | user_accounts        | VIEW       |       2 |
| def           | bank              | user_email_addresses | VIEW       |       3 |
+---------------+-------------------+----------------------+------------+---------+
(2 rows)

See Also