View
View
A view is nothing more than a SQL statement that is stored in the database with an associated
name. A view is actually a composition of a table in the form of a predefined SQL query.A view
can contain all rows of a table or select rows from a table. A view can be created from one or
many tables which depends on the written SQL query to create a view.
Views, which are a type of virtual tables allow users to do the following –
Structure data in a way that users or classes of users find natural or intuitive.
Restrict access to the data in such a way that a user can see and (sometimes) modify
exactly what they need and no more.
Summarize data from various tables which can be used to generate reports
Creating Views
Database views are created using the CREATE VIEW statement. Views can be created
from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according to the
specific implementation.
Example
Now, you can query CUSTOMERS_VIEW in a similar way as you query an actual table.
Following is an example for the same
SQL > SELECT * FROM CUSTOMERS_VIEW;
Uses of a View :
A good database should contain views due to the given reasons:
1. Restricting data access – Views provide an additional level of table security by restricting
access to a predetermined set of rows and columns of a table.
2. Hiding data complexity – A view can hide the complexity that exists in a multiple table join.
3. Simplify commands for the user – Views allows the user to select information from multiple
tables without requiring the users to actually know how to perform a join.
4. Store complex queries – Views can be used to store complex queries.
5. Rename Columns – Views can also be used to rename the columns without affecting the base
tables provided the number of columns in view must match the number of columns specified in
select statement. Thus, renaming helps to to hide the names of the columns of the base tables.
6. Multiple view facility – Different views can be created on the same table for different users.
Advantages of Views
1. Simplification of Complex Queries: By combining complex queries into a single,
reusable item, views make them easier to understand. You can make a view and refer to it
as a table rather than recreating a long query with numerous joins or computations.
2. Enhanced Data Security: Views restrict access to sensitive information by exposing
only specific columns or rows. Users accessing a view cannot see the underlying tables,
ensuring data privacy and security
3. Reusability: You can reuse a query in several reports, analytics, and apps by designating
it as a view. This lessens query logic duplication and encourages consistency
4. Data Abstraction: Views shield users from the intricacy of the database schema. It is
possible to update the view without impacting dependent queries or applications in the
event that the schema of the underlying tables changes.
5. Improved Performance with Materialized Views: Materialized views are used in
certain database systems (like Oracle and PostgreSQL) to physically store query results,
which enhances performance for data that is requested frequently.
Or
Data independence– presents a consistent, unchanging picture of the database's structure
even when the source tables change
Currency– changes to the base tables are reflected immediately in the views.
Improved security- users can be granted access to the database through a relatively small
set of views.
Reduced complexity–simplifies the writing of queries.
Convenience–users see only what they need.
Customization–views can be customized to the needs of individual users.
Data integrity–CHECKOPTION clause of the CREATE VIEW command ensures that
rows satisfy the WHERE clause of the defining query
Disadvantages of Views
Update restriction– in some cases (aswe saw),a view might not be updated.
Structure restriction– Structure is determined at the time of creation. Any columns added
to the database will not show up unless the view is dropped and redefined.
Performance–The use of view slows down response time in some cases.