sql-alterview.zh.md 3.1 KB
Newer Older
李少辉-开发者's avatar
李少辉-开发者 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
## 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 VIEW`changes various auxiliary properties of a view. (If you want to modify the view's defining query, use`CREATE OR REPLACE VIEW`.)

You must own the view to use`ALTER VIEW`. To change a view's schema, you must also have`CREATE`privilege 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 have`CREATE`privilege 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 any`INSERT`或者`更新`在为视图应用任何规则或触发器之前,其目标是视图的命令。因此,视图的默认值将优先于基础关系中的任何默认值。

*`新主人`*

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

*`新名字`*

视图的新名称。

*`新模式`*

视图的新架构。

`放 ( *`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 扩展。

## 也可以看看

[创建视图](sql-createview.html),[下拉视图](sql-dropview.html)