CS206 --- Electronic Commerce

Download Report

Transcript CS206 --- Electronic Commerce

The Relational Data Model
Tables
Schemas
Conversion from E/R to Relations
Source: slides by Jeffrey Ullman
1
A Relation is a Table
Attributes
(column
headers)
Tuples
(rows)
name
Snickers
Twizzlers
manf
M&M/Mars
Hershey
Candies
2
Schemas
Relation schema = relation name and
attribute list.
 Optionally: types of attributes.
 Example: Candies(name, manf) or
Candies(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
Candies
Relation: Candies(name, manf)
6
Relationship -> Relation
name
husband
addr
Consumers
1
name
Likes
manf
Candies
2
Buddies
Favorite
wife
Married
Likes(consumer, candy)
Favorite(consumer, candy)
Buddies(name1, name2)
Married(husband, wife)
7
Combining Relations
 OK to combine into one relation:
1. The relation for an entity-set E
2. The relations for many-one relationships
of which E is the “many.”
 Example: Consumers(name, addr) and
Favorite(consumer, candy) combine to
make Consumer1(name, addr,
favCandy).
8
Risk with Many-Many Relationships
Combining Consumers with Likes would
be a mistake. It leads to redundancy,
as:
name
addr
candy
Sally 123 Maple Twizzler
Sally 123 Maple Kitkat
Redundancy
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, nonkey
attributes.
A supporting relationship is redundant
and yields no relation (unless it 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
1. Object-oriented : One relation per subset of
subclasses, with all relevant attributes.
2. Use nulls : One relation; entities have NULL
in attributes that don’t belong to them.
3. E/R style : One relation for each subclass:
 Key attribute(s).
 Attributes of that subclass.
12
Example
name
Candies
manf
isa
color
Chocolates
13
Object-Oriented
name
Twizzler
manf
Hershey
Candies
name
Snickers
manf
color
M&M/Mars light
Chocolates
Good for queries like
“find the color of chocolate candies made by M&M/Mars.”
14
E/R Style
name
Twizzler
Snickers
manf
Hershey
M&M/Mars
Candies
name
Snickers
color
light
Chocolates
Good for queries like
“find all candies (including chocolates) made
by M&M/Mars.”
15
Using Nulls
name
Twizzler
Snickers
manf
Hershey
M&M/Mars
Candies
color
NULL
dark
Saves space unless there are lots
of attributes that are usually NULL.
16