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

DBMS_Lecture_05 View

The document discusses database views, which are virtual tables that allow users to access and manipulate data while restricting visibility of certain information. It explains how to create views using SQL, their advantages in simplifying database interactions, and their role in security and data summarization. Additionally, it covers materialized views, built-in data types, user-defined types, and limitations of views.

Uploaded by

Rasel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DBMS_Lecture_05 View

The document discusses database views, which are virtual tables that allow users to access and manipulate data while restricting visibility of certain information. It explains how to create views using SQL, their advantages in simplifying database interactions, and their role in security and data summarization. Additionally, it covers materialized views, built-in data types, user-defined types, and limitations of views.

Uploaded by

Rasel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture 5: View

Views
• A view provides a mechanism to hide certain data from the view of certain users.
• Any relation that is not of the conceptual model but is made visible to a user as a “virtual
relation” is called a view.(A view is a virtual table.)

• Consider a person who needs to know an instructors name and department, but not the salary.
This person should see a relation described, in SQL, by

select ID, name, dept_name


from instructor

• A view is nothing more than a saved SQL query. A view can also be considered as a virtual
table
• 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.
View Definition
• 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.
• The basic CREATE VIEW syntax is as follows:

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE [condition];
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.
Example Views
• A view of instructors without their salary
create view faculty as
select ID, name, dept_name
from instructor

• Find all instructors in the Biology department


select name
from faculty
where dept_name = ‘Biology’

• Create a view of department salary totals


create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;
Views Defined Using Other Views
• create view physics_fall_2009 as
select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = ’Physics’
and section.semester = ’Fall’
and section.year = ’2009’;
• create view physics_fall_2009_watson as
select course_id, room_number
from physics_fall_2009
where building= ’Watson’;
At this point Employees and Departments table should look like this.
Employees Table: Departments Table:

Now, let's write a Query which returns the output as shown below:
Views Example cont.
• To get the expected output, we need to join tblEmployees table with tblDepartments table :

Select Id, Name, Salary, Gender, DeptName from tblEmployee join tblDepartment on
tblEmployee.DepartmentId = tblDepartment.DeptId

• Now let's create a view, using the JOINS query, we have just written.
Create View vWEmployeesByDepartment
as
Select Id, Name, Salary, Gender, DeptName
from tblEmployee join tblDepartment
on
tblEmployee.DepartmentId = tblDepartment.DeptId

• To select data from the view, SELECT statement can be used the way, we use it with a table.

SELECT * from vWEmployeesByDepartment


