MySQL Views - Why we use them
MySQL Views - introduction
In this article I am using a sample MySQL tables available here, and XAMPP - localhost standard installation package, which you should already be familiar with if you are doing anything at all in the area of LAMP stack web development.

You can see that the agents_hr_data object is now visible in database view objects, below regular table listing. Now we can execute a SELECT statement that selects all the fields from this view object and in that way, render data to the HR department only, and keep it restricted for other divisions, for example. The rest of the database can remain restricted so the HR department can only have as much insights as needed for maintaining their daily work routines.
Select from View statement

Data extracted with SELECT statement

Primary mission of MySQL views - save complex queries in a database catalog
Let me tell you about one more very important tip considering MySQL views. MySQL views can significantly ease your life as a database administrator with different INNER JOIN, or similar Sql statements. Why? Well, for example, instead writing complex queries every time, you can save different JOIN queries on your server as VIEWS, and next time when you need data that those queries serve you, instead of using a query that looks like SELECT something FROM something INNER JOIN something, you can simply use SELECT FROM view query, to get the same data. Cool, isn't it? You store your complex SQL quries for later and use data within with simple SELECT statements.Short overview of VIEWS and why we use them
If we use MySQL database storage for our application data management, it is recommended to use VIEWS of the original table structure instead of using the tables directly. This is how we ensure that when we reassemble DB legacy code, we will see the orignal schema via the view, without breaking the application. If we use VIEWS we will not have to create multiple complex SQL joins because we can put all the complexity into Views. This code will be easier to integrate with our application. This will also eliminate chances for typos and your code will be more readable. Views maintain data security so you can use them to show authorized information to authorized personnel and hide data, like credit card numbers for example. Views do not contain the data that is returned, they are just virtual tables. We can also use INSERT, UPDATE or DELETE on a VIEW.In order for latter to work, we have to create an updatable View. Updatable Views work, but we have to see that some things are left out, in order for this type of View to function. Updatable Views do not support:
- aggregate functions (MIN, MAX, SUM, AVG, and COUNT)
- DISTINCT
- GROUP BY
- HAVING
- UNION and UNION ALL
- left JOIN and outer JOIN
- reference to non-updatable view in the FROM clause
- seference only to literal values
- subquery in the SELECT clause or in the WHERE clause that refers to the table appeared in the FROM clause
- multiple references to any column of the base table
- indexes
Check if View is updatable
If we need to check if a View in a database is updatable, we can do that by querying theis_updatable
column from the views table in the information_schema database: