Outdated Version
You are viewing an older version of this section. View current production version.
CREATE VIEW
Create an unmaterialized view.
Syntax
CREATE
[DEFINER = { user | CURRENT_USER }]
VIEW view_name
AS select_statement
Remarks
- Views in MemSQL are not writable. Attempts to
INSERT,UPDATE, orDELETEdata in a view will result in an error. Instead, useALTER VIEWor manipulate data directly in the underlying tables from which the view is constructed. - MemSQL will always attempt to merge queries that refer to a view with the original view definition such that portions of the view definition replace semantically equivalent portions of the query. This default behavior is similar to MySQL’s
ALGORITHM = MERGEoption as part ofCREATE VIEW. When a merge is not possible, MemSQL creates an internal temporary table. - MemSQL does not support MySQL’s
CREATE OR REPLACE VIEWsyntax. To modify an existing view, useALTER VIEW. - By default, MemSQL restricts the ability to
CREATE,ALTER, orSELECTfrom the view to the user who created the view and toSUPERusers. In MySQL terms, this is the equivalent of includingDEFINER = CURRENT_USERandSQL SECURITY DEFINERin theCREATE VIEWstatement. - Views cannot reference views or tables from other databases.
- Columns used in a view definition cannot be dropped. Running
DROP TABLEon a table referenced by a view will give an error. RunningALTER TABLE DROP COLUMN colwill give an error whencolis referenced by a view. Columns not referenced by any view can be dropped. CREATE VIEW view_name AS SELECT * FROM table_name ...will replace*with the column names at the time ofCREATE VIEWexecution rather than during execution ofSELECT ... FROM view_name. It is important to consider this distinction iftable_nameis altered between the execution ofCREATE VIEWandSELECT ... FROM view_name.- Any function (including
CURRENT_USER()andNOW()) used in a view are evaluated during execution ofSELECT ... FROM view_name, not when the view is created. - The
DEFINERclause tells MemSQL which user should be used for security checks when a view is referenced by a query. The default value is CURRENT_USER.
Examples
memsql> CREATE VIEW view_name
-> AS SELECT * FROM table_name WHERE user_id = "real_person";
memsql> create view active_items_view as
-> select * from items where status = 'active';
memsql> create view discounted_items_view as
-> select * from active_items_view where discount = 1;
Related Topics