CREATE VIEW

Synopsis

Use the CREATE VIEW statement to create a new view in a database. It defines the view name and the (select) statement defining it.

Syntax

  1. create_view ::= CREATE [ OR REPLACE ] VIEW qualified_name
  2. [ ( column_list ) ] AS select

create_view

CREATE VIEW - 图1

Semantics

create_view

CREATE [ OR REPLACE ] VIEW qualified_name [ (column_list ) ] AS select

Create a view.

qualified_name

Specify the name of the view. An error is raised if view with that name already exists in the specified database (unless the OR REPLACE option is used).

column_list

Specify a comma-separated list of columns. If not specified, the column names are deduced from the query.

select

Specify a SELECT or VALUES statement that will provide the columns and rows of the view.

Examples

Create a sample table.

  1. CREATE TABLE sample(k1 int, k2 int, v1 int, v2 text, PRIMARY KEY (k1, k2));

Insert some rows.

  1. INSERT INTO sample(k1, k2, v1, v2) VALUES (1, 2.0, 3, 'a'), (2, 3.0, 4, 'b'), (3, 4.0, 5, 'c');

Create a view on the sample table.

  1. CREATE VIEW sample_view AS SELECT * FROM sample WHERE v2 != 'b' ORDER BY k1 DESC;

Select from the view.

  1. yugabyte=# SELECT * FROM sample_view;
  1. k1 | k2 | v1 | v2
  2. ----+----+----+----
  3. 3 | 4 | 5 | c
  4. 1 | 2 | 3 | a
  5. (2 rows)

See also