バージョンごとのドキュメント一覧

CREATE VIEW

CREATE VIEW <refpurpose>define a new view</refpurpose> — 新しいビューを定義する

概要

CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW name [ ( column_name [, ...] ) ]
    [ WITH ( view_option_name [= view_option_value] [, ... ] ) ]
    AS query
    [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]

説明

<title>Description</title>

<command>CREATE VIEW</command> defines a view of a query. The view is not physically materialized. Instead, the query is run every time the view is referenced in a query. CREATE VIEWは問い合わせによるビューを定義します。 ビューは物理的な実体として存在するものではありません。 その代わり、問い合わせでビューが参照される度に、指定された問い合わせが実行されます。

<command>CREATE OR REPLACE VIEW</command> is similar, but if a view of the same name already exists, it is replaced. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list. The calculations giving rise to the output columns may be completely different. CREATE OR REPLACE VIEWも同様の働きをしますが、 このコマンドでは、同じ名前のビューが既に存在している場合、そのビューを置き換えます。 新しい問い合わせは、既存のビュー問い合わせが生成する列と同じ列(つまり、同じ順序の同じデータ型の同じ列名)を生成しなければなりません。 しかし、そのリストの最後に列を追加しても構いません。 出力列を生成する計算をまったく異なるものにしても構いません。

If a schema name is given (for example, <literal>CREATE VIEW myschema.myview ...</literal>) then the view is created in the specified schema. Otherwise it is created in the current schema. Temporary views exist in a special schema, so a schema name cannot be given when creating a temporary view. The name of the view must be distinct from the name of any other relation (table, sequence, index, view, materialized view, or foreign table) in the same schema. スキーマ名が付けられている場合(例えば、CREATE VIEW myschema.myview ...)、ビューは指定されたスキーマに作成されます。 スキーマ名がなければ、そのビューは現在のスキーマに作成されます。 一時ビューは特別なスキーマに作成されます。 そのため、一時ビューを作成する時にはスキーマ名を付けることはできません。 ビュー名は、同じスキーマ内の他のリレーション(テーブル、シーケンス、インデックス、ビュー、マテリアライズドビュー、外部テーブル)とは異なる名前である必要があります。

パラメータ

<title>Parameters</title>
TEMPORARYまたはTEMP

If specified, the view is created as a temporary view. Temporary views are automatically dropped at the end of the current session. Existing permanent relations with the same name are not visible to the current session while the temporary view exists, unless they are referenced with schema-qualified names. これが指定された場合、ビューは一時ビューとして作成されます。 現在のセッションが終わった時、一時ビューは自動的に削除されます。 一時ビューが存在する間、現在のセッションでは、これと同じ名前の永続リレーションはスキーマ修飾した名前で参照していない限り不可視です。

If any of the tables referenced by the view are temporary, the view is created as a temporary view (whether <literal>TEMPORARY</literal> is specified or not). ビューで参照されるテーブルの一部が一時テーブルであった場合、(TEMPORARYの指定があってもなくても)ビューは一時ビューとして作成されます。

RECURSIVE

Creates a recursive view. The syntax 再帰的ビューを作成します。

CREATE RECURSIVE VIEW [ schema . ] view_name (column_names) AS SELECT ...;

is equivalent to という構文は

CREATE VIEW [ schema . ] view_name AS WITH RECURSIVE view_name (column_names) AS (SELECT ...) SELECT column_names FROM view_name;

と同等です。 A view column name list must be specified for a recursive view. 再帰的ビューではビューの列名リストを指定する必要があります。

name

The name (optionally schema-qualified) of a view to be created. 作成するビューの名前です(スキーマ修飾名も可)。

column_name

An optional list of names to be used for columns of the view. If not given, the column names are deduced from the query. ビューの列名として使用する名前のリストで、省略可能です。省略された場合、問い合わせに由来する名前が使用されます。

WITH ( view_option_name [= view_option_value] [, ... ] )

