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

06 Joins Views Integrity Constraints and Authorization

Uploaded by

forstorage774
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

06 Joins Views Integrity Constraints and Authorization

Uploaded by

forstorage774
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/ 26

Joined Relations

 Join operations take two relations and return as a result another


relation.
 A join operation is a Cartesian product which requires that tuples in the
two relations match (under some condition). It also specifies the
attributes that are present in the result of the join
 Joins are:

 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

 course natural right outer join prereq

 Add to the prereq relation (on the right) the relevant info from course
 In relational algebra:
course ⟖ prereq

5
Full Outer Join

 course natural full outer join prereq

 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

select ID, name, dept_name


from instructor

 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.

8
View Definition
 A view is defined using the create view statement which has
the form

create view v as < query expression >

where <query expression> is any legal SQL expression. The


view name is represented by v.
 Once a view is defined, the view name can be used to refer to
the virtual relation that the view generates.
 View definition is not the same as creating a new relation by
evaluating the query expression
 Rather, a view definition causes the saving of an expression;
the expression is substituted into queries using the view.

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

Example: ensure that semester is one of fall, winter, spring


or summer:

create table section (


course_id varchar (8),
sec_id varchar (8),
semester varchar (6),
year numeric (4,0),
building varchar (15),
room_number varchar (7),
time slot id varchar (4),
primary key (course_id, sec_id, semester, year),
check (semester in (’Fall’, ’Winter’, ’Spring’, ’Summer’))
);
16
Referential Integrity
 Ensures that a value that appears in one relation for a given
set of attributes also appears for a certain set of attributes in
another relation.
 Example: If “Biology” is a department name appearing in
one of the tuples in the instructor relation, then there
exists a tuple in the department relation for “Biology”.
 Let A be a set of attributes. Let R and S be two relations that
contain attributes A and where A is the primary key of S. A is
said to be a foreign key of R if for any values of A appearing
in R these values also appear in S.

17
Given the following relational schema

Course (course-id, title, dept_name, credit)


Department (dept_name, building, budget)

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

 create table course (


course_id char(5) primary key,
title varchar(20),
dept_name varchar(20) references department
)
 create table course (

dept_name varchar(20),
foreign key (dept_name) references department
on delete cascade
on update cascade,
...
)
 alternative actions to cascade: set null, set default

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)

 Students can add courses


 Advisors approve courses, delete advisee courses , add advisee
courses
 Instructors can add courses to be taught, delete courses already
selected to be taught
 Head of the department can add new courses, approve actions of
advisors and instructors

20
Authorization

Forms of authorization on parts of the database:

 Read - allows reading, but not modification of data.


 Insert - allows insertion of new data, but not modification of
existing data.
 Update - allows modification, but not deletion of data.
 Delete - allows deletion of data.

Forms of authorization to modify the database schema


 Index - allows creation and deletion of indices.
 Resources - allows creation of new relations.
 Alteration - allows addition or deletion of attributes in a relation.
 Drop - allows deletion of relations.

21
Authorization Specification in SQL
 The grant statement is used to confer authorization

grant <privilege list>


on <relation name or view name> to <user list>
 <user list> is:
 a user-id
 public, which allows all valid users the privilege granted
 A role (more on this later)
 Granting a privilege on a view does not imply granting any
privileges on the underlying relations.
 The grantor of the privilege must already hold the privilege on
the specified item (or be the database administrator).

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.

revoke <privilege list>


on <relation name or view name> from <user list>
 Example:

revoke select on branch from U1, U2, U3


 <privilege-list> may be all to revoke all privileges the revokee
may hold.
 If <revokee-list> includes public, all users lose the privilege
except those granted it explicitly.
 If the same privilege was granted twice to the same user by
different grantees, the user may retain the privilege after the
revocation.
 All privileges that depend on the privilege being revoked are also
revoked.

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

You might also like