Skip to content

MySQL views

MySQL has supported database views since version 5.x. Views are pretty useful if you want to refer to values calculated from expressions without writing the expressions each time you can retrieve them.

What is a view

A view is nothing but an image table or virtual table, which contains no data, which is crated for a base table. A view can be created by taking all values from the base table or by taking only selected values from base table.

How to access view contents

To access the view contents, refer to it like any other table. you can select some or all of its columns, add a where clause to restrict which rows to retrieve, use order by to sort rows and so forth.

View data update

If we perform any modifications in base table, then those modifications automatically effected in view and vice versa.

Creating views

The CREATE VIEW statement creates a new view, or replaces an existing view if the OR REPLACE clause is given. If the view does not exist, CREATE OR REPLACE VIEW is the same as CREATE VIEW. If the view does exist, CREATE OR REPLACE VIEW is the same as ALTER VIEW.

MySQL allows you to create a view based on other views. In the SELECT statement of the view definition, you can refer to another view.

Syntax to create view:

CREATE VIEW VIEWNAME AS SELECT * FROM TABLENAME [WHERE_CONDITION_OR_OTHER_RULES]

Syntax to drop the view:

DROP VIEW VIEWNAME[..N]

You cannot create an index on a view. MySQL uses indexes of the underlying tables when you query data against the views that use the merge algorithm. For the views that use the temptable algorithm, indexes are not utilized when you query data against the views.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments