Data Modeling and Relational Database Design ISYS 650.

41
Data Modeling and Relational Database Design ISYS 650
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    0

Transcript of Data Modeling and Relational Database Design ISYS 650.

Page 1: Data Modeling and Relational Database Design ISYS 650.

Data Modeling and Relational Database Design

ISYS 650

Page 2: Data Modeling and Relational Database Design ISYS 650.

Conceptual Database Design Methodology

• Identify entity types.• Identity relationship types between the entity

types.• Identify and associate attributes with entity or

relationship types.• Determine attribute domains.• Determine candidate keys and primary key.• Validate conceptual model:

– Check for redundancy, support required transactions, review the model with user

Page 3: Data Modeling and Relational Database Design ISYS 650.

Entity Type

• A collection of entities that share common properties or characteristics.

• An entity type represents a collection of entities.

• In an ERD, it is given a singular name.

• Diagrammatic representation:– A rectangle labeled with the name of the entity

Page 4: Data Modeling and Relational Database Design ISYS 650.

Relationship Type

• Relationship: Interaction between entity types.– It is an association representing an interaction

among the instances of one or more entity types that is interest to the organization.

• It has a verb phrase name:– Faculty teach Course, Faculty advise Student– Customer open Account, Customer purchase

Product.

Page 5: Data Modeling and Relational Database Design ISYS 650.

Binary Relationship types and instances

a) Relationship type

b) Relationship instances

Page 6: Data Modeling and Relational Database Design ISYS 650.

Binary Relationship

• A relationship involves two entity types.

• Three kinds of Binary Relationship - – 1:1– 1:M– M:M

Page 7: Data Modeling and Relational Database Design ISYS 650.

Interpretation of an M:M Relationship

Peter

Paul

John

Woody

Alan

Mary

Linda

Nancy

Mia

Pia

A boy may date 0, 1, or many girls.

A girl may date 0, 1, or many boys.

Note: “Many boys date many girls” is not a correct interpretation.

Boy Girl

Page 8: Data Modeling and Relational Database Design ISYS 650.

Cardinality Constraint

• A cardinality constraint specifies the number of instances of entity type A that can (or must) be associated with each instance of entity type B.

• Participation constraint– Full participation (Mandatory)– Partial participation (Optional)

Page 9: Data Modeling and Relational Database Design ISYS 650.

Notations

Page 10: Data Modeling and Relational Database Design ISYS 650.

Other Notations

UML Notations:– 0..1, 1..1– 0..*, 1..*– 3..5

• Traditional:

Student AccountHas1 1

Student AccountHas

1..11..1

Page 11: Data Modeling and Relational Database Design ISYS 650.

Traditional ERD Notations

Student Account

Faculty Course

Has1 1

EnrollM MAdvise

M

1

TeachM1

Page 12: Data Modeling and Relational Database Design ISYS 650.

UML ERD Notations

Student Account

Faculty Course

Has1..11..1

Teach

1..*1..1

Enroll

0..* 0..*Advise

0..*

1..1

Page 13: Data Modeling and Relational Database Design ISYS 650.

Other Notations

Student Account

Faculty Course

Has

Teach

Enroll

0..*Advise

Page 14: Data Modeling and Relational Database Design ISYS 650.

Recursive Relationship

• A relationship type where the same entity type participates more than once in different roles.

• Examples:– Employee – Supervise -- Employee– Student -- Tutor– Student– Faculty – Evaluate -- Faculty

Page 15: Data Modeling and Relational Database Design ISYS 650.

Employee

Supervise

Supervisor

Supervisee

Employee

Supervise

M1

Page 16: Data Modeling and Relational Database Design ISYS 650.

Attributes• Properties of an entity or a relationship.• Simple and composite attributes

– Address:Street address, City, State, ZipCode– Street Address: Number, Street, Apt#– Phone#: Area Code, number

• Single-valued and multi-valued attributes– Student’s Major attribute– Faculty’s DegreeEarned attribute– Vehicle’s Color attribute– Others: PhoneNumber, EmailAddress

• Derived attributes• Keys: Key attribute uniquely determines an entity.

– Candidate key, primary key, composite key

Page 17: Data Modeling and Relational Database Design ISYS 650.

UML Notations

StudentSID {PK}Sname Fname LnameAddress Street City State ZipPhone[1..3]SexDateOfBirth/Age

Page 18: Data Modeling and Relational Database Design ISYS 650.

SID {PK}Sname( Fname, Lname)Address( Street, City, State, Zip){Phone}SexDateOfBirth[Age]

Page 19: Data Modeling and Relational Database Design ISYS 650.

Student

SID Sname

Fname Lname

Phone DateOfBirth

Age

Page 20: Data Modeling and Relational Database Design ISYS 650.

Attributes on Relationship Online Shopping Cart

Customer ShoppingCart

Product

Has

Has

1 M

M

M

CID CnameAddr CartID Date

PIDPname

Price

Page 21: Data Modeling and Relational Database Design ISYS 650.

Order Form

Page 22: Data Modeling and Relational Database Design ISYS 650.

Online Shopping Cart

Customer ShoppingCart

Product

Has

Has

1 M

