database design chapter 9 part-1: concepts & foreign keys 1

14
Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

Upload: darren-blair

Post on 18-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

Different Focus Requirements  Use Cases – the user must be able to: Enter orders Track order status Enter/assign/resolve complaints  Data Model – the system must be able to capture: Order # Order Date Customer Info Billing address Shipping address Products, & quantity Price, discounts Data Administrator Systems Analyst Primary responsibility Assist to ensure consistency w/existing systems

TRANSCRIPT

Page 1: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

1

Database Design

Chapter 9Part-1: Concepts & Foreign Keys

Page 2: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

2

Part OneData Management DeptTypes of DBsFrom Conceptual Data Model to a DatabaseRelational DB ModelDesigning a Relational DB−Multi-valued Attributes−Representing Relationships−Referential Integrity

Outline

Page 3: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

Different Focus

Requirements Use Cases – the user must be

able to: Enter orders Track order status Enter/assign/resolve

complaints Data Model – the system must

be able to capture: Order # Order Date Customer Info Billing address Shipping address Products, & quantity Price, discounts

Data Administrator

Systems Analyst

Primaryresponsibility

Assist to ensureconsistency

w/existing systems

Page 4: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

Data Management Dept.

Database

Data Administrator Database Administrator (DBA)

Data Standards - Naming, etc Data use - Ownership, accessibility Data quality - Integrity rules

Managing the DBMS environment Performance monitoring & tuning Security Backup & Recovery

Page 5: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

5

Types of DatabasesHierarchical NetworkRelationalObject-oriented

Relational Database Management System (RDBMS)organizes data into tables (i.e. relations)−Tables are two dimensional data structures• Record – row or tuple • Field – column or attribute

Types of Databases

Page 6: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

6

From Conceptual Data Model to a Database

Conceptual Data Model Database

ERDDomain

Class Diagram RDBMSEntity Class Table

Attribute Attribute Field

Instance Object Record

Primary Key Identifier Primary Key

Relationship Association Foreign Key

Page 7: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

7

(def) a model where data is represented as a set of related tables or relations.

Properties/Rules of Relations:−Entries in cells are simple. (Rule of Atomic Values)−Entries in columns are from the same set of values. (Domain)−Each row is unique. (Primary Key)−Order of columns may be interchanged −Order of rows may be interchanged

−Other Rules come from “Normalization”

Relational Database Model

Page 8: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

8

Topic DescriptionTables Create TBs – 1 for each entity/domain class

Primary Keys Select/define a PK for each TB

Associative Create new tables to represent many-to-many relationships

Multi-Valued Attributes

Resolve to new tables

Foreign Keys Add FKs to establish relationships

Super & Sub Classes Decide on how to represent any Generalization Hierarchies

Referential Integrity Define Referential Integrity constraints

Normalization Evaluate schema quality and make necessary improvements

Data Types Determine appropriate data types for validation, integrity

Designing a Relational DB

Page 9: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

9

No Multi-Valued Attributes Due to the Rule of Atomic Values

Multi-Valued Attributes

Employee Table

SSN LastName

FirstName

Title Dependents

111 Smith Robert Accountant Bobbie, Sue

222 Jones Leo Programmer

333 Lopez Trent Sales Rep Trent Jr, Trevor, Sue

Dependents Table

SSN Dependents

111 Bobbie

111 Sue

333 Trent Jr

333 Trevor

333 Sue

PK?

Employee

SSN {key}lastNamefirstNametitledependents

Employee Table

SSN LastName

FirstName

Title

111 Smith Robert Accountant

222 Jones Leo Programmer

333 Lopez Trent Sales Rep

Page 10: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

10

One to Many Relationshipcreate foreign key in the relation that is on the “many” side

of relationship only−NOTE: Going the other way would generate a multi-value attribute

Foreign Keys: Establishing Relationships

employs

PK FK Add DeptCd here

Copy it over

Department

deptCd {key}deptNamelocation

Employee

SSN {key}lastNamefirstNametitle

1 *

Page 11: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

11

One to One Relationshipcreate foreign key in either relation−NOTE: IF you have one & only one on both sides, these may be merged

into a single table

Foreign Keys: Establishing Relationships

is assigned

PK FK

Copy it over

Add SSN here

ParkingSpace

psNo {key}levelAccessible?

Employee

SSN {key}lastNamefirstNametitle

PKFK

Copy it over

Add psNo here

1 1

Page 12: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

12

PKFK

Copy it over

Many to Many Relationshipshould’ve been eliminated on ERD!!!create new relation with PKs of related

entities as:−1 concatenated PK, and 2 FKs

Foreign Keys: Establishing Relationships

PK FK

Copy it over

(PK)

Employee

SSN {key}lastNamefirstNametitle

Project

projCd {key}projNamedescstDateendDate

EmpProject

____________________rolestDate

0..*1..1 1..* 1..1

EmpProject

Employee Project

0..*1..*

Page 13: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

13

Super & Sub Classes2 Options to Implement

Separate Tables−Student (SID, name, birthDate)−UndergradStudent (SID, mentor)−GradStudent (SID, thesisTopic)

One Table−Student (SID, name, birthDate, mentor, thesisTopic, studentType)

Student

UndergradStudent

GradStudent

SID {key}namebirthDate

mentor thesisTopic

Page 14: Database Design Chapter 9 Part-1: Concepts & Foreign Keys 1

14

Referential IntegrityReferential Integrity

(def) an integrity constraint on the Foreign Key:1. Prevents adding a FK value, if an associated PK value does not exist.2. Prevents delete of PK value, if an associated FK value exists.3. Prevents change of PK value, if an associated FK value exists.

Can override:−Cascade Delete Overrides constraint #2

−Cascade Update Overrides constraint #3