Introduction to Data Modeling

advertisement
INTRODUCTION TO DATA
MODELING
CS 260
Database Systems
Overview


Introduction to data models
Entity-relationship (E-R) model components
 Entities
 Attributes
 Relationships
Introduction to Data Models


A data model is a precise description of the data to
be contained in a system
Data model levels: Conceptual, Logical, Physical
 Conceptual
 Describes
the data contained by a system at a high level
 Focuses on the data entities and their relationships
 Design should be independent of storage mechanism (e.g.
database)
 Should be general enough to apply to multiple logical
models
Introduction to Data Models

Data model levels (continued)

Logical
Describes the data contained by a system in more detail
 Application of the conceptual model on a particular storage
mechanism (e.g. relational database)
 Should be general enough to apply to multiple physical models
 For relational databases, the logical model may be very similar to
the conceptual model


Physical
Describes the data contained by a system with the most detail
 Application of the logical model on a specific implementation of a
particular storage mechanism (e.g. Oracle relational database)
 Implemented for relational databases using DDL

Introduction to Data Models

Why create data models?
Data models aid in the development of a complete, sound
database design that does not contain redundant data
values which can become inconsistent
 Examples of redundant data on the next slide

What happens if a department changes its name or gets a new
manager? Are there any problems with this data already?
 What happened with Joyce A. English?
 What happened with Product X?
 If EMPLOYEE_PROJECTS is the only place where a project’s name
and location are associated, what happens if Franklin T. Wong
quits?

Introduction to Data Models
EMPLOYEE (SSN, Ename, Bdate, Address, Dnumber, Dname, DMgrSSN)
SSN
123456789
EName
Smith, John B.
Bdate
09-Jan-55
333445555
999887777
987654321
666884444
Wong, Franklin T.
Zelaya, Alicia J.
Wallace, Jennifer S.
Narayan Ramesh K..
08-Dec-45
19-Jul-61
19-Jun-31
15-Sep-52
453453453
987987987
888665555
English, Joyce A.
Jabbar, Ahmad V.
Borg, James E.
31-Jun-62
29-Mar-59
10-Nov-27
Address
731 Fondren, Houston,
TX
638 Voss, Houston, TX
3321 Castle, Spring, TX
291 Berry, Bellaire, TX
975 FireOak, Humble,
TX
5631 Rice, Houston, TX
980 Dallas, Houston, TX
450 Stone, Houston, TX
Dnumber
5
DName
Research
DMgrSSN
333445555
5
4
4
5
Research
Administration
Administration
Research
333444555
987654321
987654321
333445555
5
4
1
Research
Administration
Headquarters
333445555
987654321
888665555
EMPLOYEE-PROJECTS (SSN, ProjNumber, Hours, ProjName, ProjLoc)
SSN
123456789
123456789
666884444
453453453
453453453
333445555
333445555
333445555
333445555
EName
Smith, John B.
Smith, John B.
Narayan, Ramesh K.
English, Joyce A.
Meadows, Joyce A.
Wong, Frank
Wong, Frank
Wong, Franklin T.
Wong, Franklin T.
ProjNumber
1
2
3
1
2
2
3
10
20
Hours
32.5
7.5
40.0
20.0
20.0
10.0
10.0
10.0
10.0
ProjName
ProductX
ProductY
ProductZ
ProductX
ProductY
ProductY
ProductZ
Computerization
Reorganization
ProjLocation
Stafford
Sugarland
Houston
Bellaire
Sugarland
Bellaire
Houston
Stafford
Houston
Overview


Introduction to data models
Entity-relationship (E-R) model components
 Entities
 Attributes
 Relationships
Entity-Relationship Models

This classic data model addresses data and the
relationships between that data
 Developed
to facilitate database design
 Many extensions and variations exist

Components
 Entities
 Attributes
 Relationships
Overview


Introduction to data models
Entity-relationship (E-R) model components
 Entities
 Attributes
 Relationships
Entities

Entity
 An
entity is a “thing” or “object” in the real world that is
distinguishable from all other objects
 Similar to a Java class
 Examples
 Customer
 Product
 Naming
 Use
conventions
short, singular, descriptive names
 Logical models for a relational database should use names
that consist of multiple words and be unique
Entities

Entity instance
A
specific occurrence of an entity
 Similar to a Java object
 An entity must have multiple entity instances
 Examples
 The
customer identified by “Bobby Bon Bons”
 The product identified by “Celestial Cashew Crunch”
Overview


Introduction to data models
Entity-relationship (E-R) model components
 Entities
 Attributes
 Relationships
Attributes

Attribute
 An
attribute is a descriptive property possessed by
each entity instance
 Examples
 Name
 Price
 Naming
 Use
conventions
short, descriptive names
 Logical models for relational databases should use names
that consist of multiple words and are appropriately unique
Attributes

Attribute value
 An
attribute value is the value of a particular attribute
for a particular entity instance
 Examples
 Bobby
Bon Bons
 1.5
 If
an attribute’s value uniquely identifies an entity
instance, then that attribute is a key
Attributes

Attribute characteristics
 Atomic
(simple) vs. composite
 Atomic:
cannot be reasonably divided into subparts
 Composite: can be reasonably divided into subparts
 Examples


Atomic: 105 Garfield Ave.
Composite: 105 Garfield Ave., Eau Claire, WI 54701
Attributes

Attribute characteristics
 Single-valued
vs. multivalued
 Single-valued:
always only one attribute value per attribute
 Multivalued: multiple attribute values per attribute possible
 Examples


Phone number: (555) 123-4567
Phone numbers: (555) 123-4567, (555) 987-6543
Attributes

Attribute characteristics
 Derived
A
derived attribute is one who’s value can be inferred from
the value of other attributes or entities
 Example

Age can be inferred if a date of birth attribute is present for the
associated entity
Overview


Introduction to data models
Entity-relationship (E-R) model components
 Entities
 Attributes
 Relationships
Relationships

Relationship
A
relationship is an association among entities
 Entities
“participate” in relationships
A
relationship may also have attributes
 An entity may have a specified “role” that indicates
how that entity participates in the relationship
 Examples
A
customer has a customer type
 A purchase consists of a customer and a product
Relationships

Relationship constraints
 Cardinalities
 Cardinalities
express the number of entity instances that
participate in a relationship
 Types



One-to-one (1:1)
One-to-many (1:M)
Many-to-many (M:M)
Relationships

Cardinality types
 One-to-one
(1:1)
 An
entity instance A is associated with at most one entity
instance B, and an entity instance B is associated with at
most one entity instance A
 Example

 Are
In a system that has an Employee entity and a Spouse entity, a
1:1 relationship exists between them
 An employee can have at most one spouse, and a spouse
can be associated with at most one employee
there any 1:1 relationships in our candy database?
Sample Database (CANDY)
CANDY_CUSTOMER CUST_ID
CUST_NAME
CUST_TYPE
CUST_ADDR
CUST_ZIP
CUST_PHONE USERNAME PASSWORD
1 Jones, Joe
P
1234 Main St.
91212 434-1231
jonesj
2 Armstrong,Inc.
R
231 Globe Blvd.
91212 434-7664
armstrong
3 Sw edish Burgers
R
1889 20th N.E.
91213 434-9090
sw edburg
4 Pickled Pickles
R
194 CityView
91289 324-8909
pickpick
5 The Candy Kid
W
2121 Main St.
91212 563-4545
kidcandy
6 Waterman, Al
P
23 Yankee Blvd.
91234
w ateral
7 Bobby Bon Bons
R
12 Nichi Cres.
91212 434-9045
bobbybon
8 Crow sh, Elias
P
7 77th Ave.
91211 434-0007
crow el
9 Montag, Susie
P
981 Montview
91213 456-2091
montags
10 Columberg Sw eets W
239 East Falls
91209 874-9092
columsw e
1234
3333
2353
5333
2351
8900
3011
1033
9633
8399
CANDY_PURCHASE
PURCH_ID
CANDY_CUST_TYPE
PROD_ID
CUST_ID
PURCH_DATE DELIVERY_DATE POUNDS
STATUS
1
1
5
28-Oct-04
28-Oct-04
3.5 PAID
CUST_TYPE_ID
CUST_TYPE_DESC
2
2
6
28-Oct-04
30-Oct-04
15 PAID
P
Private
3
1
9
28-Oct-04
28-Oct-04
2 PAID
R
Retail
3
3
9
28-Oct-04
28-Oct-04
3.7 PAID
Wholesale
4
3
2
28-Oct-04
5
1
7
29-Oct-04
29-Oct-04
3.7 NOT PAID
5
2
7
29-Oct-04
29-Oct-04
1.2 NOT PAID
5
3
7
29-Oct-04
29-Oct-04
4.4 NOT PAID
6
2
7
29-Oct-04
W
CANDY_PRODUCT
PROD_ID
PROD_DESC
PROD_COSTPROD_PRICE
3.7 PAID
3 PAID
1 Celestial Cashew Crunch
$
7.45
$
10.00
7
2
10
29-Oct-04
14 NOT PAID
2 Unbrittle Peanut Paradise
$
5.75
$
9.00
7
5
10
29-Oct-04
4.8 NOT PAID
3 Mystery Melange
$
7.75
$
10.50
8
1
4
29-Oct-04
4 Millionaire’s Macadamia Mix
$
12.50
$
16.00
8
5
4
29-Oct-04
5 Nuts Not Nachos
$
6.25
$
9.50
9
5
4
29-Oct-04
29-Oct-04
1 PAID
7.6 PAID
29-Oct-04
3.5 NOT PAID
Relationships

Cardinality types
 One-to-many
(1:M)
 An
entity instance A is associated with zero or more
instances of entity B, and an entity instance B can be
associated with at most one entity instance A
 Example

A
In a system that has a Store entity and a Product entity, a 1:M
relationship exists between them
 A single store can have multiple products, but a product can
belong to at most one store
many-to-one relationship (M:1) exists as well to indicate
the inverse of a 1:M relationship
 Are there any 1:M relationships in our candy database?
Sample Database (CANDY)
CANDY_CUSTOMER CUST_ID
CUST_NAME
CUST_TYPE
CUST_ADDR
CUST_ZIP
CUST_PHONE USERNAME PASSWORD
1 Jones, Joe
P
1234 Main St.
91212 434-1231
jonesj
2 Armstrong,Inc.
R
231 Globe Blvd.
91212 434-7664
armstrong
3 Sw edish Burgers
R
1889 20th N.E.
91213 434-9090
sw edburg
4 Pickled Pickles
R
194 CityView
91289 324-8909
pickpick
5 The Candy Kid
W
2121 Main St.
91212 563-4545
kidcandy
6 Waterman, Al
P
23 Yankee Blvd.
91234
w ateral
7 Bobby Bon Bons
R
12 Nichi Cres.
91212 434-9045
bobbybon
8 Crow sh, Elias
P
7 77th Ave.
91211 434-0007
crow el
9 Montag, Susie
P
981 Montview
91213 456-2091
montags
10 Columberg Sw eets W
239 East Falls
91209 874-9092
columsw e
1234
3333
2353
5333
2351
8900
3011
1033
9633
8399
CANDY_PURCHASE
PURCH_ID
CANDY_CUST_TYPE
PROD_ID
CUST_ID
PURCH_DATE DELIVERY_DATE POUNDS
STATUS
1
1
5
28-Oct-04
28-Oct-04
3.5 PAID
CUST_TYPE_ID
CUST_TYPE_DESC
2
2
6
28-Oct-04
30-Oct-04
15 PAID
P
Private
3
1
9
28-Oct-04
28-Oct-04
2 PAID
R
Retail
3
3
9
28-Oct-04
28-Oct-04
3.7 PAID
Wholesale
4
3
2
28-Oct-04
5
1
7
29-Oct-04
29-Oct-04
3.7 NOT PAID
5
2
7
29-Oct-04
29-Oct-04
1.2 NOT PAID
5
3
7
29-Oct-04
29-Oct-04
4.4 NOT PAID
6
2
7
29-Oct-04
W
CANDY_PRODUCT
PROD_ID
PROD_DESC
PROD_COSTPROD_PRICE
3.7 PAID
3 PAID
1 Celestial Cashew Crunch
$
7.45
$
10.00
7
2
10
29-Oct-04
14 NOT PAID
2 Unbrittle Peanut Paradise
$
5.75
$
9.00
7
5
10
29-Oct-04
4.8 NOT PAID
3 Mystery Melange
$
7.75
$
10.50
8
1
4
29-Oct-04
4 Millionaire’s Macadamia Mix
$
12.50
$
16.00
8
5
4
29-Oct-04
5 Nuts Not Nachos
$
6.25
$
9.50
9
5
4
29-Oct-04
29-Oct-04
1 PAID
7.6 PAID
29-Oct-04
3.5 NOT PAID
Relationships

