Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational...

32
Relational Database

Transcript of Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational...

Page 1: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Relational Database

Page 2: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

I. Relational Database

1. Introduction

2. Database Models

3. Relational Database

4. Entity-Relationship Models

5. RDB Design Principles

Page 3: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

1. Introduction

Database Is a collection of related data Is persistent

Database Management SystemSoftware system to add, delete, search,

and modify the data in the DBTo provide various ways to view the data in

the DB

Page 4: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

2. Database Models

Flat Database

Hierarchical DB

Network DB

Relational DB

Page 5: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Flat Database

Flat Database (Library DB)RedundancyPublisher name is repeatedPublisher phone number is repeatedAuthor phone is repeated If publisher address is included, … ?

Update AnomaliesTo modify publisher phone, all need to be

modified

Page 6: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Insertion AnomalyA new publisher, with no data about book,

author, and other details which are not available, cannot be included.

Deletion Anomaly If a book is deleted, its publisher data, or

author data, can also be eliminated (e.g., last book in the list)

Page 7: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Solution

Break up Flat-file DB into Separe Tables

Tables (Library DB)

Page 8: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Hierarchical DB Model

Agents

Agency Database

PaymentsEngagementsSchedules

Entertainers Clients

(This and remaining slides on DB Models are adapted from Database Design for Mere Mortals, Michael Hernandez, Addison Wesley, 1999)

Back to Ref Ing

Page 9: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Hierarchical DBM Characteristics

One table acts as a root of an inverted tree; other tables are branchesEach child table may have only one parent tableEach parent table may have many child tablesTo access any data, must start at root table

Page 10: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Hierarchical DBM Advantages & Disadvantages

Advantages: Data retrieve is fast, because table structure is

explicitly linked Referential integrity is built in

Disadvantages: Difficult to store data in child table when no

corresponding record exists in parent tables. E.g., a new entertainer cannot be entered in the DB until a specific Agent is assigned

Difficult to model complex relationships Difficult to add new tables

Page 11: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Referential Integrity

Each record in a child table is linked to an existing record in the parent table. E.g., every Payment record is associated with a particular Client.

If a record is deleted in a parent table,all associated record in child tables are also deleted. E.g., If a particular Client record is deleted, all related records in Payments and Engagements are deleted—no orphans.

Page 12: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Network Database Model

Agents

Agency Database

Musical StylesEngagementsPayments

Clients Entertainers

represents manages

performsschedulesmakes plays

Page 13: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Network DBM Characteristics

Invented to address problems with the Hierarchical DB Model

A child table can have more than one parent table

Can go up or down the structure. E.g., to answer “Who was the agent that booked a particular engagement?”, start with Engagements, Clients, then to Agents.

Page 14: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Relational DatabaseCollection of tables e.g., books, authors, publishers, clients

Each cell in a table is atomic i.e., no formulas, pointers, but single data item

Tables are linked by common values in selected columns i.e., not by pointers

Books Table

Publishers Table

Page 15: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Basic TerminologyData & Information

These are data valuesKashimata 25 October 31 25000

This is informationCustomer’s last Name: KashimataAge: 25Birthday: October 31Savings Account Balance: $25,000

Page 16: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Basic TerminologyNull

Null, zero, and blank Age = Null

age is undefined Age = 0

age is 0 MiddleName = Null

middle name is undefined MiddleName = “”

person has no middle name MiddleName = “ “

no middle name, takes up 1 byte

Page 17: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Basic TerminologyTable, Record, Field

Formal Informal Non-relational

relation table database

tuple row record

attribute column field

Page 18: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Basic TerminologyTable

Table (Books Table)Represents an entity (category) of objects

with its (relevant) attributesConsists of rows (records) and columns

(fields) In the Books table, each record is an

instance of the Books category.Each field represents an attribute

Page 19: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Basic TerminologyAttributes (fields)

To provide specific information about entityTo help identify individual entities--e.g., ISBN for books, SSN for employee, Sales_ID for sales.To describe relationship between entity entities in different entity classesFor example...

Page 20: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Your Turn. Identify Attributes..

Entity Classes for School DatabaseStudentsFacultyStaffBuildingsRoomsCourseClass

Page 21: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Identify Attributes...

Entity Classes For a Bank DatabaseCustomersAccountEmployeeTransaction

Page 22: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Basic TerminologyKeys

SuperkeySet of attributes to identify a record

uniquely in a set--e.g., to distinguish one book from all others. ISBN, ISBN+TITLE, ISBN+TITLE+AUTHOR

Key or Candidate KeyMinumum superkey. E.g., ISBN

Primary KeyA Key that is chosen for a particular table

Page 23: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Primary Key

Employee Class FirstName LastName SSN PhoneNumber DateOfBirth E-mail

Candidate Key SSN FirstName

+LastName + DateOfBirth

E-mail? E-mail + DateOfBirth

Primary Key SSN

Page 24: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Types of Relationships(Library DB)

How are the tables associated with each other, so that information can be extracted from multiple tables?

Publishers

Authors

Books

Page 25: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Types of Relationships

1 to many (most common) A A publisherpublisher may publish may publish manymany books, but each books, but each bookbook may have only may have only oneone publisher publisher

may to many (undesirable) A A bookbook may be written by may be written by manymany authors, and an authors, and an authorauthor may write may write manymany books. books.

1 to 1 (not as common) One One authorauthor has at most has at most oneone secretary, and each secretary, and each secretarysecretary works for only works for only oneone author author

Page 26: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

One-To-Many Relationship

One record in Publishers can be related to many records in Books

But, each record in Books can be associated with only one record in Publishers

Publishers Books

∞1

Publishers Books

1 1

Page 27: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Many-to-Many Relationship

One record in Authors can be related to many records in Books

And, each record in Books can be associated with many records in Authors

Authors Books

∞1

Authors Books∞ 1

Page 28: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

One-to-OneRelationship

One record in Authors can be related to only one record in Secretaries

And, each record in Secretaries can be associated with only record in Authors

Authors Secretaries1

1

Authors

11

Secretaries

Page 29: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

ExampleDatabase TypeLibrary

TablesBooksPublishers

One-to-Many Relationship?Yes

Why?

Page 30: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

ExampleDatabase TypeOrder Tracking

TablesCustomersOrders

One-to-Many Relationship?Yes

Why?

Page 31: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

ExampleDatabase TypeSchool Scheduling

TablesClassesStudents

One-to-Many Relationship?No (Many-to-Many)

Why?

Page 32: Relational Database. I. Relational Database 1. Introduction 2. Database Models 3. Relational Database 4. Entity-Relationship Models 5. RDB Design Principles.

Data Integrity

Table-level integrity E.g., no duplicate records (by defining a primary

key)

Field-level integrity E.g., do all “State” field require 2 characters? Each field contains a single atomic value?

(e.g., Author field should not contain 2 authors

Relationship-level integrity E.g., no many-to-many relationships Prevent “orphans.” e.g., if a publisher record is

deleted, a book record is left without a publisher.