MSIT IT525 Class 2

34
JON WALTER MCKEEBY, DSC DIANA DEE MSIT IT525 CLASS 2

description

MSIT IT525 Class 2. Jon Walter McKeeby, DSc Diana Dee. Book. Coronel, Carlos; Morris, Steven and Peter Rob. 2013. Database Systems: Design, Implementation, and Management.  Tenth Edition. Cengage Learning. Boston, MA. ISBN-10: 1-133-52679-7. ISBN-13: 978-1-133-52679-7. ER Modeling. - PowerPoint PPT Presentation

Transcript of MSIT IT525 Class 2

Page 1: MSIT IT525 Class 2

J O N WA LT E R M C K E E BY , D S CD I A N A D E E

MSIT IT525 CLASS 2

Page 2: MSIT IT525 Class 2

BOOK

• Coronel, Carlos; Morris, Steven and Peter Rob. 2013. Database Systems: Design, Implementation, and Management.  Tenth Edition. Cengage Learning. Boston, MA. • ISBN-10: 1-133-52679-7. ISBN-13: 978-1-133-52679-7.

Page 3: MSIT IT525 Class 2

ER MODELING

• Entities• Attributes• Relationships• Review of Checklist/Style Guide• Top-Down Database Approach• Bottom-Up Database Approach

Page 4: MSIT IT525 Class 2

ENTITIES/ATTRIBUTES/PRIMARY KEY

• What is an entity?• Person, place, thing, concept or event in which data is tracked.

• What is an entity instance?• A row within a table.

• What is an attribute? • A property of an entity type that is of interest.

• What is a composite attribute? • Two or more attributes that have meaning as a whole.• Example: Name = FirstName + MiddleName + LastName; Address = Street + City + State +

PostalCode

• What is a simple (atomic) attribute? • The smallest possible characteristic of an entity.

• What is an identifier? • An attribute (or combination of attributes) whose value distinguishes instances or uniquely

defines an instance of an entity type or a row in a table (e.g. CustomerNo)• An identifier is called a “candidate key”, one of which is chosen as the “primary key”

• What is a composite identifier? • An identifier that includes two or more attributes to uniquely define an a row

e.g. a two-column primary key in an associative entity

Page 5: MSIT IT525 Class 2

RELATIONSHIPS: PARTICIPATION & CARDINALITY

• What is connectivity? The classification of the relationship between entities such as 1:1, 1:M, M:N.

• What is a relationship? An association between entities such as 1:1, 1:M, M:N.• What is participation? Whether or not the entity is required or not within the relationship. Also

referred to as minimum cardinality.• Mandatory shown by perpendicular line which means 1• Optional shown by oval which means 0

• What is cardinality? A constraint on the number of entities participating in a relationship.• Types: 1-M; M-N; 1-1

• What is an identifying relationship? The primary key from the one table is part of the primary key in the second table. This is shown by a solid relationship line.

• What is a weak entity? Part of the PK is dependent on the PK of another table. This is shown by a solid relationship line as well as the PK containing FK to another table.

Page 6: MSIT IT525 Class 2

BUSINESS RULES: HOW TO DETERMINE RELATIONSHIP CARDINALITY

• List the Entity Type Pair.• To determine the cardinality of a relationship, you make two

statements about the relationship, each starting with a singular entity, and examine the (one-word) multiplicity of each.

• The two statements look at the relationship from each “side”. < A | an| ONE > <entity type 1> <some verb> < one | many > <entity type

2> < A | an| ONE > <entity type 2> <some verb> < one | many > <entity type

1>• List the relationship. For M:N list the associative entity.

Order – CustomerAn (one) order is placed by only one customer. (one)A (one) customer may place many orders. (many)Customer 1:M Order

Order – ProductAn (one) order contains many products. (many)A (one) product may be listed on many orders. (many)Order M:N Product AE: OrderLine 6

Page 7: MSIT IT525 Class 2

DATA INTEGRITY

• Primary Key: The selected candidate key use to uniquely identify a row. • Entity Integrity: Primary Key • Each table has column(s) with unique values• Ensures entities are traceable

• Foreign Key: An attribute or group of attributes in one table whose values must match the primary key in another table.• Referential Integrity: Foreign Key• Values of a column in one table match values from a source table• Ensures valid references among tables

Page 8: MSIT IT525 Class 2

RELATIONSHIPS OF TABLES

Page 9: MSIT IT525 Class 2

RELATIONSHIPS

• What are the relationship types of an ERD?• Recursive/Self/Unary Relationship• A relationship found within a single entity type.

• One to One Relationship• One entity instance is associated with only one instance of the related entity.

• One to Many Relationship.• One entity instance is associated with many instances of the related entity.

• Many to Many Relationship• Associations among two or more entities in which one occurrence of an entity is

associated with many occurrences of a related entity and one occurrence of the related entity is associated with many occurrences of the first entity.

