COP 5725 Fall 2012
Database Management
Systems
Chap 2. Introduction to
Database Design
Database Design Steps
• Requirements Analysis
• Conceptual Database Design
– E/R model
• Logical Database Design
– E.g., relational data model
– Output: logical/conceptual schema
Purpose of E/R Model
• The E/R model allows us to sketch database designs.
– Kinds of data and how they connect.
• Designs are pictures called
entity-relationship diagrams
.Entity Sets
•
Entity
= “thing” or object.– A student
•
Entity set
= collection of similar entities.– Students in UF
•
Attribute
= property of (the entities of) an entity set.E/R Diagrams
• In an entity-relationship diagram:
– Entity set = rectangle.
– Attribute = oval, with a line to the rectangle representing its entity set.
Example: a Multi-attribute Key
Courses
dept number hours room
A more fun Running Example
• Entity set Beers has two attributes, name and
manf (manufacturer).
• Each Beers entity has values for these two attributes, e.g. (Bud, Anheuser-Busch)
Beers
Relationships
• A relationship connects two or more
entity sets.
• It is represented by a diamond, with
Example
Drinkers like some beers. Frequents
Relationship Set
• For the relationship Sells, we might have a relationship set like:
Bar Beer
Joe’s Bar Bud
Joe’s Bar Miller
Attributes on Relationships
Bars Sells Beers
Multiway Relationships
Bars Beers
name addr name manf
license
A Ternary Relationship Set
Bar Drinker Beer
Joe’s Bar Ann Miller
Sue’s Bar Ann Bud
Sue’s Bar Ann Pete’s Ale
Joe’s Bar Bob Bud
Joe’s Bar Bob Miller
Joe’s Bar Cal Miller
Roles
• Sometimes an entity set appears more than once in a relationship.
• Label the edges between the
Many-Many Relationships
• Focus: binary relationships, such as
Sells between Bars and Beers.
• In a
many-many
relationship, an entity of either set can be connected to many entities of the other set.Many-One / One-Many
Relationships
manyone
One-One Relationships
Representing “Multiplicity”
• In E/R, a many-one relationship is
represented by an arrow entering the
“one” side (i.e., at most one).
• Show a one-one relationship by arrows entering both entity sets.
• Rounded arrow = “exactly one,” i.e., at
Example (I)
Drinkers Likes Beers
Example (II)
Manfs Best- Beers
Example (III)
Bars Beers
name addr name manf
license
Weak Entity Sets
• Occasionally, entities of an entity set
need “help” to identify them uniquely.
Players Plays- Teams on
name number name
Weak Entity-Set Rules
• A weak entity set has one or more many-one relationships to other (supporting) entity sets.
– Not every many-one relationship from a weak entity set need be supporting.
– Many-one with total participation
Subclasses
Beers
Ales isa
name manf
Conceptual Design with E/R Model
• Choices in developing E/R diagram
– Avoid Redundancy
– Entity vs. Attribute
– Entity vs. relationship
Entity vs. Attributes
• Create an entity set representing values of the attribute.
• Make that entity set participate in the relationship.
Bars Sells Beers
Entity vs. Attributes (II)
Bars Sells Beers
Prices
Note convention: arrow from multiway relationship
Design Techniques
1. Avoid redundancy.
2. Don’t use an entity set when an
attribute will do.
Avoiding Redundancy
•
Redundancy
occurs when we say thesame thing in two or more different ways.
• Redundancy wastes space and (more importantly) encourages inconsistency.
Example (I)
Beers ManfBy Manfs
Example (II)
Beers ManfBy Manfs
name name
manf
Example (III)
Beers name
This design repeats the manufacturer’s address once for each beer and loses the address if there
Entity Sets Versus Attributes
• An entity set should satisfy at least one of the following conditions:
– It is more than the name of something; it has at least one nonkey attribute.
or
Example (I)
Beers ManfBy Manfs
name
•Manfs deserves to be an entity set because of the nonkey attribute addr.
•
Example (II)
Beers name
There is no need to make the manufacturer an
Example (III)
Beers ManfBy Manfs
name
Since the manufacturer is nothing but a name, and is not at the “many” end of any relationship,
Don’t Overuse Weak Entity Sets
• Beginning database designers often doubt that anything could be a key by itself.
– They make all entity sets weak, supported by all other entity sets to which they are linked.
• In reality, we usually create unique ID’s for
When Do We Need Weak Entity
Sets?
• The usual reason is that there is no global authority capable of creating
unique ID’s.
• Example: it is unlikely that there could be an agreement to assign unique
Key Constraints
• Key constraints on relations
– Superkeys
– Keys (minimal)/Candidate keys
– Primary keys
• Key constraints on relationships
– Many-One/One-Many
More IC: Participation Constraints
• Does every Manf have a Best-seller?
– If so, this is a participation constraint: the
participation of Manf in Best-seller is said to be
total (vs. partial).
• Every Manf entity must appear in an instance of the Best Seller relationship.
IC in ER Model
• Constraints in the ER Model:
– A lot of data semantics can (and should) be captured.
– But some constraints cannot be captured in ER diagrams (e.g., functional
More example
Manages Departments Employees
At most, at least and exactly one!
Manfs Best- Beers
Aggregation
name Projects SponsorsEmployees
Monitors
lot ssn
Other Choices in E/R Design
• Attribute vs. Entity (more examples)
• Entity vs. Relationship
Entity vs. Attribute
• Should
price address
be an attribute / entity1. If we have several prices per beer (e.g., seasonal) or several addresses per bar
2. If the structure (street, city, etc.) is important, e.g., we want to retrieve bars in a given city
3. otherwise, simpler representation (i.e., attribute) would better to avoid overhead
Entity vs. Relationship
• What if a manager gets a discretionary
budget that covers
all managed depts?
– Redundancy: dbudget
stored for each dept managed by manager. – Misleading: Suggests
dbudget associated with department-mgr
Employees Departments
Binary vs. Ternary Relationships
• If each policy is owned by just 1 employee, and each dependent is tied to the
covering policy • What are the
additional
constraints in the
age
policyid cost
Beneficiary
Binary vs. Ternary Relationships
(Contd.)
• Previous example illustrated a case when two binary relationships were better than one ternary relationship.
• An example in the other direction: a ternary relation Favorite relates entities Drinkers,
Summary of Conceptual Design
• Conceptual design follows requirements analysis,
– Yields a high-level description of data to be stored • ER model popular for conceptual design
– Constructs are expressive, close to the way people think about their applications.
• Basic constructs: entities, relationships, and attributes (of entities and relationships).
Summary of ER (Contd.)
• Several kinds of integrity constraints can be expressed in the ER model: e.g.
key
constraints
,participation
constraints
,foreign
key constraints
.– Some constraints (notably, candidate keys,
Summary of ER (Contd.)
• ER design is
subjective
. There are often many ways to model a given scenario! Analyzing alternatives can be tricky,especially for a large enterprise. Common choices include:
– Entity vs. attribute, entity vs. relationship,