06 Joins Views Integrity Constraints and Authorization
06 Joins Views Integrity Constraints and Authorization
Inner join
Left Outer join
Right Outer Join
Full Outer Join
1
Outer Join
An extension of the join operation that avoids loss of information.
Computes the join and then adds tuples form one relation that does
not match tuples in the other relation to the result of the join.
Uses null values.
joins:
Inner Join
left outer join
right outer join
full outer join
2
Outer Join Example Tables
course relation
prereq relation
Observe that
course information is missing CS-347
prereq information is missing CS-315
3
Left Outer Join
General structure
<table 1> natural left outer join <table 2>
Example
course natural left outer join prereq
Add to the course relation (on the left) the relevant info from prereq
Since CS-315 does not appear in prereq we use null for
prereq_id
In relational algebra syntax:
course ⟕ prereq
4
Right Outer Join
Add to the prereq relation (on the right) the relevant info from course
In relational algebra:
course ⟖ prereq
5
Full Outer Join
In relational algebra:
course ⟗ prereq
6
Views and Authorization
Views
In some cases, it is not desirable for all users to see the
entire logical model (that is, all the actual relations stored in
the database.)
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
8
View Definition
A view is defined using the create view statement which has
the form
9
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;
10
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’;
11
Materialized Views
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.
12
Integrity Constraints
Integrity constraints guard against accidental damage to the
database, by ensuring that authorized changes to the
database do not result in a loss of data consistency.
A checking account must have a balance greater than
$10,000.00
A salary of a bank employee must be at least $4.00 an
hour
A customer must have a (non-null) phone number
13
Integrity Constraints on a Single Relation
not null
primary key
unique
check (P), where P is a predicate
14
Not Null and Unique Constraints
not null
Declare name and budget to be not null
name varchar(20) not null
budget numeric(12,2) not null
unique ( A1, A2, …, Am)
The unique specification states that the attributes A1, A2, …
Am
form a candidate key.
Candidate keys are permitted to be null (in contrast to primary
keys).
15
The check clause
check (P)
where P is a predicate
17
Given the following relational schema
Define SQL DDL for the above relational schema to implement the following
constraints
Course-id must not be null and must be unique
Dept_name must not be null and must be unique
If a dept_name is updated in department relation, it will update the
dept_name of all courses in the course relation
If a dept_name is deleted in department relation, it will delete all courses
of that department from the course relation
A dept_name cannot be inserted into course table that dept-_name is not
present in department table.
18
Cascading Actions in Referential Integrity
19
Users Management
There are following types of users
Students (S1, S2, .............. Sn)
Instructors (I1, I2, I3, ............. Im)
Advisers (Ad1, Ad2, ....... Adk)
Head of the Department (HoD1, HoD2 ...... HoDp)
20
Authorization
21
Authorization Specification in SQL
The grant statement is used to confer authorization
22
Privileges in SQL
select: allows read access to relation,or the ability to query
using the view
Example: grant users U1, U2, and U3 select
authorization on the instructor relation:
grant select on instructor to U1, U2, U3
insert: the ability to insert tuples
update: the ability to update using the SQL update
statement
delete: the ability to delete tuples.
all privileges: used as a short form for all the allowable
privileges
23
Revoking Authorization in SQL
The revoke statement is used to revoke authorization.
24
Roles
create role instructor;
grant instructor to Amit;
Privileges can be granted to roles:
grant select on takes to instructor;
Roles can be granted to users, as well as to other roles
create role teaching_assistant
grant teaching_assistant to instructor;
Instructor inherits all privileges of teaching_assistant
Chain of roles
create role dean;
grant instructor to dean;
grant dean to Satoshi;
25
Authorization on Views
create view geo_instructor as
(select *
from instructor
where dept_name = ’Geology’);
grant select on geo_instructor to geo_staff
Suppose that a geo_staff member issues
select *
from geo_instructor;
What if
geo_staff does not have permissions on instructor?
creator of view did not have some permissions on
instructor?
26