N-ary Relations and Their Applications Rosen 8.2.

20
n-ary Relations and Their Applications Rosen 8.2

Transcript of N-ary Relations and Their Applications Rosen 8.2.

Page 1: N-ary Relations and Their Applications Rosen 8.2.

n-ary Relations and Their Applications

Rosen 8.2

Page 2: N-ary Relations and Their Applications Rosen 8.2.

n-ary relations

We can have relation between more than just 2 sets

A binary relation involves 2 sets and can be described by a set of pairsA ternary relation involves 3 sets and can be described by a set of triples…

An n-ary relation involves n sets and can be described by a set of n-tuples

Relations are used to represent computer databases

Also used to represent constraints!

Page 3: N-ary Relations and Their Applications Rosen 8.2.

n-ary relations n-ary Relations

n

domainsAAA

AAAaryn

AAA

n

n

n

isrelation theof degree The

relation theof theare ,,, sets The

product cartesian theofsubset a isrelation An

sets be ,,Let

21

21

2,1

Page 4: N-ary Relations and Their Applications Rosen 8.2.

n-ary relations n-ary Relations

cbacba

NNNR

such that ),,(

triplesof consisting on relation thebe Let

Note: N is the set of natural numbers {0,1,2,3,…}

An example

}),3,2,1(,),4,2,0(),3,2,0(,),3,1,0(),2,1,0{( R

R)3,4,2(

Note: R could be considered as an extensional representation of the ternary relation a<b<c, assuming domains are finite and really quite small

The relation has degree 3

The domains of the relation are the set of natural numbers

Page 5: N-ary Relations and Their Applications Rosen 8.2.

n-ary relations n-ary Relations

)0()(such that ),,,(

tuples-4 of consisting on relation thebe Let

dcbadcbadcba

ZNZNR

Note: N is the set of natural numbers {0,1,2,3,…} Z is the set of integers {…,-2,-1,0,1,2,…}

An example

Note: R could be considered as an extensional representation of the ternary relation a<b<c, assuming domains are finite and really quite small

The relation has degree 4R

R

R

)9,3,6,6(

)3,3,11,5(

)0,1,1,0(

Page 6: N-ary Relations and Their Applications Rosen 8.2.

Relational databases

Database is made up of records.Typical operations on a database are

• find records that satisfy a given criteria• delete records• add records• update records

Some everyday databases• student records• health records• tax information• telephone directories• banking records• …

Databases may be represented using the relational model

Page 7: N-ary Relations and Their Applications Rosen 8.2.
Page 8: N-ary Relations and Their Applications Rosen 8.2.
Page 9: N-ary Relations and Their Applications Rosen 8.2.

Relational databases The relational data model

Database made up of records, they are n-tuples, made up of fields

Student record might look as follows

(name,metricNo,faculty,gpa)

(Jones,200401986,Arts,4.9)(Lee,200408972,Science,3.6)(Kuhns,200501728,Humanities,5.0)(Moore,200308327,Science,5.5)

relations (in relDB) also called tables

gpa is an attribute

Page 10: N-ary Relations and Their Applications Rosen 8.2.

Relational databases The relational data model

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

Example from the book

Attributes: name, metric No, Dept and GPA

Page 11: N-ary Relations and Their Applications Rosen 8.2.

Relational databases The relational data model

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

primary key:An attribute/domain/column is a primary key whenthe value of this attribute uniquely defines tuplesi.e. no two tuples have the same value for that attribute

Name cannot be a primary key, neither can Dept or GPSmetricNo is a primary key

Page 12: N-ary Relations and Their Applications Rosen 8.2.

Relational databases The relational data model

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

The current collection of n-tuples (records) in the relation (table)is called the extension of the relation

The permanent aspects of the relation (table) such as the attribute namesis called the intention of the relation

Page 13: N-ary Relations and Their Applications Rosen 8.2.

Relational databases The relational data model

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

A composite key is a combination of attributesThat uniquely define tuples

Page 14: N-ary Relations and Their Applications Rosen 8.2.

Relational databases Operations on n-ary relations

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

CRtuplesn

arynRS

RCarynR

c

condition esatisfy th that from

all ofrelation new the to maps operator selection The

satisfy.must in elementsthat condition a andrelation an be Let

Selection

Page 15: N-ary Relations and Their Applications Rosen 8.2.

Relational databases Operations on n-ary relations

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

CRtuplesn

arynRS

RCarynR

c

condition esatisfy th that from

all ofrelation new the to maps operator selection The

satisfy.must in elementsthat condition a andrelation an be Let

Selection

3.45GPA condition theis where

operator selection Apply the

C

Sc

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

Page 16: N-ary Relations and Their Applications Rosen 8.2.

Relational databases Operations on n-ary relations

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

Projection

nmaaa

tuplemaaatuplen

iiiPprojection

m

m

iii

n

miii

where),,,(

the to),,,( themaps

where The

21

21

21

21

It strips out specific columns

Page 17: N-ary Relations and Their Applications Rosen 8.2.

Relational databases Operations on n-ary relations

2.99Psychology786576Stevens

3.90sMathematic678543Rao

3.49sMathematic453876Goodfriend

3.49ScienceComputer 102147Chou

3.45Physics888323Adams

3.88ScienceComputer 231455Ackermann

GPADeptmetricNoName

Projection

nmaaa

tuplemaaatuplen

iiiPprojection

m

m

iii

n

miii

where),,,(

the to),,,( themaps

where The

21

21

21

21

4,1 projection Apply the P

2.99Stevens

3.90Rao

3.49Goodfriend

3.49Chou

3.45Adams

3.88Ackermann

GPAName

Page 18: N-ary Relations and Their Applications Rosen 8.2.

Relational databases Operations on n-ary relationsJoin

Joins two tables/relations together, matching up on specific attributes

575sMathematic

518ScienceComputer

551Physics

544Physics

617Psychology

501Psychology

412Zoology

335Zoology

Rosen

Rosen

Grammer

Grammer

Faber

Faber

Cruz

Cruz

CourseDeptLecturer

08.00A100412Zoology

09.00A100335Zoology

11.00A110617Psychology

15.00A100501Psychology

16.00B505544Physics

16.00N521611sMathematic

15.00N502575sMathematic

14.00N521518ScienceComputer

TimeRoomCourseDept

4)Defn p.534Rosen (seematch attributes goverlappin the where from

come attributes last theand come attributes first thesuch that

degree ofrelation new a delivers and andly respective

relations and are and where),(operator join The

pS

pnRpm

pnmnpmp

arynarymSRSRJ p

Page 19: N-ary Relations and Their Applications Rosen 8.2.

Relational databases Operations on n-ary relationsJoin

R

Rosen

Rosen

Grammer

Grammer

Faber

Faber

Cruz

Cruz

CourseDeptLecturer

Relation

575sMathematic

518ScienceComputer

551Physics

544Physics

617Psychology

501Psychology

412Zoology

335Zoology

S

TimeRoomCourseDept

Relation

08.00A100412Zoology

09.00A100335Zoology

11.00A110617Psychology

15.00A100501Psychology

16.00B505544Physics

16.00N521611sMathematic

15.00N502575sMathematic

14.00N521518ScienceComputer

),(2 SRJ

15.00N502575sMathematicRosen

14.00N521518ScienceComputer Rosen

16.00B505544PhysicsGrammer

11.00A110617PsychologyFaber

15.00A100501PsychologyFaber

08.00A100412ZoologyCruz

09.00A100335ZoologyCruz

TimeRoomCourseDeptLecturer

Page 20: N-ary Relations and Their Applications Rosen 8.2.

Explain the previous slide

Explain how what we do differs from what will be presented in IM2