MySQL Views
Reggie Niccolo Santos
UP ITDC
Outline

What is a view?

Restrictions when using views

Syntax

Sample CREATE VIEW statement

Advantages and disadvantages of using
views
What is a view?

Virtual table based on the result-set of an
SQL statement

The fields in a view are fields from one or
more real tables in the database
Restrictions on views

The SELECT statement cannot contain a
subquery

The SELECT statement cannot refer to
system or user variables

Any table or view referred to in the
definition must exist
Restrictions on views

A temporary VIEW cannot be created

A VIEW cannot be associated with a trigger
Syntax: CREATE VIEW
CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE |
TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
Example: View
CREATE OR REPLACE VIEW `view_track_details`
AS
SELECT TRK.id, TRK.title AS 'track_title',
TRK.rating, TRK.len AS 'length',
TRK.count AS 'play_count', ALB.title AS
'album_title', ART.`name` AS 'artist',
GNR.`name` AS 'genre'
FROM track TRK
JOIN album ALB ON TRK.album_id = ALB.id
JOIN artist ART ON ALB.artist_id = ART.id
JOIN genre GNR ON TRK.genre_id = GNR.id;
“Avie w is no thing m o re than a pse udo -table do ing
the wo rk o f a de fine d q ue ry. ”
Advantages and disadvantages
Advantages:

Easily maintainable pseudo-tables of data

Added level of security by not exposing the
table structure of the application
Disadvantages:

Performance hit
Advantages and disadvantages
Advantages:

Easily maintainable pseudo-tables of data

Added level of security by not exposing the
table structure of the application
Disadvantages:

Performance hit
References

http://www.w3schools.com/sql/sql_view.asp

Http://net.tutsplus.com/tutorials/databases/introdu

Http://zetcode.com/databases/mysqltutorial/views

MySQL Views