This clause specifies optional parameters for a view; the following parameters are supported: この句はビュー用のオプションのパラメータを指定します。 以下のパラメータがサポートされています。

check_option (enum)

This parameter may be either <literal>local</literal> or <literal>cascaded</literal>, and is equivalent to specifying <literal>WITH [ CASCADED | LOCAL ] CHECK OPTION</literal> (see below). このパラメータはlocalcascadedのいずれかで、WITH [ CASCADED | LOCAL ] CHECK OPTIONを指定するのと同じです(以下を参照)。

security_barrier (boolean)

This should be used if the view is intended to provide row-level security. See <xref linkend="rules-privileges"/> for full details. 行単位セキュリティを提供することを意図したビューでは、これを有効にしなければなりません。 詳細については41.5を参照してください。

security_invoker (boolean)

This option causes the underlying base relations to be checked against the privileges of the user of the view rather than the view owner. See the notes below for full details. このオプションを選択すると、基となる基底リレーションが、ビューの所有者ではなくビューのユーザーの権限に対してチェックされます。 詳細については、以下の注釈を参照してください。

All of the above options can be changed on existing views using <link linkend="sql-alterview"><command>ALTER VIEW</command></link>. 上記のオプションはすべて、ALTER VIEWを使用して既存のビューで変更できます。

query

A <link linkend="sql-select"><command>SELECT</command></link> or <link linkend="sql-values"><command>VALUES</command></link> command which will provide the columns and rows of the view. ビューの列と行を生成するSELECTまたはVALUESコマンドです。

WITH [ CASCADED | LOCAL ] CHECK OPTION

This option controls the behavior of automatically updatable views. When this option is specified, <command>INSERT</command> and <command>UPDATE</command> commands on the view will be checked to ensure that new rows satisfy the view-defining condition (that is, the new rows are checked to ensure that they are visible through the view). If they are not, the update will be rejected. If the <literal>CHECK OPTION</literal> is not specified, <command>INSERT</command> and <command>UPDATE</command> commands on the view are allowed to create rows that are not visible through the view. The following check options are supported: このオプションは、自動的に更新可能なビューの動作を制御します。 このオプションが指定された場合、ビューに対するINSERTおよびUPDATEコマンドでは、新しい行がビュー定義の条件を満たすことが検査されます(つまり、新しい行がビューで見ることができるかどうか、検査されます)。 条件を満たさない場合、更新は拒絶されます。 CHECK OPTIONが指定されない場合、ビューに対するINSERTおよびUPDATEコマンドは、ビューで見ることができない行を作ることができます。 以下のcheck optionがサポートされます。

LOCAL

New rows are only checked against the conditions defined directly in the view itself. Any conditions defined on underlying base views are not checked (unless they also specify the <literal>CHECK OPTION</literal>). 新しい行は、そのビュー自体に直接定義されている条件に対してのみ検査されます。 ビューが基にするビューについて定義されている条件は、(それらもCHECK OPTIONを指定しているのでなければ)検査されません。

CASCADED

New rows are checked against the conditions of the view and all underlying base views. If the <literal>CHECK OPTION</literal> is specified, and neither <literal>LOCAL</literal> nor <literal>CASCADED</literal> is specified, then <literal>CASCADED</literal> is assumed. 新しい行は、そのビュー、およびそれが基にするすべてのビューの条件に対して検査されます。 CHECK OPTIONが指定され、LOCALCASCADEDも指定されていないときは、CASCADEDが指定されたとみなされます。

The <literal>CHECK OPTION</literal> may not be used with <literal>RECURSIVE</literal> views. CHECK OPTIONRECURSIVEなビューで使うことはできません。

Note that the <literal>CHECK OPTION</literal> is only supported on views that are automatically updatable, and do not have <literal>INSTEAD OF</literal> triggers or <literal>INSTEAD</literal> rules. If an automatically updatable view is defined on top of a base view that has <literal>INSTEAD OF</literal> triggers, then the <literal>LOCAL CHECK OPTION</literal> may be used to check the conditions on the automatically updatable view, but the conditions on the base view with <literal>INSTEAD OF</literal> triggers will not be checked (a cascaded check option will not cascade down to a trigger-updatable view, and any check options defined directly on a trigger-updatable view will be ignored). If the view or any of its base relations has an <literal>INSTEAD</literal> rule that causes the <command>INSERT</command> or <command>UPDATE</command> command to be rewritten, then all check options will be ignored in the rewritten query, including any checks from automatically updatable views defined on top of the relation with the <literal>INSTEAD</literal> rule. CHECK OPTIONは、自動更新可能で、かつINSTEAD OFトリガーもINSTEADルールもないビューについてのみサポートされていることに注意してください。 自動更新可能ビューがINSTEAD OFトリガーのあるビューに基づいて定義されている場合、LOCAL CHECK OPTIONを使って自動更新可能ビューの条件を検査することはできますが、INSTEAD OFトリガーを持つ基のビューの条件は検査されません(cascaded check optionはトリガーで更新されるビューにまでは伝わらず、またトリガーで更新可能なビューに直接定義されたcheck optionは無視されます)。 ビューあるいはその基となるリレーションにINSTEADルールがあり、INSERTあるいはUPDATEの書き換えが生じる場合、その書き換えられたクエリでは(INSTEADルールのあるリレーションに基づく自動更新可能ビューのものも含めて)すべてのcheck optionが無視されます。

注釈

<title>Notes</title>

Use the <link linkend="sql-dropview"><command>DROP VIEW</command></link> statement to drop views. ビューを削除するには、DROP VIEW文を使用してください。

Be careful that the names and types of the view's columns will be assigned the way you want. For example: ビューの列の名前と型は指定通りに割り当てられることに注意してください。 例えば、次のコマンドを見てください。

CREATE VIEW vista AS SELECT 'Hello World';

is bad form because the column name defaults to <literal>?column?</literal>; also, the column data type defaults to <type>text</type>, which might not be what you wanted. Better style for a string literal in a view's result is something like: この例は列の名前がデフォルトの?column?になるので好ましくありません。 また、列のデータ型もデフォルトのtextになりますが、これは求めるものと違うかもしれません。 ビューの結果として文字リテラルを返したい場合は、次のように指定するのがよりよい方法です。

CREATE VIEW vista AS SELECT text 'Hello World' AS hello;

By default, access to the underlying base relations referenced in the view is determined by the permissions of the view owner. In some cases, this can be used to provide secure but restricted access to the underlying tables. However, not all views are secure against tampering; see <xref linkend="rules-privileges"/> for details. デフォルトでは、ビューで参照される基となる基底リレーションへのアクセス権は、ビューの所有者の権限によって決定されます。 場合によっては、これを使用して基となるテーブルへの安全だが制限されたアクセスを提供できます。 しかしすべてのビューが不正な改変に対して安全というわけではありません。詳細は41.5を参照してください。

If the view has the <literal>security_invoker</literal> property set to <literal>true</literal>, access to the underlying base relations is determined by the permissions of the user executing the query, rather than the view owner. Thus, the user of a security invoker view must have the relevant permissions on the view and its underlying base relations. ビューのsecurity_invoker属性がtrueに設定されている場合、基となる基底リレーションへのアクセス権は、ビューの所有者ではなく、問い合わせを実行するユーザーの権限によって決定されます。 したがって、セキュリティ実行者ビューのユーザーは、ビューおよび基となる基底リレーションに対する適切な権限を持っている必要があります。

If any of the underlying base relations is a security invoker view, it will be treated as if it had been accessed directly from the original query. Thus, a security invoker view will always check its underlying base relations using the permissions of the current user, even if it is accessed from a view without the <literal>security_invoker</literal> property. 基となる基底リレーションのいずれかがセキュリティ実行者ビューである場合、元の問い合わせから直接アクセスされたものとして処理されます。 したがって、セキュリティ実行者ビューは、security_invoker属性なしのビューからアクセスされた場合でも、常に現行ユーザーの権限を使用して基となる基底リレーションをチェックします。

