Title: Relations
1Relations
- Two Views/Definitions of a Relation
- a) A set of n-tuples satisfying a certain n-ary
predicate defines a relation, R, among the
n-tuples. - b) An n-ary relation, R, is a set of n-tuples
that satisfy an n-ary predicate - Just providing a set of n-tuples as the first
definition suggests does not easily allow us to
see the relation, R. - e.g. (2,3), (3,2), (-2,-3), (-3,-2),
(1,6),(6,1), (-1,-6),(-6,-1) - Defining the relation, R, makes it a lot clearer.
- e.g. a,b integers I ab 6 (a,b)
- Recall that in predicates, the order of the
elements is important. Similarly, the order of
the elements is important in a relation.
2Binary Relation
- A relation, R, which has only two elements is
called a binary relation, and it is often the
focus in this course. - A binary relation, R, is a set of ordered pair
satisfying a binary predicate. - Example
- a,b integers I altb (a,b)
- this is the infinite set (-3,-2), (0,2), - - -
- , (5,9) - - - - - An ordered pair, (x,y), has the following
characteristic - (x,y) (m,n) ? x m and y n
3Expressing Binary Relation
- There are several ways to express a binary
relation, R - x R y
- (x,y) R
- R(x,y).
- So far, we have demonstrated relation with
numbers. The elements for a relation may come
from any set. - e.g. x R y allows x to come from set S1, and y
to be from another set S2. (S1 may be a set of
teachers, S2 may be a set of students, and R is
the relationship of teaches.) -
4Cross Product
- A relation extracted from two sets, M and N, is
usually referred to be a relation over M x N. - M x N is known as the cross product or Cartesian
product of M and N. - The sets M and N may be the same. That is, we can
have M x M as the cross product - Example of a Relation over a Cross Product
- e.g. Let a set of doctors, D d1, d2, a set of
patients, P p1, p2, p3, p4, p5 and let
treats to be the relation, T. Then - D x P (d1,p1), (d1, p2), (d1,p3), (d1,p4),
(d1,p5), (d2,p1), (d2,p2), (d2,p3), (d2,p4),
(d2,p5) - T is a binary relation, treats, defined over D
x P , where T (d1,p1), (d1,p3), (d2, p1),
(d2,p2), (d2,p4), d2,p5) - Relation T states that doctor d1 treats patients
p1 and p3, and doctor d2 treats patients p1, p2,
p4 and p5. Note that p1 is treated by both d1 and
d2.
5More interesting Example of Relation
- Consider a student record that contains a
4-tuple name, age, classification, GPA (n, a,
c, g) - (n, a, c, g) name x age x classification x
GPA - Consider the relation, At_SPSU
- define At_SPSU relation to be the set
- At_SPSU (Jorge, 23, grad, 3.2)
(Jill, 19,soph, 3.7) (Chaudary, 18, fresh,
2.7) (Mike, 21, sen, 2.8) (Wang, 22, grad,
3.4)
6More interesting Example of Relation (cont.)
- Putting AT_SPSU relation in a tabular form as
follows -
- AT_SPSU
Table
name
classif.
age
GPA
grad
Jorge
23
3.2
19
soph
Jill
3.7
Chaudary
18
fresh
2.7
21
sen
Mike
2.8
Wang
22
3.4
grad
GPA gt3.0 ( name , AT_SPSU ) Jorge, Jill,
Wang ClassifGrad (name, AT_SPSU) Jorge,
Wang)
7More interesting Example of Relation (cont.)
- Putting AT_SPSU relation in a tabular form as
follows -
- AT_SPSU
Table
name
classif.
age
GPA
grad
Jorge
23
3.2
19
soph
Jill
3.7
Chaudary
18
fresh
2.7
21
sen
Mike
2.8
Look familiar to some of you ?
Wang
22
3.4
grad
Select name from AT_SPSU where GPA gt 3.0 Select
name from AT_SPSU where classif Grad
8Simple Operators on Relations
- Domain operator, dom, applied on a binary
relation provides the left elements of the
ordered pairs. - Example
- Users jim, tom, sally, joe, kim
- Files f1, f2, f3, f4, f7, f9, f23
- File_owners is defined over Users x Files
- File_owners (jim, f2), (jim, f4), (joe, f9)
- dom ( File_owners) jim, Joe
What does (dom(File_owners) Joe) files,
File_Owners mean to you?
9Simple Operators on Relations
- Range operator, ran, applied on a binary relation
provides the right elements of the ordered
pairs. - Example
- Users jim, tom, sally, joe, kim
- Files f1, f2, f3, f4, f7, f9, f23
- File_owners is defined over Users x Files
- File_owners (jim, f2), (jim, f4), (joe, f9)
- ran (File_owners) f2, f4, f9
10Inverse Operator
- The inverse operator applied to a relation, R,
reverses the relation. - If R is a relation, then R-1 is the inverse of R.
- Formally (a,b) R ? (b,a) R-1
- Example
- let relation, R (-6, 12), (-7, 14), (-12,
24) - Then R-1 (12, -6), (14, -7), (24, -12)
Note that dom R ran R-1
11some English statements relations
- High-Priority jobs are those with priority levels
of 5 or above. - Let the set of jobs be J
- Let the set of priority levels be P
- The cross product is J x P, and
High-Priority-jobs is a relation over J x P - High-Priority-jobs a J, b P I bgt5 (a,
b) - Customers who purchased more than 1,000
- Let the set of customer be C
- Let the set of dollars spent be Spent
- The cross product is C x Spent
- dom a C, b Spent I b gt1000 (a, b)
Are these formal definitions more helpful under
some circumstances? Your thoughts?