Appendix A Database Design Data Modeling and the Entity-Relationship Model 1
Three Stages of Database Development The three stages of database development are: 1. Requirements Analysis Stage 2. Component Design Stage 3. Implementation Stage 2 2
The Requirements Analysis Stage Sources of requirements User Interviews Forms Reports Queries Use Cases Business Rules 3 3
Requirements Become the E-R Data Model After the requirements have been gathered, they are transformed into an Entity Relationship (E-R) Data Model. The most important elements of E-R Models are: Entities Attributes Identifiers Relationships 4 4
Entities Entities represent the basic objects of information you are tracking Begin by thinking of them as separate and distinct sets. Examples: Hint: Entities are usually nouns Business Customers Employees Inventory Items Orders Veterinary Clients Pets Appointments Services 5 5
Attributes Entities have attributes that describe the entity s characteristics: FirstName LastName SSN HiredDate Attributes have a data type and properties. For each set of entities, determine what attributes will be stored in the database 6 6
Relationships Entities can be associated with one another in relationships. Relationship degree defines the number of entity classes participating in the relationship: Degree 2 is a binary relationship and is the most common. Degree 3 is a ternary relationship. Avoid this. The type of relationship is called the cardinality: 1-1, 1-M, M-N 7 7
One-to-One Binary Relationship 1:1 (one-to-one) A single entity instance in one entity class is related to a single entity instance in another entity class. -An employee may have no more than one locker; and -A locker may only be accessible by one employee (a) One-to-One Relationship 8 8
One-to-Many Binary Relationship 1:N (one-to-many) A single entity instance in one entity class is related to many entity instances in another entity class. - A quotation is associated with only one item; and - An item may have several quotations (b) One-to-Many Relationship 9 9
Many-to-Many Binary Relationship N:M (many-to-many) Many entity instances in one entity class are related to many entity instances in another entity class: - a supplier may supply several items; and - a particular item may be supplied by several suppliers. (c) Many-to-Many Relationship 10
Entity-Relationship Diagrams The diagrams in previous slides are called Entity-Relationship diagrams. Entity classes are shown by rectangles. Relationships are shown by diamonds. The name of the entity is shown inside the rectangle. The name of the relationship is shown near the diamond. 11
Developing an E-R Diagram Heather Sweeney Designs example Heather Sweeney is an interior designer who specializes in home kitchen design. She offers a variety of free seminars at home shows, kitchen and appliance stores, and other public locations. She earns revenue by selling books and videos that instruct people on kitchen design. She also offers private consultations on kitchen designs 12
Heather Sweeney Designs: The Seminar Customer List Example Seminar Customer List 13
Step 1: Identify the Entities Customers Seminars Products 14
Step 2: Identify the Attributes Customers LastName, FirstName, Phone, Address, City, State, Zip Seminars SeminarTitle, SeminarDate, SeminarTime, Location Products Description, UnitPrice, QuantityOnHand 15
Step 3: Identify the Relationships Think of verbs: Customers attend seminars Seminars are attended by customers Customers order products Products are ordered by customers 16
Step 4: Determine the Relationship Cardinalities Cardinalities are: One-to-One (1:1) One-to Many (1:M) and Many-to-Many (M:N) 17
Step 5: Create an E-R Diagram E-R stands for Entity-Relationship Use the following: Entities Attributes Relationships 18
Step 5: E-R Diagram Initial version 19
Step 6: Identify the Primary Keys Consult with the client about the data Add an attribute if necessary Indicate on the diagram with an underline Customers -lastname, firstname, phone, address, city, state, zip -No candidate key, so add custid to be the primary key Seminars -Attributes: seminartitle, seminardate, seminartime, location -No candidate key, so add SID to be the primary key Products -Attributes: productname, description, unitprice, qtyinstock -Assume product names are unique, so use productname as PK 20
Step 6: Identify the Primary Keys Final Version 21
Create the Database Tables Make one table for each entity set Add a table for each M:N relationship that contains the primary keys from the participating tables as a composite primary key If there is a 1:M (or M:1) relationship. Add the primary key from the 1 side to the M side table as a foreign key. 22
Result from Example Five tables: Customers (custid, firsname, lastname, street, city, state, zip, email) Seminars (SID, seminartitle, seminardate, seminartime, location) Products (productname, description, unitprice, qtyinstock) Attends (custid, SID) Purchases (custid, productname) 23
Tables Formal term: Relation Other formal terms 24
Keys Primary keys uniquely identify each row in a table Primary keys must also be minimal Candidate keys are fields that could serve as the primary key Alternate keys are the candidate keys not chosen 25
Composite Keys (multi-field) 26
Relationships: One-to-Many 27
Relationships: Many-to-Many 28
Relationships: One-to-One 29