I have read about the CREATE VIEW syntax of MySQL, but never used it in practice.
Please show me some examples of its applications.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A non-materialized view is effectively a macro – referencing a view means the query it contains is used in the view references place. IE:
This:
…will return a resultset that matches what you’d get from using:
I mentioned non-materialized, but MySQL doesn’t support materialized views. Other databases (Oracle, SQL Server calls them “indexed views”, DB2) support materialized views, but that’s not the question. From now on, I’ll only talk about non-materialized views when I write about views.
Views are used for encapsulation/abstraction – unless the user has access, they can’t see what the underlying query for a view is. This is good/bad depending on the situation – good if you’re worried about giving out data model information; bad in most other cases. Using an ORDER BY in a view is bad because it takes resources to apply the order, which because of encapsulation/abstraction someone else might apply an order by to — it’s a waste of resources. Layering views (views built on views) is also another bad practice – you won’t get an error until the view is run.
Views used to provide access to data without needing to grant access to the table, but they’ve evolved to support updating the underlying table. I prefer to grant access to the table for such situations.