If any of the underlying base relations has <link linkend="ddl-rowsecurity">row-level security</link> enabled, then by default, the row-level security policies of the view owner are applied, and access to any additional relations referred to by those policies is determined by the permissions of the view owner. However, if the view has <literal>security_invoker</literal> set to <literal>true</literal>, then the policies and permissions of the invoking user are used instead, as if the base relations had been referenced directly from the query using the view. 基となる基底リレーションのいずれかで行レベルセキュリティが有効になっている場合、デフォルトでは、ビューの所有者の行レベルセキュリティポリシーが適用され、これらのポリシーによって参照される追加のリレーションへのアクセスは、ビュー所有者の権限によって決定されます。 ただし、ビューのsecurity_invokertrueに設定されている場合は、基底リレーションがそのビューを使用した問い合わせから直接参照されているかのように、かわりに実行ユーザーのポリシーと権限が使用されます。

Functions called in the view are treated the same as if they had been called directly from the query using the view. Therefore, the user of a view must have permissions to call all functions used by the view. Functions in the view are executed with the privileges of the user executing the query or the function owner, depending on whether the functions are defined as <literal>SECURITY INVOKER</literal> or <literal>SECURITY DEFINER</literal>. Thus, for example, calling <literal>CURRENT_USER</literal> directly in a view will always return the invoking user, not the view owner. This is not affected by the view's <literal>security_invoker</literal> setting, and so a view with <literal>security_invoker</literal> set to <literal>false</literal> is <emphasis>not</emphasis> equivalent to a <literal>SECURITY DEFINER</literal> function and those concepts should not be confused. ビューで呼び出された関数は、ビューを使用する問い合わせから直接呼び出された場合と同様に処理されます。 したがって、ビューのユーザーは、ビューで使用されるすべての関数を呼び出す権限を持っている必要があります。 ビュー内の関数は、関数がSECURITY INVOKERまたはSECURITY DEFINERとして定義されているかどうかに応じて、問い合わせを実行するユーザーまたは関数の所有者の権限で実行されます。 したがって、たとえば、ビューでCURRENT_USERを直接呼び出すと、ビューの所有者ではなく常に実行ユーザーを返します。 これはビューのsecurity_invoker設定の影響を受けません。 したがって、security_invokerfalseに設定されているビューはSECURITY DEFINER関数と同等ではなく、これらの概念を混同しないでください。

The user creating or replacing a view must have <literal>USAGE</literal> privileges on any schemas referred to in the view query, in order to look up the referenced objects in those schemas. Note, however, that this lookup only happens when the view is created or replaced. Therefore, the user of the view only requires the <literal>USAGE</literal> privilege on the schema containing the view, not on the schemas referred to in the view query, even for a security invoker view. ビューを作成または置換するユーザーは、スキーマ内の参照オブジェクトを検索するために、ビュー問い合わせで参照されるスキーマに対するUSAGE権限を持っている必要があります。 ただし、この参照は、ビューが作成または置換された場合にのみ行なわれることに注意してください。 したがって、ビューのユーザーは、ビューを含むスキーマに対するUSAGE権限のみを必要とし、セキュリティ実行者ビューの場合でも、ビュー問い合わせで参照されるスキーマに対するUSAGE権限を必要としません。

When <command>CREATE OR REPLACE VIEW</command> is used on an existing view, only the view's defining SELECT rule, plus any <literal>WITH ( ... )</literal> parameters and its <literal>CHECK OPTION</literal> are changed. Other view properties, including ownership, permissions, and non-SELECT rules, remain unchanged. You must own the view to replace it (this includes being a member of the owning role). CREATE OR REPLACE VIEWが既存のビューに対して使用されると、ビューを定義するSELECTルール、WITH ( ... )パラメータ、CHECK OPTIONのみが変更されます。 所有者、権限、SELECT以外のルールなど他のビューの属性はそのまま変更されません。 置き換えるためにはビューの所有者(所有ロールのメンバである場合も含む)でなければなりません。

