SQL reference (MySQL and MariaDB dialects)

CREATE VIEW, ALTER VIEW and DROP VIEW

Stored queries that are read as derived tables, with the rules for updatable views and the limits of this version.

A view is a stored query. Wherever a FROM names a view, the query is read as a derived table with the view's name (or the statement's alias). The definitions are documents in a hidden table __rdb_views of the database (D70).

Syntax#

SQL
CREATE [OR REPLACE]
    [ALGORITHM = { UNDEFINED | MERGE | TEMPTABLE }]
    [DEFINER = account]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW [db_name.]view_name [(column_list)]
    AS query_expression
    [WITH [CASCADED | LOCAL] CHECK OPTION]

ALTER
    [ALGORITHM = { UNDEFINED | MERGE | TEMPTABLE }]
    [DEFINER = account]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW [db_name.]view_name [(column_list)]
    AS query_expression
    [WITH [CASCADED | LOCAL] CHECK OPTION]

DROP VIEW [IF EXISTS] view_name [, view_name] ... [RESTRICT | CASCADE]

SHOW CREATE VIEW view_name

Arguments#

NameMeaningDefault
OR REPLACEReplace an existing view of that name.off
ALGORITHM, DEFINER, SQL SECURITYStored and shown; they change nothing at run time.UNDEFINED, the creating account, DEFINER
column_listNames for the columns of the query (the count must match).names of the query
WITH CHECK OPTIONStored and shown. Not enforced.none
RESTRICT, CASCADEAccepted after DROP VIEW; no effect.none

Remarks#

  • CREATE VIEW runs the query (with no rows) first, so unknown tables and columns are refused as in MySQL. A name already used by a table or view is error 1050.
  • Views of views are expanded to 16 levels; a deeper or circular definition is error 1462.
  • DROP TABLE on a view is error 1051; SHOW CREATE VIEW on a table is error 1347.
  • SHOW [FULL] TABLES lists views (Table_type VIEW). DESCRIBE and SHOW COLUMNS give the columns of the query.
  • Updatable views. INSERT, UPDATE and DELETE on a view of one table with plain columns are done on the table: the view's columns are mapped and its WHERE is added. Any other view answers error 1288 (UPDATE/DELETE) or 1471 (INSERT).
  • Privileges: CREATE on the view name (the CREATE VIEW and SHOW VIEW privileges are known to GRANT, but the check this version makes is CREATE on the name); DROP for DROP VIEW; SELECT on the tables the view's query reads.
  • CREATE VIEW ends an open transaction (implicit commit).
  • SHOW CREATE VIEW prints the stored text, not MySQL's canonical text.

Not supported yet#

  • WITH CHECK OPTION is stored but rows are not checked against it.
  • information_schema.VIEWS has no rows yet, and TABLES / COLUMNS have no rows for views.
  • SQL SECURITY INVOKER rights checks; views over views as INSERT/UPDATE targets.
  • A column list over SELECT *.

Examples#

Example:

SQL
CREATE VIEW rich AS SELECT id, name, sal FROM emp WHERE sal >= 150;
CREATE VIEW dept_tot (d, total, n) AS SELECT dept, SUM(sal), COUNT(*) FROM emp GROUP BY dept;
CREATE OR REPLACE VIEW rich AS SELECT id, name FROM emp WHERE sal >= 150;
ALTER VIEW rich AS SELECT id, name, sal FROM emp WHERE sal >= 100;
UPDATE rich SET sal = sal + 1 WHERE id = 1;        -- done on emp
INSERT INTO rich (id, name, sal) VALUES (9, 'zed', 900);
UPDATE dept_tot SET n = 1;                         -- error 1288, grouped view
DROP VIEW IF EXISTS nothere;

See also#