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#
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_nameArguments#
| Name | Meaning | Default |
|---|---|---|
OR REPLACE | Replace an existing view of that name. | off |
ALGORITHM, DEFINER, SQL SECURITY | Stored and shown; they change nothing at run time. | UNDEFINED, the creating account, DEFINER |
column_list | Names for the columns of the query (the count must match). | names of the query |
WITH CHECK OPTION | Stored and shown. Not enforced. | none |
RESTRICT, CASCADE | Accepted after DROP VIEW; no effect. | none |
Remarks#
CREATE VIEWruns 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 TABLEon a view is error 1051;SHOW CREATE VIEWon a table is error 1347.SHOW [FULL] TABLESlists views (Table_typeVIEW).DESCRIBEandSHOW COLUMNSgive the columns of the query.- Updatable views.
INSERT,UPDATEandDELETEon a view of one table with plain columns are done on the table: the view's columns are mapped and itsWHEREis added. Any other view answers error 1288 (UPDATE/DELETE) or 1471 (INSERT). - Privileges:
CREATEon the view name (theCREATE VIEWandSHOW VIEWprivileges are known toGRANT, but the check this version makes isCREATEon the name);DROPforDROP VIEW;SELECTon the tables the view's query reads. CREATE VIEWends an open transaction (implicit commit).SHOW CREATE VIEWprints the stored text, not MySQL's canonical text.
Not supported yet#
WITH CHECK OPTIONis stored but rows are not checked against it.information_schema.VIEWShas no rows yet, andTABLES/COLUMNShave no rows for views.SQL SECURITY INVOKERrights checks; views over views asINSERT/UPDATEtargets.- A column list over
SELECT *.
Examples#
Example:
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;