---
title: "CREATE, ALTER, DROP and RENAME VIEW"
description: "Define CoraDB views: CREATE OR REPLACE VIEW, WITH CHECK OPTION, updatable views, ALTER VIEW, DROP VIEW and RENAME VIEW."
url: "https://www.coradb.ai/manual/sql/schema/view_stmt.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/schema/view_stmt.md"
product: "CoraDB"
version: "1.0"
keywords: "create view, create or replace view, alter view, drop view, rename view, updatable view, with check option, view comment, coradb view"
---

# CREATE, ALTER, DROP and RENAME VIEW

A view is a named query. It stores no data of its own: every time you select from it, CoraDB runs its query. Views give a stable name to a frequently used query, hide columns, and let users read data without access to the underlying tables. `VIEW` and `VCLASS` are synonyms.

> **Try it yourself:**
> The examples run on the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html) and show the real results. Start corasql with `corasql --no-auto-commit -u dba e-commerce` and finish with `ROLLBACK;` to remove the views again.


## CREATE VIEW

```text
CREATE [OR REPLACE] {VIEW | VCLASS} view_name
    [(view_column_name [COMMENT 'comment'], ...)]
    AS <select_statement>
    [WITH CHECK OPTION]
    [COMMENT [=] 'comment']
```

- **OR REPLACE**: replace an existing view of the same name instead of returning an error.
- *view_name*: a name that is unique among the tables and views of the database; see [Identifiers](https://www.coradb.ai/manual/sql/identifier.html).
- *view_column_name*: names for the view's columns, one per column of the query. Without them, the columns take the names of the query's select list.
- *select_statement*: any valid [SELECT](https://www.coradb.ai/manual/sql/query/select.html), including joins, grouping and [MATCH](https://www.coradb.ai/manual/graph/match.html) patterns.
- **WITH CHECK OPTION**: reject `INSERT` and `UPDATE` statements through the view that would produce rows the view's `WHERE` condition excludes.
- **COMMENT**: comments for the view and its columns.

A view with one row per order, its number of items and its value:

```sql
CREATE VIEW order_summary (order_id COMMENT 'Order number', customer_id, order_date, items, total) AS
    SELECT o.id, o.customer_id, o.order_date, COUNT(*),
           SUM(oi.quantity * p.unit_price * (100 - oi.discount_pct) / 100)
    FROM orders o
      JOIN order_items oi ON oi.order_id = o.id
      JOIN product p ON p.id = oi.product_id
    GROUP BY o.id, o.customer_id, o.order_date
    COMMENT = 'One row per order with its value';

SELECT * FROM order_summary WHERE order_id = 455;
```

| order_id | customer_id | order_date | items | total |
|---:|---:|---|---:|---:|
| 455 | 458 | 2025-12-30 | 3 | 221.75 |

Replace a view's definition, keeping its name and the permissions granted on it:

```sql
CREATE OR REPLACE VIEW order_summary AS
    SELECT id AS order_id, customer_id, order_date FROM orders
    COMMENT 'Orders without their value';
```

[SHOW CREATE VIEW](https://www.coradb.ai/manual/sql/query/show.html#show-create-view) returns a view's query, and the `comment` columns of the `db_class` and `db_attribute` catalog views return its comments.

### Updatable views

`INSERT`, `UPDATE` and `DELETE` statements can target a view when CoraDB can map each row of the view to exactly one row of a table. The view's query must:

- Read from a single table or updatable view (no joins);
- Not use `DISTINCT`, `GROUP BY`, `HAVING` or aggregate functions;
- Combine several queries only with `UNION ALL`, each reading a different table.

A column that is an expression, such as `unit_price * 1.19`, cannot be updated through the view. The user also needs the corresponding permission on the underlying table.

```sql
CREATE VIEW low_stock AS
    SELECT id, name, units_in_stock FROM product WHERE units_in_stock < 5;

UPDATE low_stock SET units_in_stock = 5 WHERE id = 5;   -- updates the product table
```

> **Warning:**
> In this release, updates through views work for views over vertex tables, such as `product` and `customer` in the e-commerce database. A view over an ordinary table is reported as `Vclass <view_name> is not updatable`; change such tables directly.


### WITH CHECK OPTION

With `WITH CHECK OPTION`, a change made through the view must keep the row visible in the view:

```sql
CREATE VIEW cheap_product AS
    SELECT id, name, unit_price FROM product WHERE unit_price < 20
    WITH CHECK OPTION;

UPDATE cheap_product SET unit_price = 18.50 WHERE id = 31;   -- allowed
UPDATE cheap_product SET unit_price = 25 WHERE id = 31;      -- the row would leave the view
```

```text
ERROR: Check option exception on view cheap_product.
```

## ALTER VIEW

```text
ALTER [VIEW | VCLASS] view_name
      AS <select_statement>
    | ADD QUERY <select_statement>
    | CHANGE QUERY [query_number] <select_statement>
    | DROP QUERY query_number [, query_number ...]
    | COMMENT [=] 'comment'
    | COMMENT ON COLUMN column_name [=] 'comment' [, column_name [=] 'comment' ...]
```

A view's definition is a list of queries whose results are combined with `UNION ALL`. The query given in `CREATE VIEW` is query 1; queries added later get the next numbers.

- **AS** *select_statement*: replace the whole definition, like `CREATE OR REPLACE VIEW`.
- **ADD QUERY**: add a query to the list.
- **CHANGE QUERY** *n*: replace query *n* (default 1).
- **DROP QUERY** *n*, ...: remove queries from the list.
- **COMMENT**, **COMMENT ON COLUMN**: change the view's comment or its columns' comments.

```sql
CREATE VIEW featured_product AS
    SELECT id, name, unit_price FROM product WHERE id = 1795;
ALTER VIEW featured_product ADD QUERY SELECT id, name, unit_price FROM product WHERE id = 123;
ALTER VIEW featured_product ADD QUERY SELECT id, name, unit_price FROM product WHERE id = 335;

SELECT * FROM featured_product ORDER BY id;
```

| id | name | unit_price |
|---:|---|---:|
| 123 | Erika Running Short | 45.00 |
| 335 | Arcadio Gym Short | 20.00 |
| 1795 | Balboa Persistence Tee | 29.00 |

```sql
ALTER VIEW featured_product CHANGE QUERY 3 SELECT id, name, unit_price FROM product WHERE id = 38;
ALTER VIEW featured_product DROP QUERY 2;

SELECT * FROM featured_product ORDER BY id;
```

| id | name | unit_price |
|---:|---|---:|
| 38 | Côte de Blaye | 263.50 |
| 1795 | Balboa Persistence Tee | 29.00 |

Replace the definition and add comments:

```sql
ALTER VIEW featured_product AS
    SELECT id, name, unit_price FROM product WHERE unit_price > 120;
ALTER VIEW featured_product COMMENT = 'Products on the home page';
ALTER VIEW featured_product COMMENT ON COLUMN unit_price = 'Current price';
```

> **Note:**
> A view is not updated when the tables it reads from change. After changing the type or collation of a column a view uses, recreate the view.


## DROP VIEW

```text
DROP {VIEW | VCLASS} [IF EXISTS] view_name [, view_name ...]
```

Removes views. The underlying tables and their data are not affected. With `IF EXISTS`, views that do not exist are skipped.

```sql
DROP VIEW IF EXISTS featured_product, order_summary;
```

## RENAME VIEW

```text
RENAME {VIEW | VCLASS} old_view_name {AS | TO} new_view_name [, old_view_name {AS | TO} new_view_name ...]
```

```sql
RENAME VIEW low_stock AS reorder_list;
```

## See also

- [SELECT](https://www.coradb.ai/manual/sql/query/select.html)
- [CREATE TABLE](https://www.coradb.ai/manual/sql/schema/table_stmt.html)
- [GRANT](https://www.coradb.ai/manual/sql/authorization.html#grant): give users access to a view