• Ternary/M-Way Relationship• Association or relationship between three entities.

Page 10: MSIT IT525 Class 2

RECURSIVE/SELF/UNARY

• Self identifying relationships are associations among members of the same set.• One entity instance is associated with one or many

instances within the same entity. • Example:• An employee has a supervisor who is also an employee.

Page 11: MSIT IT525 Class 2

1:1 RELATIONSHIP

• One entity instance is associated with only one instance of the related entity. • Not a common relationship type.• Identifying Relationship – The primary key from the one table is part of the

primary key in the second table. This is shown by a solid relationship line.• Example:• DB designer splits most frequently used patient attributes from patient

address attributes as a way to group the attributes.

Page 12: MSIT IT525 Class 2

1:M RELATIONSHIP

• Most common form of relationship between entity types.• An entity instance in one entity links to multiple rows in a

different entity.• Example:• A customer has multiple orders, an order line has multiple order lines and a product can be

contained on multiple order lines. OrderNumber within Order is unique and therefore it is not a weak entity. However, if the design was to have the OrderNo and CustomerNo as the key in Order, then Order would be a Weak Entity. OrderLine is an Associative Entity in that it borrows the OrderNumber and SKU as part of its Primary Key.

Page 13: MSIT IT525 Class 2

M:N RELATIONSHIP

• A row in one entity type “A” has a collection of rows in entity type “B” while one row entity type “B” has a collection of rows in entity type “A”.

• SQL cannot support M to N efficiently. Therefore, M-Ns are converted to 1 to M and M to 1.• Associative Entity – An entity designed to transform an M:N relationship into two 1:M

relationships. The composite entity’s primary key comprises at least the primary keys of the entities that it connects. Some systems show as having diagonal line within the entity rectangle.

• Weak Entity – An entity that displays existence dependence and inherits the primary key from its parent entity.

• Identifying Relationship – The primary key from the one table is part of the primary key in the second table. This is shown by a solid relationship line.

• Example: A student enrolls in multiple majors while a major has multiple students.

Page 14: MSIT IT525 Class 2

M:N RELATIONSHIP WITH ATTRIBUTES

• Relationships can have attributes.• Attribute depends on both entity types, not just one entity type.• Example:• Order of authors is important.• AuthOrder is part of the Writes relationship (combination of Author and Book).• AuthOrder is not part of the Author or Book entity types.

Page 15: MSIT IT525 Class 2

TERNARY/M-WAY RELATIONSHIPS

• Relationships that involve more than 2 entity types. Tracks interaction of multiple entity types.

• Associative Entity – An entity designed to transform an M:N relationship into two 1:M relationships. The composite entity’s primary key comprises at least the primary keys of the entities that it connects.

• Weak Entity – An entity that displays existence dependence and inherits the primary key from its parent entity.

• Identifying Relationship – The primary key from the one table is part of the primary key in the second table. This is shown by a solid relationship line.

• Example: 3 way relationship tracks who supplies a part on a specified project

• The entity uses tracks part, supplier per project and all are required for an entry to be placed in Uses.

Page 16: MSIT IT525 Class 2

Database Systems, 10th Edition

ER NOTATION CORONEL, MORRIS & ROB (2013)

Page 17: MSIT IT525 Class 2

DATA MODELING CHECKLIST CORONEL, MORRIS & ROB (2013)

Page 18: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGNPROCEDURE/METHODOLOGY

To design a database that will support a “form,” “user view,” or “report”.

1. Find the entity types.

2. List directly-related entity type pairs.

3. Write the business rules to determine cardinality.

4. Draw the ERD.

5. Designate primary keys (PKs) and foreign keys (FKs).

6. Identify other needed attributes.

7. Review ERD participation and constraints and update the ERD.

18

Page 19: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGN1. FIND THE ENTITY TYPES

19

Page 20: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGN1. FIND THE ENTITY TYPES

From the previous slide, for our Sales process, we have entity types:

Customer

Order

Product

20

Page 21: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGN2. LIST DIRECTLY-RELATED ENTITY TYPE PAIRS

[This may not be easy or obvious. It takes practice, and a careful reading of the business description.]

Customer – Order

Order – Product

Note: Customer is not directly related to Product.

21

Page 22: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGN3. WRITE THE BUSINESS RULES; DETERMINE CARDINALITY

Customer – Order

An (one) order is placed by only one customer. (one)

A (one) customer may place many orders. (many)

Customer 1:M Order

Order – Product

An (one) order may contain many products. (many)

A (one) product may be in many orders. (many)

Order M:N Product (AE: OrderLine)

22

Page 23: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGN4. DRAW THE ERD

5. DESIGNATE PKS AND FKS

23

Note: An FK is not promoted to be part of a PK, except in an associative entity (AE).

Page 24: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGN6. IDENTIFY OTHER NEEDED ATTRIBUTES

24

Page 25: MSIT IT525 Class 2

