# ALTER VIEW

ALTER VIEW — change the definition of a view

# Synopsis

ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name SET DEFAULT expression
ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name DROP DEFAULT
ALTER VIEW [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER VIEW [ IF EXISTS ] name RENAME [ COLUMN ] column_name TO new_column_name
ALTER VIEW [ IF EXISTS ] name RENAME TO new_name
ALTER VIEW [ IF EXISTS ] name SET SCHEMA new_schema
ALTER VIEW [ IF EXISTS ] name SET ( view_option_name [= view_option_value] [, ... ] )
ALTER VIEW [ IF EXISTS ] name RESET ( view_option_name [, ... ] )

# Description

ALTER VIEWchanges various auxiliary properties of a view. (If you want to modify the view's defining query, useCREATE OR REPLACE VIEW.)

You must own the view to useALTER VIEW. To change a view's schema, you must also haveCREATEprivilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must haveCREATEprivilege on the view's schema. (These restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the view. However, a superuser can alter ownership of any view anyway.)

# Parameters

name

The name (optionally schema-qualified) of an existing view.

column_name

Name of an existing column.

new_column_name

New name for an existing column.

IF EXISTS

Do not throw an error if the view does not exist. A notice is issued in this case.

SET/DROP DEFAULT

These forms set or remove the default value for a column. A view column's default value is substituted into anyINSERT或者更新在为视图应用任何规则或触发器之前,其目标是视图的命令。因此,视图的默认值将优先于基础关系中的任何默认值。

新主人

视图的新所有者的用户名。

新名字

视图的新名称。

新模式

视图的新架构。

放 ( *view_option_name* [= *view_option_value*] [, ... ])
重置 ( *view_option_name* [, ... ] )

设置或重置视图选项。目前支持的选项有:

check_option(枚举)

更改视图的检查选项。值必须是当地的或者级联.

安全屏障(布尔值)

更改视图的安全屏障属性。该值必须是布尔值,例如真的要么错误的.

# 笔记

由于历史原因,更改表也可以与视图一起使用;但唯一的变体更改表视图允许的与上面显示的相同。

# 例子

重命名视图酒吧

ALTER VIEW foo RENAME TO bar;

要将默认列值附加到可更新视图:

CREATE TABLE base_table (id int, ts timestamptz);
CREATE VIEW a_view AS SELECT * FROM base_table;
ALTER VIEW a_view ALTER COLUMN ts SET DEFAULT now();
INSERT INTO base_table(id) VALUES(1);  -- ts will receive a NULL
INSERT INTO a_view(id) VALUES(2);  -- ts will receive the current time

# 兼容性

改变视图是 SQL 标准的 PostgreSQL 扩展。

# 也可以看看

创建视图,下拉视图