Science Department Relational Model - Donald Bren School of

47
Relational Model Prof. Sharad Mehrotra Information and Computer Science Department University of California at Irvine Chapter 3 and 6 from SKS Chapter 3 in UW

Transcript of Science Department Relational Model - Donald Bren School of

Page 1: Science Department Relational Model - Donald Bren School of

Relational Model

Prof. Sharad MehrotraInformation and ComputerScience DepartmentUniversity of California at IrvineChapter 3 and 6 from SKSChapter 3 in UW

Page 2: Science Department Relational Model - Donald Bren School of

Outline� Relational model

� basic modeling concepts for schema specification

� Mapping ER diagrams to Relational Model� Relational Languages

� relational algebra (algebraic)� basic operators, expressions in relational algebra

� relational calculus (logic based) /*will not becovered in class */

Page 3: Science Department Relational Model - Donald Bren School of

3

Relational Model -- Quick Example

S sno N am e S treet city

N U L L F rank 12 M ain C ham p aign

1200331 C athy

3 N eil U rbana

2000312 B ill 45 W . O ak U rbana

AccountNumber

Custid balance

34 1000201 10,799

45 1200331 1,345

63 2000312 100,345

Key constraints:

•ssno is the key for customertable

•both accountno and custid arekeys for account table

Referential Integrity constraints:(foreign keys)

The custid attribute in account tabletakes values from ssno in customertable

Null Constraint:

customer name cannottake null values

� A relational schema = tables and constraints.

� Tables: customer, account

� Constraints:

Page 4: Science Department Relational Model - Donald Bren School of

4

Relational Model

� Database schema consists of� a set of relation schema� a set of constraints over the relation schema

� Relational Schema = name(attributes).Graphically drawn as table.

� Example: employee(ssno, name, salary)� Recall:

� relation is a subset of cartesian product ofsets

� relation is a set of n-tuples where n = degreeof the relation

Page 5: Science Department Relational Model - Donald Bren School of

Attributes� With each attribute a domain is specified� In relational model attributes are atomic:

� the values are not divisible. That is, wecannot refer to or directly see a subpart of thevalue.

� an attribute can take a special null value� Null value represents either attributes

whose value is not known, or do not exist

Page 6: Science Department Relational Model - Donald Bren School of

6

null in this case may mean that diagnosis is not complete anddisease has not been identified. Notice possibility of confusionthat null means that the patient has no disease! This is one ofthe reasons why using nulls is not a great idea! We will seeother reasons as well later

Example of a RelationDiagnosis: an example relation/table

Patient Disease

Jim SchizophrenicJane Obsessive-CompJerry ManicJoe null

Page 7: Science Department Relational Model - Donald Bren School of

7

Constraints

� What are they?� represent the semantics of the domain being modeled.� restrict the set of possible database states

� Why do we want to specify the constraints?� Useful information to application programmers. They

can write programs to prevent constraints violation� constraint -- acct balance should not fall below 0 dollars� programmer writing code for debit application should

check at the end if the account gets overdrawn!� DBMS might enforce specified constraints directly

making task of application writer easier� If DBMS guarantees that account does not get overdrawn,

then debit application programmer need not worry aboutchecking for overdrawn account condition.

Page 8: Science Department Relational Model - Donald Bren School of

8

Constraints

� Why specify constraints?� knowledge of some type of constraints

enables us to identify redundancy in schemasand hence specification of constraints helps indatabase design (we will see this later)

� Knowledge of some type of constraints canalso help the DBMS in query processing

Page 9: Science Department Relational Model - Donald Bren School of

Specifying Constraints in DataModels� ER model

� domain and key constraints over entities� participation and cardinality constraints over

relationships

� Relational Model� domain constraints, entity identity, key

constraint, functional dependencies --generalization of key constraints, referentialintegrity, inclusion dependencies --generalization of referential integrity.

Page 10: Science Department Relational Model - Donald Bren School of

10

Domain Constraint

� In the schema, every attribute isdeclared to have a type --- integer, float,date, boolean, string, etc.

� An insertion request can violate thedomain constraint.

� DBMS can check if insertion violatesdomain constraint and reject theinsertion.

