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.
CREATE VIEW
Section titled “CREATE VIEW”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.
- 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, including joins, grouping and MATCH patterns.
- WITH CHECK OPTION: reject
INSERTandUPDATEstatements through the view that would produce rows the view'sWHEREcondition excludes. - COMMENT: comments for the view and its columns.
A view with one row per order, its number of items and its value:
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:
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 returns a view's query, and the comment columns of the db_class and db_attribute catalog views return its comments.
Updatable views
Section titled “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,HAVINGor 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.
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 tableWITH CHECK OPTION
Section titled “WITH CHECK OPTION”With WITH CHECK OPTION, a change made through the view must keep the row visible in the view:
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; -- allowedUPDATE cheap_product SET unit_price = 25 WHERE id = 31; -- the row would leave the viewERROR: Check option exception on view cheap_product.ALTER VIEW
Section titled “ALTER VIEW”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.
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 |
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:
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';DROP VIEW
Section titled “DROP VIEW”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.
DROP VIEW IF EXISTS featured_product, order_summary;RENAME VIEW
Section titled “RENAME VIEW”RENAME {VIEW | VCLASS} old_view_name {AS | TO} new_view_name [, old_view_name {AS | TO} new_view_name ...]RENAME VIEW low_stock AS reorder_list;See also
Section titled “See also”- SELECT
- CREATE TABLE
- GRANT: give users access to a view