Views
Save queries as virtual tables, updatable views, WITH CHECK OPTION and using views for security.
A view is a saved SELECT statement that you can query like a table. Views hide complexity (an eight-table join becomes SELECT * FROM order_summary), give a stable interface while the underlying tables change, and limit what users can see.
Sample data#
Creating and using a view#
You can filter, sort, join and aggregate a view exactly like a table. The view doesn't store rows: each query runs against the current data in customers and orders, so it's never stale:
Inspecting, changing and dropping views#
SHOW CREATE VIEW customer_sales\Gshows the stored definition. MySQL expands*into explicit columns and fully qualifies names.CREATE OR REPLACE VIEW name AS ...changes the definition (so doesALTER VIEW).DROP VIEW IF EXISTS name;removes it. The underlying data is untouched.
Because SELECT * in a view is expanded when the view is created, columns added to the base table later won't appear until you recreate the view.
Views for security#
Give people what they need and nothing more. A support agent shouldn't see card digits:
Then grant access to the view only: GRANT SELECT ON shop.customer_directory TO 'support'@'%';. By default a view runs with its definer's privileges (SQL SECURITY DEFINER), so users can read through the view without having rights on the base table. Use SQL SECURITY INVOKER if the caller's own privileges should apply instead.
Updatable views#
A simple view, one that maps each of its rows to exactly one row of one base table, can be used in INSERT, UPDATE and DELETE:
The update went straight through to customers. A view is not updatable if it uses aggregates, GROUP BY, DISTINCT, HAVING, UNION, certain subqueries, or most joins (MySQL can update one table of a join view at a time):
WITH CHECK OPTION
Without it, you can update a row out of a view's filter, or insert a row the view can't even see:
WITH CHECK OPTION guarantees that every row written through the view still satisfies its WHERE clause. That makes views a useful guard rail, for example a view per tenant in a multi-tenant app.
Performance#
MySQL processes views with one of two algorithms:
- MERGE: the view's SQL is merged into your query, as if you'd written it inline. Your extra
WHEREconditions can still use indexes on the base tables. - TEMPTABLE: the view's result is built into a temporary table first, then your query reads it. MySQL must do this for views with aggregates,
DISTINCT,UNIONand similar.
A view is never faster than the query inside it. Stacking views on views can produce enormous queries, so check them with EXPLAIN. MySQL has no materialised views. If you need precomputed results, keep a summary table that you refresh with a scheduled event (see Triggers & scheduled events).
Common mistakes#
- Expecting new base-table columns to appear in a
SELECT *view. Recreate the view. - Dropping or renaming a base table column a view uses. The view becomes invalid.
CHECK TABLE view_name;reports broken views. - Using views as a performance tool. They're about convenience and security, not speed.
- A definer account that gets deleted.
DEFINERviews stop working if the account that created them is dropped. Create shared views with a dedicated, long-lived account.
What's next#
You can now build powerful queries on top of well-structured tables. But how do you decide which tables and columns to have in the first place? That's normalisation and schema design.
Check your understanding
Quick quiz
1.What does a (non-materialised) view store?
2.What does
WITH CHECK OPTIONdo on an updatable view?3.Which of these makes a view NOT updatable?
Finished reading?
Mark this lesson complete to track your progress.