Changes

An Introduction to MySQL Views

67 bytes added, 18:58, 19 May 2009
no edit summary
MySQL views are essentially a way to package up SELECT statements into re-usable virtual tables whereby the data can be retrieved simply by referencing the view, rather than having to repeat the associated SELECT statement.
Views are most commonly used in conjunction with joins. In the previous chapter ([[Joining Tables in MySQL]]) we looked at using Joins to retrieve data from columns residing in multiple tables using a single SELECT statement. If you are not familiar with Joins we recommend you read the previous chapter before proceeding
The purpose of this chapter is to explore the concept of views in MySQL.
As you can see from the above mysql output, we have retrieved data from both the ''suppliers'' table and the ''product'' table where the ''product'' ''supplier_id'' column matches the ''suppliers'' supplier_id column.
Ordinarily we would need to repeat the above SELECT statement each time we wanted to perform the join and retrieve the data. Instead, we can create a view called ''prodsupp'' which that is a virtual table populated with the results of our previous join between the ''product'' and ''suppliers'' tables:
<pre>
You will notice that this is identical the output from our original join SELECT statement.
We can also filter data based on our viewjust as we can with any table. For example to retrieve the data for a particular prod_code:
<pre>
CREATE OR REPLACE VIEW ''view name'' AS ''select statement''
 
<google>BUY_MYSQL_BOTTOM</google>
<hr>