TOP-DOWN DATABASE DESIGN7. REVIEW PARTICIPATION & CONSTRAINTS

• Participation is whether or not the entity is required or not within the relationship. Also referred to as minimum cardinality.• Mandatory shown by perpendicular line which means 1; Optional shown by oval which means 0

• Constraints (Required fields will be in bold; required FK will change participation cardinality to be one and only one).

• Customer: Require LastName, FirstName, StreetAddressLine1, City, State, ZipCode, HomePhone• Phone must be of valid format, State must be valid abbreviation• Order: Require CusotmerId, OrderDate (Changes Order to Customer to require Customer)• Order requires at least one OrderLine• Subtotal = Quantity * Unit Price from OrderLine• Tax = StateTaxRate * SubTotal; when applies• Shipping Selected from Table• Total = Subtotal + Tax + Shipping• OrderLine: Require Quantity, UnitPrice (Changes OrderLine to Product to require Product)• Product: Require Description, UnitPrice• UnitPrice > 0

Page 26: MSIT IT525 Class 2

BOTTOM-UP DATABASE DESIGN PROCEDURE / METHODOLOGY

To design a database that will support a “form,” “user view,” or “report.”1. Find all the attributes on the form.2. Establish the dependencies (determinants).3. Group attributes that have a common

determinant into an entity type; name it.4. Find directly-related entity type pairs.5. Determine cardinalities for each pair.6. Draw the ERD.7. Review ERD participation and constraints and

update the ERD.26

Page 27: MSIT IT525 Class 2

BOTTOM-UP DATABASE EXAMPLE

Here is part of a customer order form:

27

Page 28: MSIT IT525 Class 2

BOTTOM-UP DATABASE EXAMPLE1. FIND ALL THE ATTRIBUTES

Attributes:CustomerID SubtotalCustomerName TaxAddress ShippingOrderNumber TotalOrderDateQtySKUDescriptionUnitPrice

28

Page 29: MSIT IT525 Class 2

BOTTOM-UP DATABASE EXAMPLE2. ESTABLISH THE DEPENDENCIES

• What does Functional Dependency mean?

The value of one attribute is determined by the value of another attribute.

Example:CustomerName is functionally dependent on CustomerID

CustomerID determines (is a determinant of) CustomerName

29

Page 30: MSIT IT525 Class 2

BOTTOM-UP DATABASE EXAMPLE2. ESTABLISH THE DEPENDENCIES

30

Attribute Depends On (Determinant)

CustomerID [nothing // is itself a determinant]CustomerName CustomerIDAddress CustomerIDOrderNumber [nothing]OrderDate OrderNumberSKU [nothing]Description SKUSubtotal OrderNumberTax OrderNumberShipping OrderNumberTotal OrderNumberQty OrderNumber, SKUUnitPrice OrderNumber, SKU at for ordered productUnitPrice SKU at product level

Page 31: MSIT IT525 Class 2

BOTTOM-UP DATABASE EXAMPLE3. GROUP ATTRIBUTES INTO ENTITY TYPES

•Attributes with the same determinant(s) are grouped together with their determinant(s), and entity types are named:

Customer (CustomerID, CustomerName, Address)

Order (OrderNumber, OrderDate, Subtotal, Tax, Shipping, Total)

Product (SKU, Description)

OrderLine (SKU, OrderNumber, Qty, UnitPrice)

31

Page 32: MSIT IT525 Class 2

BOTTOM-UP DATABASE EXAMPLE4-5. DIRECTLY-RELATED PAIRS AND

CARDINALITIES

Directly-related pairs and cardinalities are the same as was determined in the top-down example:

Customer 1:M Order

Order M:N Product

A many-to-many relationship has a linking or bridging table, formally called an “associative entity.”

Order 1:M OrderLine M:1 Product

32

Page 33: MSIT IT525 Class 2

BOTTOM-UP DATABASE EXAMPLE6. DRAW THE ERD

33

Page 34: MSIT IT525 Class 2

BOTTOM-UP DATABASE DESIGN7. REVIEW PARTICIPATION & CONSTRAINTS

• Participation is whether or not the entity is required or not within the relationship. Also referred to as minimum cardinality.• Mandatory shown by perpendicular line which means 1; Optional shown by oval which means 0

• Constraints (Required fields will be in bold; required FK will change participation cardinality to be one and only one).

• Customer: Require LastName, FirstName, StreetAddressLine1, City, State, ZipCode, HomePhone• Phone must be of valid format, State must be valid abbreviation• Order: Require CusotmerId, OrderDate (Changes Order to Customer to require Customer)• Order requires at least one OrderLine• Subtotal = Quantity * Unit Price from OrderLine• Tax = StateTaxRate * SubTotal; when applies• Shipping Selected from Table• Total = Subtotal + Tax + Shipping• OrderLine: Require Quantity, UnitPrice (Changes OrderLine to Product to require Product)• Product: Require Description, UnitPrice• UnitPrice > 0