The Line-Item Pattern

The Line-Item Pattern

Figure 5-32 shows a typical sales order, or invoice. Such forms usually have data about the order itself, such as the order number and order date, data about the customer, data about the salesperson, and then data about the items on the order. A data model for a typical SALES_ORDER is shown in Figure 5-33.

In Figure 5-33, CUSTOMER, SALESPERSON, and SALES_ORDER are all strong entities, and they have the nonidentifying relationships you would expect. The relationship from CUSTOMER to SALES_ORDER is 1:N, and the relationship from SALESPERSON to SALES_ORDER also is 1:N. According to this model, a SALES_ORDER must have a CUSTOMER and may or may not have a SALESPERSON. All of this is readily understood.

The interesting relationships concern the line items on the order. Examine the data grid in the form in Figure 5-32. Some of the data values belong to the order itself, but other data values belong to items in general. In particular, Quantity and ExtendedPrice belong to the SALES_ORDER, but ItemNumber, Description, and UnitPrice belong to ITEM. The lines on an order do not have their own identifier. No one ever says, “Give me the data for line 12.” Instead, they say, “Give me the data for line 12 of order 12345.” Hence, the identifier of a line is a composite of the identifier of a particular line and the identifier of a particular order. Thus, entries for line items are always ID-dependent on the order in which they appear. In Figure 5-33, ORDER_LINE_ITEM is ID-dependent on SALES_ORDER. The identifier of the ORDER_LINE_ITEM entity is (SalesOrderNumber, LineNumber).

Now, and this is the part that is sometimes confusing for some students, ORDER_

Part 2 Database Design

Figure 5-32

Data Entry Form for a Sales Order

assigned to it. Further, if an ITEM is deleted, we do not want the line item to be deleted with it. The deletion of an ITEM may make the value of ItemNumber and other data invalid, but it should not cause the line item itself to disappear.

Now consider what happens to a line item when an order is deleted. Unlike with the dele- tion of an item, which only causes data items to become invalid, the deletion of the order removes the existence of the line item. Logically, a line item cannot exist if its order is deleted. Hence, line items are existence-dependent on orders.

Work through each of the relationships in Figure 5-33 and ensure that you understand their type and their maximum and minimum cardinalities. Also understand the implications

Figure 5-33

CUSTOMER

Data Model for the Sales CustomerID SALESPERSON Order in Figure 5-32

LastName

SalespersonID

FirstName Address

SALES_ORDER

Phone SalesOrderNumber

Date Subtotal Tax Total

ITEM

ItemNumber UnitPrice

Description

ORDER_LINE_ITEM SalesOrderNumber

LineNumber Quantity

UnitPrice ExtendedPrice

Chapter 5 Data Modeling with the Entity-Relationship Model

AmountOnHand StorageLocation

INGREDIENT_USE RecipeName

IngredientNumber

Figure 5-34

Amount

Mixed Relationship Pattern

Instructions

for Restaurant Recipe

of this data model. For example, do you see why this sales order data model is unlikely to be used by a company in which salespeople are on commission?