Views

A view is a stored selection query and provides a shorthand name for it. CockroachDB's views are dematerialized: they do not store the results of the underlying queries. Instead, the underlying query is executed anew every time the view is used.

Why use views?

There are various reasons to use views, including:

Hide query complexity

When you have a complex query that, for example, joins several tables, or performs complex calculations, you can store the query as a view and then select from the view as you would from a standard table.

Example

Let's say you're using our sample startrek database, which contains two tables, episodes and quotes. There's a foreign key constraint between the episodes.id column and the quotes.episode column. To count the number of famous quotes per season, you could run the following join:

  1. > SELECT startrek.episodes.season, count(*)
  2. FROM startrek.quotes
  3. JOIN startrek.episodes
  4. ON startrek.quotes.episode = startrek.episodes.id
  5. GROUP BY startrek.episodes.season;
  1. +--------+----------+
  2. | season | count(*) |
  3. +--------+----------+
  4. | 2 | 76 |
  5. | 3 | 46 |
  6. | 1 | 78 |
  7. +--------+----------+
  8. (3 rows)

Alternatively, to make it much easier to run this complex query, you could create a view:

  1. > CREATE VIEW startrek.quotes_per_season (season, quotes)
  2. AS SELECT startrek.episodes.season, count(*)
  3. FROM startrek.quotes
  4. JOIN startrek.episodes
  5. ON startrek.quotes.episode = startrek.episodes.id
  6. GROUP BY startrek.episodes.season;
  1. CREATE VIEW

Then, executing the query is as easy as SELECTing from the view:

  1. > SELECT * FROM startrek.quotes_per_season;
  1. +--------+--------+
  2. | season | quotes |
  3. +--------+--------+
  4. | 2 | 76 |
  5. | 3 | 46 |
  6. | 1 | 78 |
  7. +--------+--------+
  8. (3 rows)

Limit access to underlying data

When you do not want to grant a user access to all the data in one or more standard tables, you can create a view that contains only the columns and/or rows that the user should have access to and then grant the user permissions on the view.

Example

Let's say you have a bank database containing an accounts table:

  1. > SELECT * FROM bank.accounts;
  1. +----+----------+---------+-----------------+
  2. | id | type | balance | email |
  3. +----+----------+---------+-----------------+
  4. | 1 | checking | 1000 | max@roach.com |
  5. | 2 | savings | 10000 | max@roach.com |
  6. | 3 | checking | 15000 | betsy@roach.com |
  7. | 4 | checking | 5000 | lilly@roach.com |
  8. | 5 | savings | 50000 | ben@roach.com |
  9. +----+----------+---------+-----------------+
  10. (5 rows)

You want a particular user, bob, to be able to see the types of accounts each user has without seeing the balance in each account, so you create a view to expose just the type and email columns:

  1. > CREATE VIEW bank.user_accounts
  2. AS SELECT type, email
  3. FROM bank.accounts;
  1. CREATE VIEW

You then make sure bob does not have privileges on the underlying bank.accounts table:

  1. > SHOW GRANTS ON bank.accounts;
  1. +----------+------+------------+
  2. | Table | User | Privileges |
  3. +----------+------+------------+
  4. | accounts | root | ALL |
  5. | accounts | toti | SELECT |
  6. +----------+------+------------+
  7. (2 rows)

Finally, you grant bob privileges on the bank.user_accounts view:

  1. > GRANT SELECT ON bank.user_accounts TO bob;

Now, bob will get a permissions error when trying to access the underlying bank.accounts table but will be allowed to query the bank.user_accounts view:

  1. > SELECT * FROM bank.accounts;
  1. pq: user bob does not have SELECT privilege on table accounts
  1. > SELECT * FROM bank.user_accounts;
  1. +----------+-----------------+
  2. | type | email |
  3. +----------+-----------------+
  4. | checking | max@roach.com |
  5. | savings | max@roach.com |
  6. | checking | betsy@roach.com |
  7. | checking | lilly@roach.com |
  8. | savings | ben@roach.com |
  9. +----------+-----------------+
  10. (5 rows)

How views work

Creating views

To create a view, use the CREATE VIEW statement:

  1. > CREATE VIEW bank.user_accounts
  2. AS SELECT type, email
  3. FROM bank.accounts;
  1. CREATE VIEW

Note:

Any selection query is valid as operand to CREATE VIEW, not just simple SELECT clauses.

