data model 21 after this lecture, you should be able to: use e-r model effectively to design a...

26
Data Model 2 After this lecture, you should be able to: Use E-R Model effectively to design a database for a mail order company. Know more features of ER Diagram: Weak Entity Converting a M:M Relationship Type into Two 1:M Relationship Types Ternary Relationship Type Build Extended ER digram to represent Entity Type Hierarchy (supertype- subtype relationship). Work on Assignment 4. More on E-R Diagrams

Upload: justina-adams

Post on 25-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 1

After this lecture, you should be able to: Use E-R Model effectively to design a

database for a mail order company. Know more features of ER Diagram:

Weak Entity Converting a M:M Relationship Type into

Two 1:M Relationship Types Ternary Relationship Type

Build Extended ER digram to represent Entity Type Hierarchy (supertype-subtype relationship).

Work on Assignment 4.

More on E-R Diagrams

Page 2: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 2

Mail-Order Company DatabaseA small mail-order company must maintain the followinginformation:

The company must keep track of all its customers with their names, addresses, and the dates of their first orders. A unique customer number is assigned to each customer.

Each order placed by a customer may contain multiple order-lines. Each order-line is used to order one kind of product for some quantity. The date of the order must be recorded. Each customer assigns order numbers to his/her orders. Orders from different customers may have identical order numbers.

Each product has a product number, a product name, and a unit retail price.

Page 3: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 3