M

M

CID CnameAddr CartID Date

Qty

PIDPname

Price

Page 23: Data Modeling and Relational Database Design ISYS 650.

Attributes on Relationship

• Examples:– Student/Course: Grade– Order/Product: Quantity

Page 24: Data Modeling and Relational Database Design ISYS 650.

N-ary Relationship

• Doctor – Patient – Ailment

• Police – Criminal – Crime

• AirCraft – Bomb – Target

• Note: There is no deterministic relationship (1:1 or 1:M) between any two of these entities.

Page 25: Data Modeling and Relational Database Design ISYS 650.

Examples of relationships of different degrees (cont.)

c) Ternary relationship

Note1: a relationship can have attributes of its own.Note2: This ternary relationship exists only if there is no binary relationship between these three entities.

Page 26: Data Modeling and Relational Database Design ISYS 650.

Entities can be related to one another in more than one way

Examples of multiple relationships

Employees and departments

Example: Auction site: User and Auction Item

Page 27: Data Modeling and Relational Database Design ISYS 650.

Properties of a Relation

• Simple attribute– No composite, no multivalued attribute

• Each relation must have a primary key:– Simple or composite key– May have other keys (candidate keys)– Key cannot be null– Cannot be duplicated

Page 28: Data Modeling and Relational Database Design ISYS 650.

Integrity Constraints

• Domain constraints

• Entity integrity:– Primary key cannot be null, cannot be

duplicated

• Referential integrity

• Other constraints

Page 29: Data Modeling and Relational Database Design ISYS 650.

Relational Database Design • Entity: Create a table that includes all simple

attributes– Composite

• Multi-valued attribute: Create a table for each multi-valued attribute– Key + attribute

• Relationship: – 1:1, 1:M

• Relationship table: for partial participation to avoid null values• Foreign key

– M:M: relationship table– N-ary relationship: relationship table– Recursive relationship

• Attribute of relationship

Page 30: Data Modeling and Relational Database Design ISYS 650.

Online Shopping Cart

Customer ShoppingCart

Product

Has

Has

1 M

M

M

CID Cname EmailAddr CartID Date

Qty

PID PnamePrice

Page 31: Data Modeling and Relational Database Design ISYS 650.

Recursive Relationship

Note: Partial participation

Page 32: Data Modeling and Relational Database Design ISYS 650.

Ternary Relationship

Page 33: Data Modeling and Relational Database Design ISYS 650.

Third Normal Form

• The database designed according to these rules will meet the 3NF requirements.

Page 34: Data Modeling and Relational Database Design ISYS 650.

Normalized Database

• A normalized database is a database where in each relation the non key fields are functionally dependent on the key, the whole key, and nothing but the key.

Page 35: Data Modeling and Relational Database Design ISYS 650.

Function Dependency

• Relationship between attributes

• X -> Y– The value of X uniquely determines the value

of Y.– Y is functionally dependent on X.– A value of X is associated with only one value

of Y.

Page 36: Data Modeling and Relational Database Design ISYS 650.

Example• Employee table:

– SSN Ename Sex DOB– S1 Peter M 1/1/75– S2 Paul M 12/25/80– S3 Mary F 7/4/72

• Function Dependencies:– SSN -> Ename, SSN ->Sex, SSN -> DOB– SSN -> Ename, Sex, DOB

• Any other FD:– Ename -> SSN ?– Ename -> Sex ?– DOB -> SSN ?

Page 37: Data Modeling and Relational Database Design ISYS 650.

• What is the key of Employee table:– SSN

• Observations:– All non-key fields are functionally dependent on SSN.

– There is no other FD.

– The only FD is the key dependency.

– There is no data duplication in the Employee table.

Page 38: Data Modeling and Relational Database Design ISYS 650.

If we mix a multivalue attribute with regular attributes in one table

• Employee Table:– SSN, Ename, Sex, DOB, Phone– Assuming an employee may have more than 1

phone.

• Key: SSN or SSN + Phone

• Duplication ?

• Problem: Some non-key fields depend on part of the key– SSN + Phone -> Ename, Sex, DOB– SSN -> Ename, Sex, DOB

Page 39: Data Modeling and Relational Database Design ISYS 650.

If we mix two entities with M:M relationship in one table

• StudentCourse table:– SID, Sname, GPA, CID, Cname, Units

• Key: SID + CID

• Duplication?

• Problem: Some non-key fields depend on part of the key– SID+CID -> Sname, GPA, Cname, Units– SID -> Sname, GPA– CID-> Cname, Units

Page 40: Data Modeling and Relational Database Design ISYS 650.

If we mix two entities with 1:M relationship in one table

• FacultyStudent table:– Faculty Advise Student: 1:M relationship– FID, Fname, SID, Sname, SAddress

• Key: SID

• Duplication?

• Problem: Not nothing but the key– SID -> FID, Fname– FID -> Fname

Page 41: Data Modeling and Relational Database Design ISYS 650.

Third Normal Form

• Every non-key field is:– Fully functionally dependent on the key, and– Not transitively dependent on the key.

• The database does not have duplication due to mixing: – Multi-value attribute with single-value attributes– Entity types with relationship