Transcript er-rel

The Relational Data
Model
Conversion from E/R to Relations
Based on the notes by Jeff Ullman, 2004
1
A Relation is a Table
Attributes
(column
headers)
Tuples
(rows)
name
manf
Sleeman 20th
Anniversary Ale
Relation
name
Sleeman Brewing
& Malting Co.
Beers
2
Schemas

Relation schema = relation name and
attribute list




Optionally: types of attributes.
Example: Beers(name, manf) or Beers(name:
string, manf: string)
Database = collection of relations.
Database schema = set of all relation
schemas in the database
3
Why Relations?

Very simple model

Often matches how we think about data

Abstract model that underlies SQL, the most
important database language today.
4
From E/R Diagrams to
Relations

Entity set  relation.


Attributes  attributes.
Relationships  relations whose attributes
are only:


The keys of the connected entity sets.
Attributes of the relationship itself.
5
Entity Set  Relation
name
manf
Beers
Relation: Beers(name, manf)
6
Relationship  Relation
name
addr
name
Drinkers
Likes
manf
Beers
husband
1
2
Favorite
Buddies
wife
Married
Likes(drinker, beer)
Favorite(drinker, beer)
Buddies(name1, name2)
Married(husband, wife)
7
Combining Relations

OK to combine into one relation:



The relation for an entity-set E
The relations for many-one relationships of which
E is the “many.”
Example: Drinkers(name, addr) and
Favorite(drinker, beer) combine to make
Drinker1(name, addr, favBeer).
8
Risk with Many-Many
Relationships

Combining Drinkers with Likes would be a
mistake. It leads to redundancy, as:
name
addr
beer
Denis
<classified>
Sleeman’s
Honey Brown
Denis
<classified>
Creemore
Springs
Lager
9
Handling Weak Entity Sets


Relation for a weak entity set must include
attributes for its complete key (including
those belonging to other entity sets), as well
as its own, non-key attributes.
A supporting relationship is redundant and
yields no relation

unless the relationship has attributes
10
Example
name
billTo
Logins
name
At
Hosts
location
Hosts(hostName, location)
Logins(loginName, hostName, billTo)
At(loginName, hostName, hostName2)
At becomes part of
Logins
Must be the same
11
Subclasses: Three
Approaches



Object-oriented: One relation per subset of
subclasses, with all relevant attributes.
Use nulls: One relation; entities have NULL in
attributes that don’t belong to them.
E/R style: One relation for each subclass:


Key attribute(s).
Attributes of that subclass.
12
Example
name
Beers
manf
isa
color
Ales
13
Object-Oriented
name
manf
Canadian
Original Dark
Molson
Sleeman
Beers
name
manf
color
Original Dark
Sleeman
Dark
Ales
Good for queries like “find the
color of ales made by Sleeman’s.”
14
E/R Style
name
manf
Canadian
Original Dark
Molson
Sleeman
Beers
name
color
Original Dark
Dark
Ales
Good for queries like “find all beers (including ales) made
by Sleeman.”
15
Using Nulls
name
manf
color
Original Dark
Canadian
Sleeman
Molson
Dark
NULL
Beers
Saves space unless there are lots
of attributes that are usually NULL.
16
A mathematical
diversion
Introduction to Set Theory
Based on the notes by James H. Steiger
17
Sets





Definition. A Set is any well-defined collection of
“objects.”
Definition. The elements of a set are the objects in
a set.
Notation. Usually we denote sets with upper-case
letters, elements with lower-case letters. The
following notation is used to show set membership
x  A means that x is a member of the set A
x  A means that x is not a member of the set
A.
18
Ways of Describing Sets

List the elements
A= 1,2,3,4,5,6

Give a verbal description


“A is the set of all integers from 1 to 6, inclusive”
Give a mathematical inclusion rule
A=Integers x 1  x  6
19
Some Special Sets

The Null Set or Empty Set. This is a set with no
elements, often symbolized by


The Universal Set. This is the set of all elements
currently under consideration, and is often
symbolized by

20
Membership Relationships

Definition. Subset.
A  B “A is a subset of B”
We say “A is a subset of B” if x  A  x  B , i.e., all
the members of A are also members of B. The
notation for subset is very similar to the notation for
“less than or equal to,” and means, in terms of the
sets, “included in or equal to.”
21
Membership Relationships

Definition. Proper Subset.
A  B “A is a proper subset of B”
We say “A is a proper subset of B” if all the
members of A are also members of B, but in addition
there exists at least one element c such that
but c  B. Thecnotation
 A for subset is very similar to
the notation for “less than,” and means, in terms of
the sets, “included in but not equal to.”
22
Combining Sets – Union

A B

“A union B” is the set of all elements that are
in A, or B, or both.

This is similar to the logical “or” operator.
23
Combining Sets – Intersection



A B
“A intersect B” is the set of all elements that
are in both A and B.
This is similar to the logical “and”
24
Set Complement



A
“A complement,” or “not A” is the set of all
elements not in A.
The complement operator is similar to the
logical not, and is reflexive, that is,
A A
25
Set Difference


A B
The set difference “A minus B” is the set of
elements that are in A, with those that are in
B subtracted out. Another way of putting it is,
it is the set of elements that are in A, and not
in B, so
A B  A B
26
Examples
  {1,2,3,4,5,6}
A  {1,2,3}
B  {3,4,5,6}
A  B  {3}
A  B  {1,2,3,4,5,6}
B  A  {4,5,6}
B  {1,2}
27
Venn Diagrams

Venn Diagrams use topological areas to
stand for sets
B
A
AB
28
Venn Diagrams

Try this one!
B
A
AB
29
Venn Diagrams

Here is another one
B
A
AB
30
Mutually Exclusive and
Exhaustive Sets


Definition. We say that a group of sets is
exhaustive of another set if their union is
equal to that set. For example, if A  B  C
we say that A and B are exhaustive with
respect to C.
Definition. We say that two sets A and B are
mutually exclusive if A  B   , that is, the
sets have no elements in common.
31
Set Partition

Definition. We say that a group of sets partitions
another set if they are mutually exclusive and
exhaustive with respect to that set. When we
“partition a set,” we break it down into mutually
exclusive and exhaustive regions, i.e., regions with
no overlap. The Venn diagram below should help
you get the picture. In this diagram, the set A (the
rectangle) is partitioned into sets W,X, and Y.
32
Set Partition
A
W
X
Y
33
Some Test Questions
A  ?
34
Some Test Questions
A  A=?
35
Some Test Questions
A  ?
36
Some Test Questions
A  A=?
37
Some Test Questions
AA  ?
38
Some Test Questions
A  ?
39
Some Test Questions
  ?
40
Some Test Questions
If A  B then
AB ?
41
Some Test Questions
If A  B then
AB ?
42