View Expansion
• Expand use of a view in a query/another view
create view physics_fall_2009_watson as
(select course_id, room_number
from (select course.course_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = ’Physics’
and section.semester = ’Fall’
and section.year = ’2009’)
where building= ’Watson’;
Advantages of using views:
1. Views can be used to reduce the complexity of the database schema, for non IT users.
The sample view, vWEmployeesByDepartment, hides the complexity of joins. Non-IT users,
finds it easy to query the view, rather than writing complex joins.

2. Views can be used as a mechanism to implement row and column level security. Row
Level Security:
For example, I want an end user, to have access only to IT Department employees. If I grant
him access to the underlying tblEmployees and tblDepartments tables, he will be able to see,
every department employees.
To achieve this, I can create a view, which returns only IT Department employees, and grant the
user access to the view and not to the underlying table.

3. Views can be used to present only aggregated data and hide detailed data.
Views Defined Using Other Views
• One view may be used in the expression defining another view
• A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the
expression defining v1
• A view relation v1 is said to depend on view relation v2 if either v1 depends directly to v2 or
there is a path of dependencies from v1 to v2
• A view relation v is said to be recursive if it depends on itself.
View that returns summarized data
• Total number of employees by Department :
Create View vWEmployeesCountByDepartment
as
Select DeptName, COUNT(Id) as TotalEmployees from tblEmployee join
tblDepartment
on tblEmployee.DepartmentId = tblDepartment.DeptId
Group By DeptName

• To look at view definition - sp_helptext vWName


• To modify a view - ALTER VIEW statement
• To Drop a view - DROP VIEW vWName
Update of a View
• Add a new tuple to faculty view which we defined earlier
insert into faculty values (’30765’, ’Green’, ’Music’);
This insertion must be represented by the insertion of the tuple
(’30765’, ’Green’, ’Music’, null)
into the instructor relation
Some Updates cannot be Translated Uniquely
• create view instructor_info as
select ID, name, building
from instructor, department
where instructor.dept_name= department.dept_name;
• insert into instructor_info values (’69987’, ’White’, ’Taylor’);
• which department, if multiple departments in Taylor?
• what if no department is in Taylor?
• Most SQL implementations allow updates only on simple views
• The from clause has only one database relation.
• The select clause contains only attribute names of the relation, and does not have any expressions,
aggregates, or distinct specification.
• Any attribute not listed in the select clause can be set to null
• The query does not have a group by or having clause.
• The following query updates, Name column from Mike to Mikey. Though, we are updating
the view, SQL server, correctly updates the base table tblEmployee. To verify, execute,
SELECT statement, on tblEmployee table.

Update vWEmployeesDataExceptSalary Set Name = 'Mikey' Where Id = 2

• Along the same lines, it is also possible to insert and delete rows from the base table using
views.

Delete from vWEmployeesDataExceptSalary where Id = 2

Insert into vWEmployeesDataExceptSalary values (2, 'Mikey', 'Male', 2)


Update View
Update View
Materialized Views
Materializing a view: A materialized view is a replica of a target master from a single point in time.
The master can be either a master table at a master site or a master materialized view at a materialized
view site.
Materializing a view: create a physical table containing all the tuples in the result of the query
defining the view
• If relations used in the query are updated, the materialized view result becomes out of date
• Need to maintain the view, by updating the view whenever the underlying relations are
updated.

Why Use Materialized Views?


You can use materialized views to achieve one or more of the following goals:
• Ease Network Loads
• Create a Mass Deployment Environment
• Enable Data Subsetting
• Enable Disconnected Computing
Materialized view
• Create materialized view

CREATE MATERIALIZED VIEW employees AS SELECT * FROM employees;

• Updatable Materialized Views

CREATE MATERIALIZED VIEW departments FOR


UPDATE AS SELECT * FROM departments;

• The following statement creates a materialized view group:

BEGIN
DBMS_REPCAT.CREATE_MVIEW_REPGROUP
( gname => 'hr_repg', master => 'orc1.world',
propagation_mode => 'ASYNCHRONOUS');
END;
Built-in Data Types in SQL
• date: Dates, containing a (4 digit) year, month and date
• Example: date ‘2005-7-27’
• time: Time of day, in hours, minutes and seconds.
• Example: time ‘09:00:30’ time ‘09:00:30.75’
• timestamp: date plus time of day
• Example: timestamp ‘2005-7-27 09:00:30.75’
• interval: period of time
• Example: interval ‘1’ day
• Subtracting a date/time/timestamp value from another gives an interval
value
• Interval values can be added to date/time/timestamp values
User-Defined Types
• create type construct in SQL creates user-defined type

create type Dollars from numeric (12,2)

• create table department


(dept_name varchar (20),
building varchar (15),
budget Dollars);
Domains
• create domain construct in SQL-92 creates user-defined domain types

create domain person_name char(20) not null

• Types and domains are similar. Domains can have constraints, such as not null, specified on
them.
• create domain degree_level varchar(10)
constraint degree_level_test
check (value in (’Bachelors’, ’Masters’, ’Doctorate’));
Limitations of views

1. You cannot pass parameters to a view. Table Valued functions are an excellent
replacement for parameterized views.
2. Rules and Defaults cannot be associated with views.
3. The ORDER BY clause is invalid in views unless TOP or FOR XML is also
specified.
4. Views cannot be based on temporary tables.
• Insert Random Number • Curser Example in Database:
Declare @s int
Declare @id int set @s = 0
set @id=1 while (@s <=57)
while(@id<=10000) Begin
begin print (@s)
insert into Temp values ('Product -'+ set @s=@s+1
cast(@id as varchar(20)), End
'Product -' + cast(@id as
varchar(20)) + ' Description') Declare @s int
print @id set @s = 65
set @id=@id+1 while (@s <=90)
end Begin
print CHAR(@s)
set @s=@s+1
End
END

You might also like