Documente Academic
Documente Profesional
Documente Cultură
In this chapter:
• Approaches to database design
• Definitions of entity, attribute, and relationship
• Representing one-to-many relationships
• List all of the data elements that you need, and build them into
a single table. This is what many naïve designers do, and the
result is guaranteed to be a disaster for anyone who has to use
it or maintain it. We have seen many “horror-stories” of data-
bases in real organizations that were (non-) designed this way;
perhaps you have, too. Actually, listing all of the necessary data
isn’t a bad way to start, but you will need to develop the skills
to turn the list into a well-designed database. From a software
engineering perspective, you could call this a “bottom-up”
design strategy. You’ll learn how to do it later in this book.
24 Practical Relational
Database Design
2.1 Entities
An entity is any “thing” that is part of the enterprise and has to be
represented in a database. It could be a physical object: a person, a
piece of furniture, or an item for sale in a store. It could be a concep-
tual object: a movie, for example, that has a title, a plot, dialog and
action but no physical form until it is stored on film or video tape. It
could be an event that happens: a customer places an order, a student
enrolls in a class, or a bus makes a stop to pick up passengers. It could
simply be a fact: a medical patient’s blood serum level is measured at a
specific value.
For our first try at ER modeling, let’s consider
the enterprise of a library. You’re familiar with this
enterprise; you’ve used libraries and you know their
purpose and how they operate. What are the Figure 2-1:
“things” in this enterprise that might need to be rep- Library entities
resented in a database? Well, books would probably
be the first to come to mind. But there are also customers (who check
out books), librarians (who catalog and maintain the books), shelves
(to store the books), reading tables, and probably many more. Of
these, which are relevant to a database? Since our purpose in this
example is to keep track of which books are currently loaned out to
which customers, those two (books and customers) should be the
entities we start with. We’ll diagram them as simple labeled rectangles
or boxes. As it happens, both of these entities are physical objects, but
we will soon show examples of entities that you can’t touch or see.
We will use the word “entity” (and the names of entities) in differ-
ent ways, which normally should be clear from the context. Where it
Chapter 2 25
The ER model
may not be clear, we will specifically identify which one of the follow-
ing we are talking about.
2.2 Attributes
In Chapter 1, we claimed that building a model was a process of
selecting relevant characteristics of whatever we were modeling. The
Civil War soldier was represented by his name, rank, height, eye color,
and so on. The pipe, painted by Magritte, was represented by its shape
and color. The cities, roads, and other features on the map were repre-
sented by lines, symbols, colors, and text. The characteristics of each
entity that you select to include in your ER model are called the
attributes of that entity. When you are first thinking of attributes, try
to decide what (relevant) characteristics are needed to describe the
entity. Or to put it another way: what does the client need to know
about this entity?
Unlike entities, attributes can actually be represented in a com-
puter. We can say that an attribute defines storable values that can be
stored on disk, transmitted over a network, and retrieved to an output
device in the form of a text string, a number, a digitized picture, an
audio or video file, and so on.
We can also think of an attribute more formally as a function that
assigns a single storable value (from a well-defined set of storable val-
ues) to each individual in a set of real-world entities. So name (you) =
“Your Name,” birthdate (you) = “1 July 1975” (or whatever it is), and
so on. The set of storable values that each attribute can assume is
called the domain of that attribute. You will learn more about
attribute domains in Chapter 9.
Let’s think about the entities in our library model. What charac-
teristics—attributes—describe each of them? What does the client
need to know about them?
• Books have titles and authors, but in a library they also have
call numbers and possibly copy numbers (if the library has
Chapter 2 27
The ER model
The attributes that are starred are those that serve to uniquely iden-
tify individuals of the entity type.
Attributes are defined for each entity type according to the rules
of the type. If the attribute value A(j) is defined for an individual j of
type E whenever j belongs to the active set for E, then the attribute A
is said to be defined for the type E. The attribute set SE for an entity
type E is the set of all attributes defined for E. If an attribute is
defined for every individual of a given type then it is defined for that
type. However, the converse is not true. Some attributes that are
defined for the type may not be defined for individuals that are not in
the active set. For example, books that are not in the library do not
have call numbers. However, every book in the library has a call num-
ber. Thus, the attribute call number is defined for the entity type BOOK
in the library, even though call number is not defined for every book
individual in the universe.
2.3 Relationships
Once we have our first entities, and know at least a few of their
attributes, we need to decide how they are connected—what they
have to do with each other. (If an entity has absolutely nothing to do
with any other entity in the model, it shouldn’t be there.) The connec-
tion between two entities is called, not surprisingly, a relationship.
In general, what
is the connection
between customers
and books; what do
they have to do
with each other?
Clearly, customers Figure 2-5: Relationship between customer and book
borrow books—or, entities
said the other way,
books are borrowed by customers. A relationship always can be
described in either direction, and we’ll show it by simply drawing a
line between the two related entities. (We’ve labeled the line in the
illustration. Some development tools let you do this; others don’t—but
you should try to do so whenever possible.)
Let’s be a bit more precise in our definition of this relationship.
Think about only one customer. How many books can that customer
borrow at a given time? Normally, the answer is many—although a
given customer might have only one book (or none at all) checked out
right now. We are concerned here with the largest number, though, so
we will describe the relationship as “each customer borrows many
books.” Now think about the relationship the other way, but start
with only one book. If we are only concerned with the current status
of the book (whether or not it is checked out, and if so, to whom),
then each book is borrowed by (at most) one customer at a time.
The relationship
between the entity
types, shown in Fig-
ure 2-5 above, repre-
sents the mapping of
customers (members
of the active CUSTOM-
ERS entity set) to
books (members of
the active BOOKS en-
tity set) that they
have currently bor- Figure 2-6: Mapping Customers to Books
rowed. Figure 2-6 il-
lustrates the mapping; each line connecting a customer to a book
represents a single link in this mapping.
30 Practical Relational
Database Design
(name, address, etc.) because they are part of the customer entity.
Remember how the soldier in our Civil War example could have
accomplished the same thing by using an officer’s initials to save writ-
ing the name over and over.
In other words, the customer ID is the only foreign attribute that
has to appear in the BOOK entity; all of the other customer attributes
will be inherited by the book along with it. This special foreign
attribute, used to represent the relationship between a customer and a
book, is called a foreign key (which we will also define in Chapter 4).
A primary key value and a matching foreign key value form the link
between two individuals of their respective entity types. In our ER
diagrams, we show foreign keys in italic, so you can immediately see
their function. (Some designers add “fk” to foreign key attribute
names.)
Notice that each customer ID will appear as a member of the active
CUSTOMERS entity set only once (remember that we designed it to be
guaranteed unique). Importantly, the local attributes associated with
that customer ID (name, address, and phone) will also appear only once
in the database, as our design guideline above suggested. But because
the relationship of customers to books is one-to-many, the same cus-
tomer ID might appear as a foreign key attribute value of many books.
When the customer returns a book, the customer ID attribute
value will be removed from that book. In other words, a book that is
not checked out will not have a customer ID associated with it. When
an attribute value like this is missing we call it null. The special con-
stant value null, compatible with the domain of any attribute in a
relational database, means that the attribute is undefined for this
entity individual.
So far, we’ve seen only relationships in which at least one side has
a cardinality of one. In fact, we’ve said that relationships can only be
one-to-many, many-to-one, or one-to-one. What happens if both sides
of a relationship appear to have a value of many? We’ll look at this sit-
uation in the next chapter.
2.6 Glossary
Entity: any “thing” that is part of the enterprise and has to be repre-
sented in a database. (p. 24)
Active set (of an entity type): all those individuals of the given type
who are currently participating in the enterprise. (p. 25)
Domain: the set of storable values that each attribute can assume. (p.
26)
36 Practical Relational
Database Design
2.7 Exercises
2-1. Any university database will have an entity type called STUDENT.
Develop a model of this entity type, including any attributes which
you think are appropriate. To demonstrate the concept of storable val-
ues, try to include some attributes that cannot be represented in text
(character) form. Describe in your own words what (or who) a set of
students might include, and give an example of an individual member
of the student entity set.
between them. Don’t forget to show local and foreign attributes, and
the cardinality of the relationship.
2-5. Build the tables of the library or the patients database, as we have
shown them in this chapter. If your software permits setting up rela-
tionships at design time, link the tables on their related attributes.