Mail-Order Company Database (cont'd)

Each product may have multiple suppliers. A unique supplier number is assigned to each supplier. Different suppliers may offer the same product at different wholesale prices. A supplier may supply multiple products. The names and addresses of the suppliers must be recorded.

The company owns multiple warehouses. Each warehouse is identified by the name of the city where it is located. The telephone number of each warehouse must be recorded. The number of each product stocked at each warehouse must be recorded. A warehouse can stock different products. However, each product is stocked at most at one warehouse.

Page 4: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 4

What Should an Entity Be?

“An entity is an object in the real world that is distinguisable from other objects”

SHOULD BE: An object that will have many instances

in the database An object that will be composed of

multiple attributes An object that we are trying to modelSHOULD NOT BE: A user of the database system An output of the database system (e.g., a

report)

Page 5: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 5

Mail-Order Company Database: E-R Diagram

Customer

WarehouseSupplier

Product

Order

place

supplyfor

store

C#

city

since

address

name

quantity

telephone #

O#

quantity

P# name retailPrice

date

wholeSalePrice

address

nameS#

1

M

M

M

M

M M

1

Page 6: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 6

Weak Entities The keys of weak entities are unique only among

their respective parents. Such keys are referred to as weak keys.

In referring to a weak entity, we must use the combination of the weak key of the weak entity and the primary key of the parent entity.

If the order numbers are assigned by customers, the order numbers are weak keys, and hence each order must be identified with (C#, O#).

Weak entities can exist only when their parent entities exist.

Page 7: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 7

Generating Relational Tables Provide a table for each entity type. The attributes

of the entity type become the table columns.

Provide a table for each M : M relationship type. The primary keys of the entity types associated by the relationship type become the foreign keys in the table. The combination of those foreign keys becomes the primary key of the table. Also, the attributes of the relationship type need be added to the table.

A M:1 or 1:M relationship type does not require a new table. The primary key of the entity type on the 1-side can be added as a foreign key to the table representing the entity type on the M-side.

Page 8: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 8

Mail-Order Company Database: E-R Diagram

Customer

WarehouseSupplier

Product

Order

place

supplyfor

store

C#

city

since

address

name

quantity

telephone #

O#

quantity

P# name retailPrice

date

wholeSalePrice

address

nameS#

1

M

M

M

M

M M

1

Page 9: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 9

Mail-Order Company Database: Relational Schema

(1) Customer(C#, name, address, since) (2) Order(C#, O#, Date) (3) For(C#, O#, P#, quantity) (4) Product(P#, name, retailPrice, city, quantity) (5) Supplier(S#, name, address) (6) Supply(S#, P#, wholeSalePrice) (7) Warehouse(city, telephone#)

Page 10: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 10

Converting a M:M Relationship Types into

Two 1:M Relationship Types.

Orderline

Product

Order

for

O#

quantity

P# name retailPrice

date

M

M

Product

Order

O#

P# name retailPrice

date

M

1

M

1

quantity

Ol#

Page 11: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 11

Mail-Order Company Database: Relational Schema

(1) Customer(C#, name, address, since) (2) Order(C#, O#, Date) (4) Orderline(Ol#, C#, O#, P#, quantity) (5) Product(P#, name, retailPrice, city, quantity) (6) Supplier(S#, name, address) (7) Supply(S#, P#, wholeSalePrice) (8) Warehouse(city, telephone#)

Page 12: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 12

Ternary Relationship Type

Supplier supply Part

Name Name

P#(key)

S#(key)

WeightCity Price

Project

NamePJ#

(key)

Page 13: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 13

Table Created from a Ternary Relationship Type

S# PJ# P# Price

S1 PJ1 P1 10

S1 PJ1 P2 20

S1 PJ2 P1 12

S2 PJ1 P1 15

S2 PJ3 P2 22

S3 PJ2 P3 30

Table Supplier-Project-Part

Page 14: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 14

Is This Equivalent to the One Ternary Relationship Type?

Supplier supply Part

Project

participate use

We cannot know which supplier supplied which part with respect to which project.

Page 15: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 15

Convert Ternary To Binary Relationship Type

Supplier Part

Name Name

P#(key)

S#(key)

WeightCity Price

Project

NamePJ#

(key)

Shipment

Page 16: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 16

(Entity) Type Hierarchy

Indicated a supertype-subtype (superclass-subclass) relationship

Is also called an IS-A hierarchy (or relationship)

Example: A car is a vehicle A truck is a vehicle A dump truck is a truck A trailer-truck is a truck

Page 17: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 17

(Entity) Type Hierarchy: Extended ER Schema

Vehicle

Car Truck

DumpTruck TrailerTruck

ownerlicense#color

style

nPassengers

loadWeight

weight

nTrailers

Page 18: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 18

(Entity) Type Hierarchy: Vehicle Database

For each vehicle, record its license number, owner, and color.

For each car, record the number of passengers and style (sedan, convertible, etc.).

For each truck, record the weight of the truck itself.

For each dump truck, record the maximum weight of the load.

For each trailer-truck, record the number of the trailers (1 - 3).

Page 19: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 19

Relational Schema for Type Hierarchy I

Vehicle(license#, owner, color)Car(license#, owner, color, nPassengers, style)Truck(license#, owner, color, weight)DumpTruck(license#, owner, color, weight, loadWeight)TrailerTruck(license#, owner, color, weight, nTrailers)

Store all the information on one entity in one table.

Page 20: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 20

Relational Schema for Type Hierarchy I

Vehicle(license#, owner, color)Car(license#, owner, color, nPassengers, style)Truck(license#, owner, color, weight)DumpTruck(license#, owner, color, weight, loadWeight)TrailerTruck(license#, owner, color, weight, nTrailers)

select * from TrailerTruck;

Get all information on all trailertrucks.

Page 21: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 21

Relational Schema for Type Hierarchy I

Vehicle(license#, owner, color)Car(license#, owner, color, nPassengers, style)Truck(license#, owner, color, weight)DumpTruck(license#, owner, color, weight, loadWeight)TrailerTruck(license#, owner, color, weight, nTrailers)

select licence#, owner, color from Vehicle unionselect licence#, owner, color from Carunionselect licence#, owner, color from Truckunionselect licence#, owner, color from DumpTruckunionselect licence#, owner, color from TrailerTruck;

Get license# and owner of all vehicles.

Page 22: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 22

Relational Schema for Type Hierarchy II

The information on one entity is stored in multiple tables.

When a class hierarchy is relatively flat, query statements in SQL become simpler

Vehicle(license#, owner, color) Car(license#, nPassengers, style)Truck(license#, weight)DumpTruck(license#, loadWeight)TrailerTruck(license#, nTrailers)

Page 23: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 23

SQL Queries for Type II Class Hierarchy

select v.licence#, owner, color, weight, nTrailersfrom Vehicle v, Truck t, TrailerTruck ttwhere v.license# = t.license# and t.license# = tt.license#

Vehicle(license#, owner, color) Car(license#, nPassengers, style)Truck(license#, weight)DumpTruck(license#, loadWeight)TrailerTruck(license#, nTrailers)

Get all information on all trailertrucks.

Page 24: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 24

SQL Queries for Type II Class Hierarchy

select licens#, owner from Vehicle;

Vehicle(license#, owner, color) Car(license#, nPassengers, style)Truck(license#, weight)DumpTruck(license#, loadWeight)TrailerTruck(license#, nTrailers)

Get license# and owner of all vehicles.

Page 25: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 25

Ex: Convert ER Diagram to Relational Schema A – G are entity types. P, Q, and R are relationship

types. Entity types B and D are subtypes of A, and entity type C is a subtype of B. Attributes are shown as a1, a2, ... Primary key attributes are marked with “*”. F is a weak entity-type of E. Attribute f1 of F marked with “**” is a weak-entity key. Construct the relational schema using minimum number of tables.

Page 26: Data Model 21 After this lecture, you should be able to:  Use E-R Model effectively to design a database for a mail order company.  Know more features

Data Model 2 26

Ex: Convert ER Diagram to Relational Schema

A(a1, a2) B(a1, b1, b2) C(a1, c1, c2) D(a1, d1, e1, f1, q1)

E(e1, e2) F(e1,f1, f2) G(g1, g2) R(e1, f1, g1, r1)