Page 11: Science Department Relational Model - Donald Bren School of

11

Key Constraint

� Each relation has a primary key.� Superkey:

� set of attributes such that if two tuples agreeon those attributes, then they agree on all theattributes of the relation

� Note: the set of all the attributes of a relationis always a superkey.

� Candidate key:� superkey no subset of which i s a superkey

� Primary key:� one of the candidate keys

Page 12: Science Department Relational Model - Donald Bren School of

12

Disallowing Null Values� Some fields of a relation are too

important to contain null values.� Example:

� in sales(customer, salesman, date, amount,saleID) we may not want ‘customer’ tocontain a null value.

Page 13: Science Department Relational Model - Donald Bren School of

Entity Integrity Constraint� A primary key must not contain a null

value.� Else it may not be possible to identify some

tuples.� For Example, if more than one tuple has a null

value in its primary key, we may not be ableto distinguish them .

Page 14: Science Department Relational Model - Donald Bren School of

14

Foreign Key and ReferentialIntegrity Constraint� Consider following 2 relation schemas:

� R1(A1, A2, …An) and R2(B1, B2, … Bm)

� Let PK be subset of {A1, …,An} be primary keyof R1

� A set of attributes FK is a foreign key of R2 if:� attributes in FK have same domain as the attributes in

PK� For all tuples t2 in R2, there exists a tuple t1in R1 such

that t2[FK] = t1[PK].

� A referential integrity constraint from attributesFK of R2 to R1 means that FK is a foreign thatrefers to the primary key of R1.

Page 15: Science Department Relational Model - Donald Bren School of

15

� student-grades student C# Semester grade Susan CS101 1-91 A Jane CS101 1-91 B

� LegalGrades Grade

A we will have a referential integrity B constraint saying that C every value of student-grades.grade D must also be a value of F LegalGrades.grade, Audit Ex

Example of Referential Integrity

Page 16: Science Department Relational Model - Donald Bren School of

16

� Generalization of referential integrity constraint.� Inclusion dependency R1[A1,...,An] ⊆ ==R2 [B1,...,Bn]

means that the values in the first relation R1 refer tothe values in the second relation

� Formally, R1[A1,...,An] ⊆ ==R2 [B1,...,Bn] iff thefollowing holds:� for all t1 in R1, there exists a t2 in R2 such that t1[A1, …, An]

= t2[B1, …, Bn]

� Notice that referential integrity constraint is aninclusion dependency in which {B1, .. Bn} is theprimary key of R2.

Inclusion Dependencies

Page 17: Science Department Relational Model - Donald Bren School of

Example

� student-grade[Grade] ⊆ =LegalGrade[Grade]� CourseOffering[C#] ⊆ ==Courses[C#]� Takes[S#] ⊆ ==Students[S#]� CourseOffering[Professor] ⊆ ==UCEmployee[E#]

Page 18: Science Department Relational Model - Donald Bren School of

18

Data Modification and IntegrityConstraints� Modification, insertion and deletion requests

can lead to violations of integrity constraints.� Key constraint, entity identity, null value constraint,

referential integrity, inclusion dependencies, functionaldependencies, multivalued dependencies.

� must check for violation at end of each operationand suitably allow or disallow operation.

� Impact of data modification on inclusiondependencies can be sometimes tricky!

Page 19: Science Department Relational Model - Donald Bren School of

Example� Relations

� CourseOfferings(C#, semester, instructor)� Takes(S#, C#, semester, grade)

� Referential Integrity Constraint:� Takes(C#,semester) ⊆

CourseOffering(C#,semester)

� Consider canceling a course.� Delete from courseOfferings where c# =

“CS101” AND Semester = “2-91”;

� What should happen to tuples in Takesthat refer to CS101 and semester 2-91??

Page 20: Science Department Relational Model - Donald Bren School of

Example (cont) Takes S# C# Semester Grade 1001 CS101 2-91 ^ 1002 CS101 2-91 ^ 1003 CS101 1-91 A

� Possible Solutions: 1) reject update (abort) - or - 2) delete tuples from ‘Takes’ that refer

to ‘CS101, 2-91’ (cascade)

Page 21: Science Department Relational Model - Donald Bren School of

21

FDs is a generalization of concept of keys. Given a relation R with attributes A1,...,An,B1,...,Bm,C1,...,Cl, we say that A1,...,An functionally determine B1,...,Bm

(A1,...,An B1,...,Bm) if whenever two tuples agree on their values for A1,...,An, they agree on B1,…,Bm

The key of a relation functionally determines all the attributes of therelation.

(by definition of a key)

Functional Dependencies

Page 22: Science Department Relational Model - Donald Bren School of

Example� Takes(C#, S#, semester, grade).� Key = (C#,S#,semester)

C# S# Semester gradeCS101 13146 1-91 A CS101 13146 1-91 B

illegal since it violates FD thatC#,S#,Semester functionally determinegrade

Page 23: Science Department Relational Model - Donald Bren School of

Logical Implication of FunctionalDependencies� Consider R(A,B,C)� Let the following functional dependencies

hold:� A B (f1)� B C (f2)

� We can show that f1 and f2 together logicallyimply that the following functional dependencyalso holds:� A C (f3)

Page 24: Science Department Relational Model - Donald Bren School of

Proof� say f3 does not hold.� then there exists tuples t1, t2 in R such that

t1[A] = t2[A] and t1[C] is not equal to t2[C]� Since f1 holds and since t1[A] = t2[A], it must

be the case that t1[B] = t2[B]� Hence since t1[B] = t2[B] and f2 holds, it must

be the case that t1[C] = t2[C]� This is a contradition!� Hence, f3 must also hold!

Page 25: Science Department Relational Model - Donald Bren School of

Closure of FunctionalDependency SetDefinition: Let R be a relation scheme, and F be the set offunctional dependencies defined over R. F+ denotes the set of allfunctional dependencies that hold over R. That is,

F+ = { X Y | F logically

implies X Y}Example:

Let F = {A B, B C}

then

A C is in F+

F+ is called the closure of F

Page 26: Science Department Relational Model - Donald Bren School of

Inferring FunctionalDependenciesGiven a set of fds F over a relation scheme R, how to computeF+ ?1. Reflexivity:

If Y is a subset of X, then X Y

Examples: AB A, ABC AB, etc.

2. Augmentation:

If X Y, then XZ YZ

Examples: If A B, then

AC BC3: Transitivity:

If X Y, and Y Z,

then X Z.

These 3 rulesare calledARMSTRONGSAXIOMS!!

Page 27: Science Department Relational Model - Donald Bren School of

27

Using AA to infer dependencies

� Union Rule:� if X Y, X Z, then X YZ

� Proof:� Since X Y, using augmentation, X XY (1)� Since X Z, using augmentation, XY XZ (2)� Using (1) and (2) and transitivity we get X YZ Q.E.D.

� Pseudo-Transitivity Rule:� If X Y, WY Z, then WX Z

� Proof:� Since X Y, using augment XW YW (1)� Given WY Z, and (1) using transitivity� WX Z Q.E.D.

Page 28: Science Department Relational Model - Donald Bren School of

Armstrongs Axioms

Armstrongs Axioms are sound:

If X Y is deduced using AA from F, then X Yholds over any relation r in which fd’s in F hold.

Armstrongs Axioms are complete:

If a functional dependency X Y is in the closure of F(that is, in F+), then it can be deduced using ArmstrongsAxioms over F.

Since AAs are sound and complete, they provide amechanism to us to compute F+ from F.Even though we defined F+ as the set of all fds that are logicalimplication of F, since AA are sound and complete, we can redefine F+as the fds that follow from F using AA.

Page 29: Science Department Relational Model - Donald Bren School of

29

Superkeys and FDs

� Using FDs we can formally define the notion ofkeys

� Let R(A1, A2, …,An) be a relation� Let X be a subset of {A1, A2, …An}� X is a superkey of R if and only if the functional

dependency X A1,A2, …,Anis in F+

� Naïve Algorithm to test for superkey� compute F+ using AA� If X -----> A1,A2,…,An is in F+

� X is a superkey

Page 30: Science Department Relational Model - Donald Bren School of

Cardinality of Closure of F

Let F = {A B1, A B2, ..., A Bn} (cardinality ofF = n)

then

{A Y | Y is a subset of {B1, B2, ..., Bn}} is a subset ofF+

(cardinality of F+ is more than 2^n).

So computing F+ may take exponential time!

Fortunately, membership in F+ can be tested without computingF+. (we will study the algorithm later)

Page 31: Science Department Relational Model - Donald Bren School of

31

� Commercial systems allow users tospecify many other types of constraintsbesides the ones discussed in class. Wewill study them when we study SQL. :

� Example:� Relations:

� Takes(C#, S#, semester, grade)� courses(C#, UnitsOfCredit, ...)

� Integrity Constraints:� Every student must enroll for at least three credits

every semester.

General Integrity Constraints

Page 32: Science Department Relational Model - Donald Bren School of

Strong Entity Relation:

ssno name

salary

employee

Employee(ssno name salary)

Key : ssno

ER to Relational Mapping

Page 33: Science Department Relational Model - Donald Bren School of

Weak Entity Relation:acct# customer balance transaction(acct#,trans#, amount)

account

log

transaction

trans# amount

Key: acct# trans#

IND:transactional[acct#] account[acct#]

ER to Relational Mapping

Page 34: Science Department Relational Model - Donald Bren School of

ssno name salary Relation:

works_on(ssno,proj#,startdate)

Key:ssno,proj#

Ind: workson[proj#] project[proj#]

workson[ssno] employee[ssno]

employee

Works on

project

StartdateM

N

proj projmgr

ER to Relational Mapping

Page 35: Science Department Relational Model - Donald Bren School of

ssno name salary Relation:

works_on(ssno,proj#,startdate)

Key:ssno

Ind: workson[proj#] project[proj#]

workson[ssno] employee[ssno]

employee

Works on

project

StartdateM

1

proj projmgr

Employeeworks onatmost 1project

ER to Relational Mapping

Page 36: Science Department Relational Model - Donald Bren School of

ssno name salary Relation:

works_on(ssno,proj#,startdate)

Key:ssno,proj#

Ind: workson[proj#] project[proj#]

workson[ssno] employee[ssno] employee[ssno] workson[ssno]

employee

Works on

project

Startdate

N

proj projmgr

M

Each employ must work on aproject

ER to Relational Mapping

Page 37: Science Department Relational Model - Donald Bren School of

ssno name salary Relation : worksonusing(ssno,proj#,toolid,

startdate)

Key : ssno,toolid

IND : worksonusing[proj#] project[proj#] worksonusing[ssno] employee[ssno]

worksonusing[toolid] tools[toolid] employee[ssno] worksonusing[ssno]

employee

Workson using

project

tools

startdate

toolid

toolspecs

M

1

N

Proj# projmgr

Each employeemust work on a projusing a tool. Employee uses a given tool works on a single proj

ER to Relational Mapping

Page 38: Science Department Relational Model - Donald Bren School of

ssno name salary Relation:staff(ssno, name, salary, position)faculty(ssno, name, salary, rank)student_assistant(ssno, name, salary,

percentage_time)

Key: ssno for all the relationscannot use if partial:cannot representemployees who are neither staff, nor faculty, not student assistants!

Cannot use if overlap:if staff could also bea student assistant, then redundancy

requires a union to construct list of all employees

Is-a d

staff

faculty

Student assistant

employee

If no overlap,and totalparticipation

ER to Relational Mapping

Page 39: Science Department Relational Model - Donald Bren School of

ssno name salary Relation:employee(ssno, name, salary, jobtype,position, rank, percentage-time)

Key : ssno

job type can be used to specify whether an employee is a staff, a faculty, or a student

assistant

a lot of null values will be used.

If an employee does not belong to any subclass, use null value for job typecannot be used if overlap

total participation can be represented by preventing null in jobtype

does not require union to construct the list ofemployees

Is-a d

staff

faculty

Student assistant

employee

positionrank

Percenttage time

If no overlap,participationcan be partial

ER to Relational Mapping

Page 40: Science Department Relational Model - Donald Bren School of

ssno name salary Relation:employee (ssno, name, salary)staff(ssno, position)faculty(ssno, rank)student_assistant(ssno, percentage_time)

Key : ssno for all the relations

INDstaff[ssno] employee[ssno]faculty[ssno] employee[ssno]student_assistant[ssno] employee[ssno]

cannot represent total constraint

Is-ao

staff

faculty

Student assistant

employee

positionrank

Percenttage time

If overlapping

ER to Relational Mapping

Page 41: Science Department Relational Model - Donald Bren School of

ssno name salary Relation:employee(ssno, name, salary, Isstaff,position, Isfaculty, rank, Isstudentassistant,percentage-time)

Key : ssno

Isstaff, Isfaculty, Isstudent_assistant are boolean values which are either true or false.The relation will contain lot of null values

cannot represent total constraint.

Is-a o

staff

faculty

Student assistant

employee

positionrank

Percenttage time

another mechanism ifoverlapping

ER to Relational Mapping

Page 42: Science Department Relational Model - Donald Bren School of

� ER Diagrams can be mapped to relational model (except sometimestotal participation in a superclass/ subclass relationship is difficult tomodel)

� Recall that at times during the design cycle we wish to do the reverse--that is, map relational schema to ER model.

� Can this always be done ?� So far the mapping to be correct, we should be able to represent the

constraints over a relational schema in the ER model.� Constraints in relational schema -- functional dependencies, inclusion

dependencies.� Constraints in ER model: key constraints, cardinality constraints,

participation constraints.� Can we model fds and INDs using the constraints in ER model?

ER to Relational Mapping

Page 43: Science Department Relational Model - Donald Bren School of

Example

� Consider we wish to build a catalog with three fields:street, city, zip

� So least we need to do is create an entity with the three attributes:- street city uniquely determines zip-zip uniquely determines city

� This can be modelled using the following two FDs in the relationalmodel:-street city zip-zip city

� Can the same be modelled in ER using the set of constraints present?

ER to Relational Mapping

Page 44: Science Department Relational Model - Donald Bren School of

Example

street city zip

Assume we create a single entity with the three attributes.The only constraints that can be applied are the key constraints{street, city} and {street, zip} are keys.This however, does not prevent presence of two catalog objects:(kirby, champaign, 61801)(florida, urbana, 61801)which should be prevented since zip uniquely determines a city

catalog

ER to Relational Mapping

Page 45: Science Department Relational Model - Donald Bren School of

Example street zip code

Lets try creating an entity for each attribute and a relationship involving each entity.We can now use cardinality constraints to get the required constraints?Notice that street city uniquely determine zip, so relationship is functional wrt zip.Similarly, street zip uniquely determine city, so relationship is functional wrt city.But how can we model a constraint zip determines the city which involves only two

entities using a ternary relation?This shema will also not prevent the catalog objects:(kirby, champaign, 61801)(florida, urbana, 61801)which should be prevented since a zip uniquely determines a city !!

street catalog zip

city city

n1

1

ER to Relational Mapping

Page 46: Science Department Relational Model - Donald Bren School of

Example

Will this do?No! since city-of may be an empty relationship and will thus not prevent(kirby, champaign, 61801)(florida, urbana, 61801)which should be prevented since a zip uniquely determines a city!!Actually, it can be formally shown that no ER schema can be used to represent theconstraints in this example(you should try other possibilities at home to convince yourself)

street catalogzip

city

n 1

1cityof

n

1

street Zip code

ER to Relational Mapping

Page 47: Science Department Relational Model - Donald Bren School of

� Conceptual Modelling -- ER diagrams� ER schema transformed to relational schema (ER to relational

mapping).� Add additional constraints at this stage to reflect real world.� Resulting relational schema normalized to generate a good schema

(schema normalization process)- avoid redundancy- avoid anomalies- semantically equivalent to the original schema

� Schema is tested example databases to evaluate its quality� Correctness results analyzed and corrections to schema are made� Corrections may be translated back to conceptual model to keep the

conceptual description of data consistent (relations to ER mapping).� We have seen the ER to relation mapping. We next study normalization

process.

ER to Relational Mapping