Hi, in this post I will explore the Views in SQL, Views are an important object in database
A VIEW is a virtual table that represents the data in an alternative way
A VIEW contains queries that retrieve data as needed.
VIEWS provide a level of encapsulation.
We can create two types of view :
1)Static view
2)Dynamic view
Static View:
Also known as read-only view.
With the help of this view we can only access the information from the
table but we cannot modify the information in the table.
If you are using order by clause or group by clause in the select
statement while creating the view then this type of view will be static view.
Dynamic View:
Also known as updatable view.
By using dynamic view we can access the information from the table as
well as we can insert update or delete the information in the table.
If we are creating a view and in the select statement you are not using
the order by or group by clause then this type of view is called dynamic view.
If we want to insert the information in the table with the help of view
then all the primary key column and not null column must be available in the
view
Syntax
SQL command for creating a View is:
CREATE VIEW view_name
AS SQL Query
SQL command to Run a
view is:
SELECT * FROM view_name
SQL command to Drop a view
is:
DROP VIEW view_name
SQL command to Modify a
view is:
REPLACE VIEW view_name
AS SQL Query
Example
Create:
CREATE VIEW EmployeeView
AS SELECT First_Name,Last_Name from EmployeeMaster
Run:
SELECT * FROM EmployeeView
Drop:
DROP VIEW EmployeeView
Modify
REPLACE VIEW view_name
AS Select First_Name from EmployeeMaster
Uses of Views
1) To reuse SQL statements.
2) To simplify complex SQL operations. After the query is written, it
can be reused easily, without having
to know the details of the underlying query itself. i.e. using a multi join query multiple times causes the code to be viewed complicated hence we write a view and use it, this is a form of abstraction in database
3)To expose parts of a table instead of complete tables.
4) To change data formatting and representation. Views can return
data formatted and presented differently
from their underlying tables.
No comments:
Post a Comment