Skip to content

Commit c921ac4

Browse files
Add CREATE VIEW documentation (#4208)
Adds a `CREATE VIEW` reference page to the Sphinx docs, covering syntax, parameters, examples (basic, nested, join, CTE, forward references, indexes on views), and limitations. Also updates `TABLE`, `FUNCTION`, `INDEX`, and `SCHEMA_TEMPLATE` pages with cross-references, and fixes the misplaced `.. _create-schema-template:` label that caused broken `ref` warnings.
1 parent f5b3efd commit c921ac4

7 files changed

Lines changed: 179 additions & 5 deletions

File tree

docs/sphinx/source/reference/sql_commands/DDL/CREATE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ CREATE
1313
CREATE/INDEX
1414
CREATE/FUNCTION
1515
CREATE/TEMPORARY_FUNCTION
16+
CREATE/VIEW

docs/sphinx/source/reference/sql_commands/DDL/CREATE/FUNCTION.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,4 @@ See Also
308308

309309
* :ref:`Subqueries <subqueries>` - Using functions in subqueries
310310
* :ref:`Joins <joins>` - Using functions in join operations
311+
* :ref:`CREATE VIEW <create-view>` - Views: reusable queries without parameters

docs/sphinx/source/reference/sql_commands/DDL/CREATE/INDEX.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ ORDER BY clause:
279279
- Expressions in GROUP BY are supported
280280
- WHERE clauses can be used with aggregate indexes for filtered aggregates
281281

282+
.. _index-on-syntax:
283+
282284
INDEX ON Syntax
283285
===============
284286

@@ -361,6 +363,21 @@ First define a view with the filter:
361363
362364
CREATE INDEX idx_expensive_products ON v_expensive_products(price)
363365
366+
**Index on View (Array Unnesting)**
367+
368+
A common pattern is to create a view that unnests an array column, then index the result. This creates a self-contained schema template with the table, view, and index together:
369+
370+
.. code-block:: sql
371+
372+
CREATE SCHEMA TEMPLATE my_template
373+
CREATE TABLE products (
374+
id BIGINT,
375+
tags STRING ARRAY,
376+
PRIMARY KEY(id))
377+
CREATE VIEW product_tags AS
378+
SELECT SQ.tag FROM products AS p, (SELECT tag FROM p.tags AS tag) AS SQ
379+
CREATE INDEX idx_tags ON product_tags (tag)
380+
364381
**Custom Ordering**
365382

366383
.. code-block:: sql

docs/sphinx/source/reference/sql_commands/DDL/CREATE/SCHEMA_TEMPLATE.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
.. _create-schema-template:
2+
13
======================
24
CREATE SCHEMA TEMPLATE
35
======================
46

5-
.. _create-schema-template:
6-
7-
A schema template is a predefined structure that defines the set of ``TABLE`` and ``INDEX`` definitions. This can then
8-
used as a blueprint to create ``SCHEMA`` in a database. See: :doc:`../../../Databases_Schemas_SchemaTemplates` for more
9-
information.
7+
A schema template is a predefined structure that defines a set of ``TABLE``, ``INDEX``, ``VIEW``, ``TYPE``, and
8+
``FUNCTION`` definitions. It can then be used as a blueprint to create ``SCHEMA`` instances in a database.
9+
See: :doc:`../../../Databases_Schemas_SchemaTemplates` for more information.
1010

1111
Syntax
1212
======

docs/sphinx/source/reference/sql_commands/DDL/CREATE/TABLE.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,9 @@ Attempting to insert a second row in a :sql:`SINGLE ROW ONLY` table will result
109109
* - :sql:`NULL`
110110
- :json:`0.0`
111111
- :json:`"X"`
112+
113+
See Also
114+
========
115+
116+
* :ref:`CREATE VIEW <create-view>` — Virtual (non-materialized) tables defined by a query
117+
* :doc:`INDEX` — Defining indexes on tables and views
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Diagram(
2+
Sequence(
3+
Terminal('CREATE'),
4+
Terminal('VIEW'),
5+
NonTerminal('viewName'),
6+
Terminal('AS'),
7+
NonTerminal('query')
8+
)
9+
)
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
===========
2+
CREATE VIEW
3+
===========
4+
5+
.. _create-view:
6+
7+
Clause in a :ref:`schema template definition <create-schema-template>` to create a non-materialized view. A view is a virtual table whose contents are defined by a SQL query. Views do not store data themselves; each query against a view executes the underlying query against the base tables. Views are read-only and cannot be the target of ``INSERT``, ``UPDATE``, or ``DELETE`` statements. ``CREATE VIEW`` is a clause within a schema template and cannot be a standalone statement. View names must not collide with table, type, or function names in the same schema template.
8+
9+
Syntax
10+
======
11+
12+
.. raw:: html
13+
:file: VIEW.diagram.svg
14+
15+
Parameters
16+
==========
17+
18+
``viewName``
19+
The name of the view. Must be unique within the schema template — cannot collide with table, type, or other view names.
20+
21+
``query``
22+
The SQL ``SELECT`` statement that defines the view. The query can reference tables, other views, and functions defined in the same schema template.
23+
24+
Examples
25+
========
26+
27+
Setup
28+
-----
29+
30+
``CREATE VIEW`` must appear inside a :ref:`schema template <create-schema-template>` definition and cannot be a standalone statement. For the examples on this page, assume the following schema template:
31+
32+
.. code-block:: sql
33+
34+
CREATE SCHEMA TEMPLATE my_template
35+
CREATE TABLE employees (
36+
id BIGINT,
37+
name STRING,
38+
dept STRING,
39+
salary BIGINT,
40+
PRIMARY KEY(id))
41+
42+
Basic View
43+
----------
44+
45+
Create a view that filters rows from a base table:
46+
47+
.. code-block:: sql
48+
49+
CREATE VIEW engineering AS
50+
SELECT id, name, salary
51+
FROM employees
52+
WHERE dept = 'Engineering'
53+
54+
Query the view like a table:
55+
56+
.. code-block:: sql
57+
58+
SELECT * FROM engineering
59+
60+
.. list-table::
61+
:header-rows: 1
62+
63+
* - :sql:`id`
64+
- :sql:`name`
65+
- :sql:`salary`
66+
* - :json:`1`
67+
- :json:`"Alice"`
68+
- :json:`100000`
69+
* - :json:`2`
70+
- :json:`"Bob"`
71+
- :json:`110000`
72+
73+
Nested Views
74+
------------
75+
76+
Views can reference other views. The following creates a second view on top of the first:
77+
78+
.. code-block:: sql
79+
80+
CREATE VIEW engineering AS
81+
SELECT id, name, salary
82+
FROM employees
83+
WHERE dept = 'Engineering'
84+
85+
CREATE VIEW high_earners AS
86+
SELECT id, name
87+
FROM engineering
88+
WHERE salary > 100000
89+
90+
.. code-block:: sql
91+
92+
SELECT * FROM high_earners
93+
94+
.. list-table::
95+
:header-rows: 1
96+
97+
* - :sql:`id`
98+
- :sql:`name`
99+
* - :json:`2`
100+
- :json:`"Bob"`
101+
102+
View with JOIN
103+
--------------
104+
105+
Views support joins, including self-joins (see :ref:`inner_join` for join syntax):
106+
107+
.. code-block:: sql
108+
109+
CREATE VIEW peer_pairs AS
110+
SELECT A.name AS emp1, B.name AS emp2
111+
FROM employees A, employees B
112+
WHERE A.dept = B.dept AND A.id < B.id
113+
114+
View with CTE
115+
-------------
116+
117+
Views can use Common Table Expressions (see :doc:`WITH` for CTE syntax):
118+
119+
.. code-block:: sql
120+
121+
CREATE VIEW senior_engineering AS
122+
WITH filtered AS (
123+
SELECT id, name, salary
124+
FROM employees
125+
WHERE dept = 'Engineering' AND salary > 100000
126+
)
127+
SELECT * FROM filtered
128+
129+
Indexes on Views
130+
----------------
131+
132+
Indexes can be defined on views using the standard ``CREATE INDEX`` syntax. For a self-contained example including array unnesting, see :ref:`index-on-syntax` in :doc:`INDEX`.
133+
134+
See Also
135+
========
136+
137+
* :ref:`create-schema-template` — Schema templates and their clauses
138+
* :doc:`TABLE` — Defining tables within a schema template
139+
* :doc:`INDEX` — Defining indexes, including :ref:`index-on-syntax` for indexes on views
140+
* :doc:`FUNCTION` — User-defined functions

0 commit comments

Comments
 (0)