ER Model and Shopping Cart Example
The Entity-Relationship approach is a standard modeling technique
for the data aspects of an application. It was introduced by Peter Chen
in 1976 [1]. The model is usually the base for an implementation in a
relational database, but it is not concerned
with implementation details such as data types. It contains several basic concepts:
- An Entity is defined for every object relevant in the application that
occurs in several instances, e.g. customers, products, orders.
- The entities are connected by Relationships, e.g. the fact
that customers place orders is shown in the diagram below with a diamond.
This is the style used by Chen in the original publication. Many other
styles are in use today.
- The Cardinality or number of occurances in relationships is indicated with
1 or n. The 1:n relationship for customer-order results from the fact
that
- one customer can place several orders, but
- each order refers to exactly one customer
- Both entities and relationships can have Attributes, e.g. Name, Price,
Stock, and ID for products. Attributes can be added to the diagram; however,
they are usually listed separately, as real-world ER diagrams
tend to be complex enough without attributes.
It is the task of the analyst to employ careful questioning of key users
in order to reveal entities and relationships. Assume that
in a simple shopping cart application the following entities and their relationships have been identified:
- CUST: the customer; may place one or more orders. Attributes are name, address, ID
- ORDR: an order is placed by exactly one customer and contains one or more items.
Attributes are ID, date, customer
- ITEM: each item refers to exactly one product and is part of exactly one order.
Attributes as order, product, quantity
- PROD: the product; may be part of one or more order items. Attributes are ID, name, price,
stock
The corresponding diagram is shown below. This is not the only solution, as
the ITEM entity could also be understood as an n:m relationship between order and product.
However, to facilitate the transition to the physical implementation
such relationships are usually resolved into 1:n relationships by introducing
additional entities, such as ITEM in this example.
References
[1] Peter Chen (1976). "The Entity-Relationship Model - Toward a Unified View of Data".
ACM Transactions on Database Systems 1 (1): 9-36.