Cardinality types
 Many-to-many
(M:M)
 An
entity instance A is associated with zero or more
instances of entity B, and an entity instance B is associated
with zero or more instances of entity A
 Example

 Are
In a system that has a Student entity and a Course entity, a M:M
relationship exists between them
 A student may take zero or more courses, and a course may
have zero or more enrolled students
there any M:M relationships in our candy database?
Sample Database (CANDY)
CANDY_CUSTOMER CUST_ID
CUST_NAME
CUST_TYPE
CUST_ADDR
CUST_ZIP
CUST_PHONE USERNAME PASSWORD
1 Jones, Joe
P
1234 Main St.
91212 434-1231
jonesj
2 Armstrong,Inc.
R
231 Globe Blvd.
91212 434-7664
armstrong
3 Sw edish Burgers
R
1889 20th N.E.
91213 434-9090
sw edburg
4 Pickled Pickles
R
194 CityView
91289 324-8909
pickpick
5 The Candy Kid
W
2121 Main St.
91212 563-4545
kidcandy
6 Waterman, Al
P
23 Yankee Blvd.
91234
w ateral
7 Bobby Bon Bons
R
12 Nichi Cres.
91212 434-9045
bobbybon
8 Crow sh, Elias
P
7 77th Ave.
91211 434-0007
crow el
9 Montag, Susie
P
981 Montview
91213 456-2091
montags
10 Columberg Sw eets W
239 East Falls
91209 874-9092
columsw e
1234
3333
2353
5333
2351
8900
3011
1033
9633
8399
CANDY_PURCHASE
PURCH_ID
CANDY_CUST_TYPE
PROD_ID
CUST_ID
PURCH_DATE DELIVERY_DATE POUNDS
STATUS
1
1
5
28-Oct-04
28-Oct-04
3.5 PAID
CUST_TYPE_ID
CUST_TYPE_DESC
2
2
6
28-Oct-04
30-Oct-04
15 PAID
P
Private
3
1
9
28-Oct-04
28-Oct-04
2 PAID
R
Retail
3
3
9
28-Oct-04
28-Oct-04
3.7 PAID
Wholesale
4
3
2
28-Oct-04
5
1
7
29-Oct-04
29-Oct-04
3.7 NOT PAID
5
2
7
29-Oct-04
29-Oct-04
1.2 NOT PAID
5
3
7
29-Oct-04
29-Oct-04
4.4 NOT PAID
6
2
7
29-Oct-04
W
CANDY_PRODUCT
PROD_ID
PROD_DESC
PROD_COSTPROD_PRICE
3.7 PAID
3 PAID
1 Celestial Cashew Crunch
$
7.45
$
10.00
7
2
10
29-Oct-04
14 NOT PAID
2 Unbrittle Peanut Paradise
$
5.75
$
9.00
7
5
10
29-Oct-04
4.8 NOT PAID
3 Mystery Melange
$
7.75
$
10.50
8
1
4
29-Oct-04
4 Millionaire’s Macadamia Mix
$
12.50
$
16.00
8
5
4
29-Oct-04
5 Nuts Not Nachos
$
6.25
$
9.50
9
5
4
29-Oct-04
29-Oct-04
1 PAID
7.6 PAID
29-Oct-04
3.5 NOT PAID
Entity-Relationship Models

In a database for an Italian restaurant, match the
following items with their component types:
 “Entrée”
 “Pasta
Primavera”
 “Entrée price”
A.
B.
C.
D.
Entity
Attribute
Entity instance
Attribute instance
Download