Listing views

Once created, views are listed alongside regular tables in the database:

  1. > SHOW TABLES FROM bank;
  1. +---------------+
  2. | Table |
  3. +---------------+
  4. | accounts |
  5. | user_accounts |
  6. +---------------+
  7. (2 rows)

To list just views, you can query the views table in the Information Schema:

  1. > SELECT * FROM bank.information_schema.views;
  1. > SELECT * FROM startrek.information_schema.views;
  1. +---------------+-------------------+----------------------+---------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
  2. | table_catalog | table_schema | table_name | view_definition | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into |
  3. +---------------+-------------------+----------------------+---------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
  4. | bank | public | user_accounts | SELECT type, email FROM bank.accounts | NULL | NULL | NULL | NULL | NULL | NULL |
  5. +---------------+-------------------+----------------------+---------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
  6. (1 row)
  7. +---------------+-------------------+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
  8. | table_catalog | table_schema | table_name | view_definition | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into |
  9. +---------------+-------------------+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
  10. | startrek | public | quotes_per_season | SELECT startrek.episodes.season, count(*) FROM startrek.quotes JOIN startrek.episodes ON startrek.quotes.episode = startrek.episodes.id GROUP BY startrek.episodes.season | NULL | NULL | NULL | NULL | NULL | NULL |
  11. +---------------+-------------------+----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
  12. (1 row)

Querying views

To query a view, target it with a table expression, for example using a SELECT clause, just as you would with a stored table:

  1. > SELECT * FROM bank.user_accounts;
  1. +----------+-----------------+
  2. | type | email |
  3. +----------+-----------------+
  4. | checking | max@roach.com |
  5. | savings | max@roach.com |
  6. | checking | betsy@roach.com |
  7. | checking | lilly@roach.com |
  8. | savings | ben@roach.com |
  9. +----------+-----------------+
  10. (5 rows)

SELECTing a view executes the view's stored SELECT statement, which returns the relevant data from the underlying table(s). To inspect the SELECT statement executed by the view, use the SHOW CREATE statement:

  1. > SHOW CREATE bank.user_accounts;
  1. +--------------------+---------------------------------------------------------------------------+
  2. | table_name | create_statement |
  3. +--------------------+---------------------------------------------------------------------------+
  4. | bank.user_accounts | CREATE VIEW "bank.user_accounts" AS SELECT type, email FROM bank.accounts |
  5. +--------------------+---------------------------------------------------------------------------+
  6. (1 row)

You can also inspect the SELECT statement executed by a view by querying the views table in the Information Schema:

  1. > SELECT view_definition FROM bank.information_schema.views WHERE table_name = 'user_accounts';
  1. +----------------------------------------+
  2. | view_definition |
  3. +----------------------------------------+
  4. | SELECT type, email FROM bank.accounts |
  5. +----------------------------------------+
  6. (1 row)

View dependencies

A view depends on the objects targeted by its underlying query. Attempting to rename an object referenced in a view's stored query therefore results in an error:

  1. > ALTER TABLE bank.accounts RENAME TO bank.accts;
  1. pq: cannot rename table "bank.accounts" because view "user_accounts" depends on it

Likewise, attempting to drop an object referenced in a view's stored query results in an error:

  1. > DROP TABLE bank.accounts;
  1. pq: cannot drop table "accounts" because view "user_accounts" depends on it
  1. > ALTER TABLE bank.accounts DROP COLUMN email;
  1. pq: cannot drop column email because view "bank.user_accounts" depends on it

There is an exception to the rule above, however: When dropping a table or dropping a view, you can use the CASCADE keyword to drop all dependent objects as well:

  1. > DROP TABLE bank.accounts CASCADE;
  1. DROP TABLE

Warning:

CASCADE drops all dependent objects without listing them, which can lead to inadvertent and difficult-to-recover losses. To avoid potential harm, we recommend dropping objects individually in most cases.

Renaming views

To rename a view, use the ALTER VIEW statement:

  1. > ALTER VIEW bank.user_accounts RENAME TO bank.user_accts;
  1. RENAME VIEW

It is not possible to change the stored query executed by the view. Instead, you must drop the existing view and create a new view.

Removing views

To remove a view, use the DROP VIEW statement:

  1. > DROP VIEW bank.user_accounts
  1. DROP VIEW

See also

Was this page helpful?
YesNo