0% found this document useful (0 votes)
5K views5 pages

Experiment No 11

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views5 pages

Experiment No 11

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No :11 Date / / 2023

Title: Views

Aim: To study the implementation of MYSQL Views

Theory:

A database view is a virtual table or logical table which is defined as a SQL SELECT query with joins.
Because a database view is similar to a database table, which consists of rows and columns, so you can
query data against it. Most database management systems, including MySQL, allow you to update data
in the underlying tables through the database view with some prerequisites.

A database view is dynamic because it is not related to the physical schema. The database system stores
database views as a SQL SELECT statement with joins. When the data of the tables changes, the view
reflects that changes as well.

Advantages of database view

The following are advantages of using database views.

A database view allows you to simplify complex queries: a database view is defined by an SQL statement
that associates with many underlying tables. You can use database view to hide the complexity of
underlying tables to the end-users and external applications. Through a database view, you only have to
use simple SQL statements instead of complex ones with many joins.

A database view helps limit data access to specific users. You may not want a subset of sensitive data
can be queryable by all users. You can use a database view to expose only non-sensitive data to a
specific group of users.

A database view provides extra security layer. Security is a vital part of any relational database
management system. The database view provides extra security for a database management system.
The database view allows you to create read-only view to expose read-only data to specific users. Users
can only retrieve data in read-only view but cannot update it.

A database view enables computed columns. A database table should not have calculated columns
however a database view should. Suppose in the orderDetails table you havequantityOrder (the number
of ordered products) and priceEach (price per product item) columns. However, the orderDetails table
does not have computed column to store total sales for each line item of the order. If it has, the

91
database schema would not be a good design. In this case, you can create a computed column
named total , which is a product of quantityOrderand priceEach to represent the computed result.
When you query data from the database view, the data of the computed column is calculated on fly.

A database view enables backward compatibility. Suppose you have a central database, which many
applications are using it. One day, you decide to redesign the database to adapt with the new business
requirements. You remove some tables and create new tables, and you don’t want the changes affect
other applications. In this scenario, you can create database views with the same schema as the legacy
tables that you will remove.

Disadvantages of database view

Besides the advantages above, there are several disadvantages of using database views:

Performance: querying data from a database view can be slow especially if the view is created based on
other views.

Tables dependency: you create view based on underlying tables of the a database. Whenever you
change the structure of those tables that view associates with, you have to change the view as well.

To create a new view in MySQL, you use the CREATE VIEW statement. The syntax of creating a view in
MySQL is as follows:

CREATE

[ALGORITHM = {MERGE | TEMPTABLE | UNDEFINED}]

VIEW [database_name].[view_name]

AS

[SELECT statement]

View processing algorithms

The algorithm attribute allows you to control which mechanism MySQL uses when creating the view.
MySQL provides three algorithms: MERGE , TEMPTABLE , and UNDEFINED .

Using MERGE algorithm, MySQL first combines the input query with the SELECT statement which defines
the view, into a single query. And then MySQL executes the combined query to return the result set.
92
The MERGE algorithm is not allowed if the SELECT statement contains aggregate functions such
as MIN, MAX, SUM, COUNT, AVG, etc., or DISTINCT, GROUP BY,HAVING, LIMIT, UNION, UNION
ALL, subquery. In case the SELECT statement refers to no table, the MERGE algorithm is also not
allowed. If the MERGE algorithm is not allowed, MySQL switches the algorithm to UNDEFINED . Note
that the combination of input query and the query in the view definition into one query is referred to
as view resolution.

Using TEMPTABLE algorithm, MySQL first creates a temporary table based on the SELECTstatement of
the view definition, and then it executes the input query against this temporary table. Because MySQL
has to create temporary table to store the result set and moves the data from the underlying tables to
the temporary table, the TEMPTABLE algorithm is less efficient than the MERGE algorithm. In addition,
a view that uses TEMPTABLE algorithm is not updateable.

The UNDEFINED is the default algorithm when you create a view without specifying an explicit
algorithm. The UNDEFINED algorithm lets MySQL to make a choice
of using MERGE or TEMPTABLE algorithm.

MySQL prefers MERGE algorithm to TEMPTABLE algorithm because MERGEalgorithm is much more
efficient.

example: A query that returns data from both tables customers and payments using the inner join

93
Next time, if you want to get the same information including customer name, check number, payment
date, and amount, you need to issue the same query again.

One way to do this is to save the query in a file, either .txt or .sql file so that later you can open and
execute it

A better way to do this is to save the query in the database server and assign a name to it. This named
query is called a database view, or simply, view.

To create a new view you use the CREATE VIEW statement. This statement creates a
view customerPayments based on the above query above

94
Once you execute the CREATE VIEW statement, MySQL creates the view and stores it in the database.
Now, you can reference the view as a table in SQL statements. For example, you can query data from
the customerPayments view using the SELECT statement:
MySQL allows you to create a view based on a SELECT statement that retrieves data from one or more
tables. This picture illustrates a view based on columns of multiple tables

Conclusion: Studied & implemented MYSQL Views.

95

You might also like