更新可能ビュー

<title>Updatable Views</title>

Simple views are automatically updatable: the system will allow <command>INSERT</command>, <command>UPDATE</command> and <command>DELETE</command> statements to be used on the view in the same way as on a regular table. A view is automatically updatable if it satisfies all of the following conditions: 簡単なビューは自動更新可能になります。 システムは、ビューに対するINSERTUPDATEDELETE文を通常のテーブルの場合と同じ方法で使用できるようにします。 以下の条件のすべてを満たす場合に、ビューは自動更新可能になります。

  • The view must have exactly one entry in its <literal>FROM</literal> list, which must be a table or another updatable view. ビューのFROMリストには正確に1つだけの項目を持たなければならず、それはテーブルまたは他の更新可能ビューでなければなりません。

  • The view definition must not contain <literal>WITH</literal>, <literal>DISTINCT</literal>, <literal>GROUP BY</literal>, <literal>HAVING</literal>, <literal>LIMIT</literal>, or <literal>OFFSET</literal> clauses at the top level. ビューの定義の最上位レベルにおいてWITHDISTINCTGROUP BYHAVINGLIMITOFFSETを含めてはなりません。

  • The view definition must not contain set operations (<literal>UNION</literal>, <literal>INTERSECT</literal> or <literal>EXCEPT</literal>) at the top level. ビューの定義の最上位レベルにおいて集合操作(UNIONINTERSECTEXCEPT)を含めてはなりません。

  • The view's select list must not contain any aggregates, window functions or set-returning functions. ビューの選択リストに、集約関数、ウィンドウ関数、集合を返す関数を含めてはなりません。

An automatically updatable view may contain a mix of updatable and non-updatable columns. A column is updatable if it is a simple reference to an updatable column of the underlying base relation; otherwise the column is read-only, and an error will be raised if an <command>INSERT</command> or <command>UPDATE</command> statement attempts to assign a value to it. 自動更新可能ビューでは、更新可能な列と更新不可能な列を混在させることができます。 基になるリレーションの更新可能な列を単純に参照する列は更新可能です。 そうでなければ列は更新不可能で、INSERTあるいはUPDATE文でその列に値を設定しようとしたらエラーが発生します。

If the view is automatically updatable the system will convert any <command>INSERT</command>, <command>UPDATE</command> or <command>DELETE</command> statement on the view into the corresponding statement on the underlying base relation. <command>INSERT</command> statements that have an <literal>ON CONFLICT UPDATE</literal> clause are fully supported. ビューが自動更新可能であれば、システムはビューに対するINSERTUPDATEまたはDELETE文を基となるベースリレーションへの対応する文に変換します。 ON CONFLICT UPDATE句を持つINSERT文は完全にサポートされます。

If an automatically updatable view contains a <literal>WHERE</literal> condition, the condition restricts which rows of the base relation are available to be modified by <command>UPDATE</command> and <command>DELETE</command> statements on the view. However, an <command>UPDATE</command> is allowed to change a row so that it no longer satisfies the <literal>WHERE</literal> condition, and thus is no longer visible through the view. Similarly, an <command>INSERT</command> command can potentially insert base-relation rows that do not satisfy the <literal>WHERE</literal> condition and thus are not visible through the view (<literal>ON CONFLICT UPDATE</literal> may similarly affect an existing row not visible through the view). The <literal>CHECK OPTION</literal> may be used to prevent <command>INSERT</command> and <command>UPDATE</command> commands from creating such rows that are not visible through the view. 自動更新可能ビューがWHERE条件を持つ場合、 ベースリレーションのどの行をビューに対するUPDATEDELETE文により変更可能かをその条件が制限します。 しかしUPDATEによる行の変更の結果WHEREを満たさなくなり、その結果、ビューからは参照することができなくなることがあります。 同様にINSERTコマンドはWHERE条件を満たさず、そのためビューを通して参照することができない行をベースリレーションに挿入する可能性があります(ON CONFLICT UPDATEはビューを通して見えない既存の行に同様に影響を及ぼすかもしれません)。 CHECK OPTIONINSERTUPDATEがビューで見ることができない行を作るのを防ぐために使うことができます。

If an automatically updatable view is marked with the <literal>security_barrier</literal> property then all the view's <literal>WHERE</literal> conditions (and any conditions using operators which are marked as <literal>LEAKPROOF</literal>) will always be evaluated before any conditions that a user of the view has added. See <xref linkend="rules-privileges"/> for full details. Note that, due to this, rows which are not ultimately returned (because they do not pass the user's <literal>WHERE</literal> conditions) may still end up being locked. <command>EXPLAIN</command> can be used to see which conditions are applied at the relation level (and therefore do not lock rows) and which are not. 自動更新可能ビューがsecurity_barrier属性を持つ場合、ビューのすべてのWHERE条件(およびLEAKPROOFの演算子を使ったすべての条件)が、必ず、ビューのユーザーが追加した条件より前に評価されます。 詳細は41.5を参照してください。 このため、最終的には(ユーザーのWHERE条件を満たさないために)戻されない行もロックされてしまう場合があることに注意してください。 EXPLAINを使って、リレーションのレベルでどの条件が使われ(その結果、行をロックしない)、どの条件が使われないかを調べることができます。

A more complex view that does not satisfy all these conditions is read-only by default: the system will not allow an insert, update, or delete on the view. You can get the effect of an updatable view by creating <literal>INSTEAD OF</literal> triggers on the view, which must convert attempted inserts, etc. on the view into appropriate actions on other tables. For more information see <xref linkend="sql-createtrigger"/>. Another possibility is to create rules (see <xref linkend="sql-createrule"/>), but in practice triggers are easier to understand and use correctly. これらの条件をすべて満たさないより複雑なビューはデフォルトで読み取り専用です。 システムはビューに対する挿入、更新、削除を許可しません。 ビューに対するINSTEAD OFトリガを作成することで、更新可能ビューの効果を得ることができます。 このトリガはビューに対する挿入試行などを他のテーブルに対する適切な操作に変換するものでなければなりません。 詳細についてはCREATE TRIGGERを参照してください。 他にもルールを作成する(CREATE RULE参照)ことでも実現できますが、実際にはトリガの方が理解しやすく正しく使用するのが容易です。

Note that the user performing the insert, update or delete on the view must have the corresponding insert, update or delete privilege on the view. In addition, by default, the view's owner must have the relevant privileges on the underlying base relations, whereas the user performing the update does not need any permissions on the underlying base relations (see <xref linkend="rules-privileges"/>). However, if the view has <literal>security_invoker</literal> set to <literal>true</literal>, the user performing the update, rather than the view owner, must have the relevant privileges on the underlying base relations. ビューに対する挿入、更新、削除を行うユーザーは、ビューに対して対応する挿入、更新、削除権限を持たなければならないことに注意してください。 さらに、デフォルトでは、ビューの所有者は基となる基底リレーションに対する適切な権限を持たなければならないのに対して、更新を行なうユーザーは基となる基底リレーションに対する権限をまったく必要としません(41.5参照)。 しかし、ビューのsecurity_invokertrueに設定されていれば、ビューの所有者ではなく更新を行なうユーザーが基となる基底リレーションに対する適切な権限を持たなければなりません。

<title>Examples</title>

Create a view consisting of all comedy films: 全てのコメディ映画(Comedy films)からなるビューを作成します。

CREATE VIEW comedies AS
    SELECT *
    FROM films
    WHERE kind = 'Comedy';

This will create a view containing the columns that are in the <literal>film</literal> table at the time of view creation. Though <literal>*</literal> was used to create the view, columns added later to the table will not be part of the view. これはビューを作成した時点でfilmテーブル内にある列を持つビューを作成します。 ビューを作成するために*が使用されていますが、その後にテーブルに追加された列はビューには含まれません。

Create a view with <literal>LOCAL CHECK OPTION</literal>: LOCAL CHECK OPTIONを使ってビューを作成します。

CREATE VIEW universal_comedies AS
    SELECT *
    FROM comedies
    WHERE classification = 'U'
    WITH LOCAL CHECK OPTION;

This will create a view based on the <literal>comedies</literal> view, showing only films with <literal>kind = 'Comedy'</literal> and <literal>classification = 'U'</literal>. Any attempt to <command>INSERT</command> or <command>UPDATE</command> a row in the view will be rejected if the new row doesn't have <literal>classification = 'U'</literal>, but the film <literal>kind</literal> will not be checked. これはcomediesビューに基づくビューを作成し、kind = 'Comedy'かつclassification = 'U'である映画だけを表示します。 このビューでの行のINSERTUPDATEは、classification = 'U'でなければ拒絶されますが、映画のkindは検査されません。

Create a view with <literal>CASCADED CHECK OPTION</literal>: CASCADED CHECK OPTIONでビューを作成します。

CREATE VIEW pg_comedies AS
    SELECT *
    FROM comedies
    WHERE classification = 'PG'
    WITH CASCADED CHECK OPTION;

This will create a view that checks both the <literal>kind</literal> and <literal>classification</literal> of new rows. これは新しい行についてkindclassificationの両方を検査するビューを作成します。

Create a view with a mix of updatable and non-updatable columns: 更新可能な列と更新不可能な列が混在するビューを作成します。

CREATE VIEW comedies AS
    SELECT f.*,
           country_code_to_name(f.country_code) AS country,
           (SELECT avg(r.rating)
            FROM user_ratings r
            WHERE r.film_id = f.id) AS avg_rating
    FROM films f
    WHERE f.kind = 'Comedy';

This view will support <command>INSERT</command>, <command>UPDATE</command> and <command>DELETE</command>. All the columns from the <literal>films</literal> table will be updatable, whereas the computed columns <literal>country</literal> and <literal>avg_rating</literal> will be read-only. このビューはINSERTUPDATEDELETEをサポートします。 filmsテーブルからのすべての列は更新可能ですが、計算される列countryavg_ratingは更新できません。

Create a recursive view consisting of the numbers from 1 to 100: 1から100までの数からなる再帰的ビューを作成します。

CREATE RECURSIVE VIEW public.nums_1_100 (n) AS
    VALUES (1)
UNION ALL
    SELECT n+1 FROM nums_1_100 WHERE n < 100;

Notice that although the recursive view's name is schema-qualified in this <command>CREATE</command>, its internal self-reference is not schema-qualified. This is because the implicitly-created CTE's name cannot be schema-qualified. 上記のCREATEにおいて再帰的ビューの名前はスキーマ修飾されていますが、その内側の自己参照はスキーマ修飾されていないことに注意してください。 これは、暗黙的に作成されるCTEの名前はスキーマ修飾できないからです。

互換性

<title>Compatibility</title>

<command>CREATE OR REPLACE VIEW</command> is a <productname>PostgreSQL</productname> language extension. So is the concept of a temporary view. The <literal>WITH ( ... )</literal> clause is an extension as well, as are security barrier views and security invoker views. CREATE OR REPLACE VIEWPostgreSQLの言語拡張です。 一時ビューという概念も言語拡張です。 WITH ( ... )句も拡張ですし、セキュリティバリアビューとセキュリティ実行者ビューも同様です。

関連項目

<title>See Also</title> ALTER VIEW, DROP VIEW, CREATE MATERIALIZED VIEW