DBMS_Lecture_05 View
DBMS_Lecture_05 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
• 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:
• 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
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.
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
• Along the same lines, it is also possible to insert and delete rows from the base table using
views.
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
• 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