0% found this document useful (0 votes)
2 views

24 - Database Views and Materilized Views

The document discusses database views and materialized views, defining views as a way to combine data from multiple tables for security and efficiency. It outlines different types of views, including dynamic, materialized, and partitioned views, and explains how to create and manage them. Additionally, it covers the characteristics of views, such as computed attributes, updating data, and the implications of using materialized views for performance optimization.

Uploaded by

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

24 - Database Views and Materilized Views

The document discusses database views and materialized views, defining views as a way to combine data from multiple tables for security and efficiency. It outlines different types of views, including dynamic, materialized, and partitioned views, and explains how to create and manage them. Additionally, it covers the characteristics of views, such as computed attributes, updating data, and the implications of using materialized views for performance optimization.

Uploaded by

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

Database

Systems

Database Views and Materialized Views


Definition
A view is defined to combine
certain data from one or more tables
for different reasons
A view is like a window through
which we can see data from one or
more tables
Why Views?
Security
 Show the data to a users’ group that
is necessary/required for them
 Give only necessary authorizations
 No concern with rest of the data
Why Views?
Efficiency
 Part of a query that is frequently used
(a subquery), define it as a view
 The view definition will be stored and
will be executed anytime anywhere
view is referred
Why Views?

Join columns from multiple tables


so that they look like a single table
Aggregate information instead of
supplying details
Characteristics of Views
Not exactly the external views of
3-level schema architecture
Major Types
 Dynamic
 Materialized
 Partitioned
 Simple/complex
Dynamic Views
Data is not stored for the views
Definition is stored in the
schema
Executed every time view is
referred
Defining Dynamic Views
CREATE VIEW [ < database_name > . ] [ <
owner > . ] view_name [ ( column [ ,...n ] ) ]
[ WITH ENCRYPTION |
SCHEMABINDING [ ,...n ] ]
AS
select_statement
[ WITH CHECK OPTION ]
Dynamic Views Example
CREATE VIEW st_view1 AS
(select stName, stFname,
prName from student
WHERE prName = 'MCS')
View can be referred in SQL
statements like tables
SELECT * FROM st_view1
With Check Option
CREATE VIEW st_view2 AS
(select stName, stFname,
prName from student
WHERE prName = ‘BCS')
WITH CHECK OPTION
SELECT * FROM ST_VIEW1
Update ST_VIEW1 set prName = ‘BCS’
Where stFname = ‘Loving’
Select * from ST_VIEW1
SELECT * FROM ST_VIEW2

Update ST_VIEW2 set prName = ‘MCS’


Where stFname = ‘Loving’
Characteristics of Views
Different attribute names

CREATE VIEW st_view3 (name,


abbaG, pata) as (select stname,
stfname, stadres from student)
SELECT * FROM ST_VIEW3
Characteristics of Views
Computed attributes
Nesting of views
CREATE VIEW enr_view AS (select *
from enroll)
CREATE VIEW enr_view1 as (select
stId, crcode, smrks, mterm, smrks +
mterm sessional from enr_view)
Select * from enr_view1
Data from Multiple Tables
View can include data from
multiple tables
Through product, or any form
of join
Generally Join
CREATE VIEW st_pr_view AS (select
stName, stFname,
student.prName, prcredits
FROM student, program WHERE
student.prname = program.prname)

SELECT * FROM st_pr_view


CREATE VIEW st_cr_enr_view1 (a1,
a2, a3, a4, a5, a6, a7)
AS (select stName , crname, mterm,
smrks, fmrks, mterm+smrks, totmrks,
FROM student, course, enroll
WHERE student.stid = enroll.stid and
course.crcode = enroll.crcode)
SELECT * FROM st_cr_enr_view1
Updating Data
If single table, updation piece of
cake (with check option)
We can even insert data, BUT
 Not through computed attribute
 Cannot miss “Not NULL” attributes
 One of the multiple tables
 Not in case of aggregate functions
 CREATE VIEW st_view1 as
(SELECT stId, stName, stFname,
prName from student where prName
= 'MCS’)
 INSERT INTO st_view1
values ('S1042', 'Ahmad Ali',
'Ali Hussain', 'BCS')
 ALTER VIEW st_view1 as
(SELECT stId, stName, stFname,
prName from student where prName
= 'MCS’) WITH CHECK OPTION
 INSERT INTO st_view1
values ('S1044', ‘Ali Raza',
‘M. Raza ', 'BCS')
Updating Multiple tables
One at a time
CREATE VIEW st_pr_view1 (a1, a2,
a3, a4) AS (select stId, stName,
program.prName, prcredits from
student, program WHERE
student.prName = program.prName)
insert into st_pr_view1 (a3, a4) values
('MSE', 110)

Select * from program


insert into st_pr_view1 (a1, a2) values
(‘S1043', ‘Bilal Masood’)
SELECT * from student
Interesting Things
Select * from st_pr_view1
Aggregate functions
CREATE VIEW st_view3
(a1, a2) AS
(select prName, avg(cgpa) from
student group by prName)
Select * from st_view3
Materialized Views
Views are virtual tables
Query executed every time
For complex queries involving
large number of join rows and
aggregate functions
Problematic
Materialized Views
Solution is materialized views also
called indexed views created
through clustered index
Creating a clustered index on a
view stores the result set built at
the time the index is created.
Materialized Views
An indexed view also automatically
reflects modifications made to the
data in the base tables after the index
is created, the same way an index
created on a base table does.
Materialized Views
Create indexes only on views
where the improved speed in
retrieving results outweighs the
increased overhead of making
modifications.
Materialized views
alter view st_view1 with
schemabinding as (select
stName, stFname, prName from
dbo.student where prName =
'MCS')
Materialized Views
create unique clustered index
stdview_ind1 on st_view1
(stfname)
Partitioned Views
Lets not discuss it
Idea is, data lying at multiple places
and combined in a view
Same table partitioned horizontally
based on some condition
Simple or Complex Views
One form of classification
View on single table is simple,
while involving multiple tables
is called complex view
There is lot more on views

You might also like