Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are views in MySQL?

Tags:

sql

mysql

views

What are views in MySQL? What's the point of views and how often are they used in the real world.

I'm after a layman explanation please with a example if possible to aid my understanding!

Thank you in advance ;-)

like image 879
Imran Avatar asked Jul 27 '10 11:07

Imran


People also ask

What is the use of views in MySQL?

MySQL supports views, including updatable views. Views are stored queries that when invoked produce a result set. A view acts as a virtual table. The following discussion describes the syntax for creating and dropping views, and shows some examples of how to use them.

What are views in a database?

A database view is a subset of a database and is based on a query that runs on one or more database tables. Database views are saved in the database as named queries and can be used to save frequently used, complex queries. There are two types of database views: dynamic views and static views.

What is a view used for?

Views are virtual tables that can be a great way to optimize your database experience. Not only are views good for defining a table without using extra storage, but they also accelerate data analysis and can provide your data extra security.

What are the types of views in MySQL?

There are 2 types of Views in SQL: Simple View and Complex View. Simple views can only contain a single base table. Complex views can be constructed on more than one base table.


2 Answers

Normal Views are nothing more then queryable queries.

Example:

You have two tables, orders and customers, orders has the fields id, customer_id, performance_date and customers has id, first_name, last_name.

Now lets say you want to show the order id, performance date and customer name together instead of issuing this query:

SELECT    o.id as order_id, c.first_name + ' ' + c.last_name as customer_name,
          o.performance_date 
FROM      orders o inner join customers c

you could create that query as a view and name it orders_with_customers, in your application you can now issue the query

SELECT   *
FROM     orders_with_customer

One benefit is abstraction, you could alter the way you store the customers name, like inlcuding a middle name, and just change the views query. All applications that used the view continue to do so but include the middle name now.

like image 90
Maxem Avatar answered Sep 23 '22 18:09

Maxem


Have a look at:

MySQL Views Tutorial
Views and More in MySQL 5.0

like image 41
Sarfraz Avatar answered Sep 23 '22 18:09

Sarfraz