CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture...

51
Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database System Concepts” 6 th edition book and are a modified version of the slides which accompany the book (http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/index.html), in addition to the 2009/2012 CMSC 461 slides by Dr. Kalpakis These slides are based on “Database System Concepts” 6 th edition book and are a modified version of the slides which accompany the book (http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/index.html), in addition to the 2009/2012 CMSC 461 slides by Dr. Kalpakis CMSC 461, Database Management Systems Spring 2018 Dr. Jennifer Sleeman https://www.csee.umbc.edu/~jsleem1/courses/461/spr18

Transcript of CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture...

Page 1: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lecture 11 - Chapter 8 Relational Database Design Part 1These slides are based on “Database System Concepts” 6th edition book and are a modified version of the slides which accompany the book (http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/index.html), in addition to the 2009/2012 CMSC 461 slides by Dr. Kalpakis

These slides are based on “Database System Concepts” 6th edition book and are a modified version of the slides which accompany the book (http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/index.html), in addition to the 2009/2012 CMSC 461 slides by Dr. Kalpakis

CMSC 461, Database Management SystemsSpring 2018

Dr. Jennifer Sleeman https://www.csee.umbc.edu/~jsleem1/courses/461/spr18

Page 2: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Logistics

● HW3 released today due 3/12/2018● Phase 2 due Wednesday 3/7/2018● Midterm 3/14/2018

2

Page 3: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lecture Outline

• Relational Database Design • First Normal Form (1NF) • Functional Dependencies • Boyce-Codd (BCNF) • Third Normal Form (intro)

3

Page 4: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lecture Outline

• Relational Database Design • First Normal Form (1NF) • Functional Dependencies • Boyce-Codd (BCNF) • Third Normal Form (intro)

4

Page 5: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Relational Database Design● We want to move from the E-R diagram to a

set of relations ○ Eliminate redundancy ○ Ensure design is complete ○ Ensure information is easily retrievable

● Going to learn about ○ normal form○ functional dependencies

Based on and image from “Database System Concepts” book and slides, 6th edition

5

Page 6: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Combining Schemas

● Recall the instructor and department relations

Based on and image from “Database System Concepts” book and slides, 6th edition

6

Page 7: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Combining Schemas

● Suppose we combine instructor and department into inst_dept

○ No connection to relationship set inst_dept● Why is this a bad idea?

Based on and image from “Database System Concepts” book and slides, 6th edition

7

Page 8: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Combining Schemas

● Repetition of information

Based on and image from “Database System Concepts” book and slides, 6th edition

8

Page 9: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Combining Schemas without Repetition?

● Consider combining relations:

● into one relation

Based on and image from “Database System Concepts” book and slides, 6th edition

9

Page 10: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Smaller schemas

Given inst_dept was the result of our design…..

How would we know to split up (decompose) it into instructor and department?

Based on and image from “Database System Concepts” book and slides, 6th edition

10

Page 11: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Smaller schemas

● What can we say about this data?

Based on and image from “Database System Concepts” book and slides, 6th edition

11

Page 12: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Smaller schemas

● Write a rule:

“if there were a schema (dept_name, building, budget), then dept_name would be able to serve as the primary key”

Based on and image from “Database System Concepts” book and slides, 6th edition

12

Page 13: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Smaller schemas

● Denote as a functional dependency: dept_name → building, budget

● In inst_dept, because dept_name is not a primary key, the building and budget of a department may have to be repeated

○ This indicates the need to decompose inst_dept

Based on and image from “Database System Concepts” book and slides, 6th edition

13

Page 14: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Design Alternatives: Smaller schemas

Not all decompositions are good.

Suppose we decompose:

employee(ID, name, street, city, salary)

into

employee1 (ID, name) employee2 (name, street, city, salary)

Based on and image from “Database System Concepts” book and slides, 6th edition

14

Page 15: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

A Lossy Decomposition

Based on and image from “Database System Concepts” book and slides, 6th edition

15

Page 16: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lossless Join Decomposition

Based on and image from “Database System Concepts” book and slides, 6th edition

16

Page 17: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Another Example● Consider the relation schema:

○ Lending-schema = (branchName, branchCity, assets, customerName, loanNumber, amount)

Based on and image from “Database System Concepts” book and slides, 6th edition

17

Page 18: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Another Example● Redundancy:

○ Data for branchName, branchCity, assets are repeated for each loan that a branch makes

○ Wastes space○ Complicates updating, introducing possibility of

inconsistency of assets value ● Null values

○ Cannot store information about a branch if no loans exist

○ Can use null values, but they are difficult to handle.

Based on and image from “Database System Concepts” book and slides, 6th edition

18

Page 19: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Decomposition

Based on and image from “Database System Concepts” book and slides, 6th edition

19

Decompose the relation schema Lending-schema into:

All attributes of an original schema (R) must appear in the decomposition (R1,R2):

Lossless-join decomposition.For all possible relations r on schema R

Page 20: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lecture Outline

• Relational Database Design • First Normal Form (1NF) • Functional Dependencies • Boyce-Codd (BCNF) • Third Normal Form

20

Page 21: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

First Normal Form (1NF)● In the relational model attributes have no substructure

○ composite attributes - each component becomes its own attribute

○ multivalued attributes - one tuple for each item ● Domain is atomic if its elements are considered to be

indivisible units○ Examples of non-atomic domains:

■ Set of names, composite attributes■ Identification numbers like CS001 where department

is combined with employee number■ Fine line: ‘CS101’ might be a course identifier and

could be interpreted as atomic

Based on and image from “Database System Concepts” book and slides, 6th edition

21

Page 22: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

First Normal Form (1NF)● Non-atomic values complicate storage and encourage

redundant (repeated) storage of data○ Example: Set of accounts stored with each customer,

and set of owners stored with each account

Based on and image from “Database System Concepts” book and slides, 6th edition

22

Page 23: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

First Normal Form (1NF)● A relational schema R is in first normal form if the

domains of all attributes of R are atomic

Based on and image from “Database System Concepts” book and slides, 6th edition

23

Page 24: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

First Normal Form (1NF)● Atomicity is actually a property of how the elements of the

domain are used○ Example: Strings would normally be considered

indivisible○ Suppose that students are given roll numbers which are

strings of the form CS0012 or EE1127○ If the first two characters are extracted to find the

department, the domain of roll numbers is not atomic.○ Doing so is a bad idea: leads to encoding of information

in application program rather than in the database

Based on and image from “Database System Concepts” book and slides, 6th edition

24

Page 25: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Is it atomic?● Address● Course ID (CS-101)● Student Name● SSN● ISBN Number

Based on and image from “Database System Concepts” book and slides, 6th edition

25

Page 26: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Convert it to 1NF

Based on and image from “Database System Concepts” book and slides, 6th edition

26

Page 27: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Pitfalls in Relational Database Design

● Relational database design requires that we find a “good” collection of relation schemas

● A bad design may lead to ○ Repetition of Information○ Inability to represent certain information

● Design Goals: ○ Avoid redundant data○ Ensure that relationships among attributes are

represented○ Facilitate the checking of updates for violation of

database integrity constraints

Based on and image from “Database System Concepts” book and slides, 6th edition

27

Page 28: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Goal: Devise a Theory for the following:

● Decide whether a particular relation R is in “good” form● In the case that a relation R is not in “good” form,

decompose it into a set of relations {R1 , R2 , ..., Rn } such that

○ each relation is in good form○ the decomposition is a lossless-join decomposition

● This theory is based on:○ functional dependencies○ multivalued dependencies

Based on and image from “Database System Concepts” book and slides, 6th edition

28

Page 29: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lecture Outline

• Relational Database Design • First Normal Form (1NF) • Functional Dependencies • Boyce-Codd (BCNF) • Third Normal Form

29

Page 30: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Functional Dependencies● Constraints on the set of legal relations● Require that the value for a certain set of attributes

determines uniquely the value for another set of attributes● A functional dependency is a generalization of the notion of

a key

Based on and image from “Database System Concepts” book and slides, 6th edition

30

Page 31: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Remember● r is a relation● r(R) is the schema for the relation r ● R denotes the set of attributes● K represents the set of attributes that is the superkey● A superkey – set of attributes that uniquely identify a tuple

Based on and image from “Database System Concepts” book and slides, 6th edition

31

Page 32: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Functional Dependencies● Let R be a relation schema

● The functional dependency

● holds on R if and only if for any legal relations r(R), whenever any two tuples t1 and t2 of r agree on the

attributes , they also agree on the attributes . That is

● Example: Consider r(A,B ) with the following instance of r

● On this instance, A→B does NOT hold, but B→A does holdBased on and image from “Database System Concepts” book and slides, 6th edition

32

Page 33: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Functional Dependencies● Functional dependencies allow us to express constraints

that cannot be expressed using superkeys. Consider the schema: inst_dept (ID, name, salary, dept_name, building, budget )

We expect these functional dependencies to hold: dept_name→building but would not expect the following to hold: dept_name→salary

Based on and image from “Database System Concepts” book and slides, 6th edition

33

Page 34: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Functional Dependencies● We use functional dependencies to:

○ test relations to see if they are legal under a given set of functional dependencies

■ If a relation r is legal under a set F of functional dependencies, we say that r satisfies F

○ specify constraints on the set of legal relations● We say that F holds on R if all legal relations on R satisfy

the set of functional dependencies F● Note: A specific instance of a relation schema may satisfy a

functional dependency even if the functional dependency does not hold on all legal instances

○ For example, a specific instance of instructor may sometimes satisfy

name→IDBased on and image from “Database System Concepts” book and slides, 6th edition

34

Page 35: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Functional Dependencies● A functional dependency is trivial if it is satisfied by all

instances of a relation

Based on and image from “Database System Concepts” book and slides, 6th edition

35

Page 36: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Functional Dependencies Examples

Assume schema:

student(student_id, first_name, last_name, major, SSN)

Which are true in regards to functional dependencies:

student_id → last_name last_name → student_id student_id → last_name, major, SSN, student_id SSN → student_id, last_name, major, SSN first_name→ last_name last_name → last_name

Based on and image from “Database System Concepts” book and slides, 6th edition

36

Page 37: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Functional Dependencies Examples

Based on and image from “Database System Concepts” book and slides, 6th edition

37

Assume schema:

student(student_id, first_name, last_name, major, SSN)

Which are true in regards to functional dependencies:

student_id → last_name TRUElast_name → student_id FALSE student_id → last_name, major, SSN, student_id TRUESSN → student_id, last_name, major, SSN TRUE first_name→ last_name FALSElast_name → last_name TRUE

Page 38: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Closure of a Set of Functional Dependencies

● Given a set F of functional dependencies, there are certain other functional dependencies that are logically implied by F

○ For example: Given a schema r(A,B,C) If A → B and B → Cthen we can infer that A → C

● The set of all functional dependencies logically implied by F is the closure of F

● We denote the closure of F by F+ ● F+ is a superset of F

Based on and image from “Database System Concepts” book and slides, 6th edition

38

Page 39: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lecture Outline

• Relational Database Design • First Normal Form (1NF) • Functional Dependencies • Boyce-Codd (BCNF) • Third Normal Form (Intro)

39

Page 40: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Boyce-Codd Normal Form

Based on and image from “Database System Concepts” book and slides, 6th edition

40

Page 41: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Boyce-Codd Normal Form

Based on and image from “Database System Concepts” book and slides, 6th edition

41

Page 42: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Boyce-Codd Normal Form

Based on and image from “Database System Concepts” book and slides, 6th edition

42

Page 43: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Decomposing a Schema into BCNF

Based on and image from “Database System Concepts” book and slides, 6th edition

43

Page 44: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Convert it to BCNF

Based on and image from “Database System Concepts” book and slides, 6th edition

44

Schema:

Student(ID,Name,AdvisorID,AdvisorName)

What are the functional dependencies?

Page 45: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Convert it to BCNF

Based on and image from “Database System Concepts” book and slides, 6th edition

45

Schema:

Student(ID,Name,AdvisorID,AdvisorName)

What are the functional dependencies?

ID -> NameAdvisorID -> AdvisorName

What uniquely identifies the tuples?

Page 46: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Convert it to BCNF

Based on and image from “Database System Concepts” book and slides, 6th edition

46

Schema:

Student(ID,Name,AdvisorID,AdvisorName)

What are the functional dependencies?

ID -> NameAdvisorID -> AdvisorName

What uniquely identifies the tuples?(ID,AdvisorID)

Is there a BCNF violation?

Page 47: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Convert it to BCNF

Based on and image from “Database System Concepts” book and slides, 6th edition

47

Schema:

Student(ID,Name,AdvisorID,AdvisorName)

What are the functional dependencies?

ID -> NameAdvisorID -> AdvisorName

What is the primary key?(ID,AdvisorID)

Is there a BCNF violation? YES!

Page 48: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Convert it to BCNF

Based on and image from “Database System Concepts” book and slides, 6th edition

48

Schema: Student(ID,Name,AdvisorID,AdvisorName)

What are the functional dependencies?ID -> NameAdvisorID -> AdvisorName

What is the primary key?(ID,AdvisorID)

Is there a BCNF violation? YES!

Use ID-> Name to decompose R- (Name-ID)= (ID,AdvisorID,AdvisorName) and ID union Name = (ID,Name)

Page 49: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

BCNF and Dependency Preservation

Based on and image from “Database System Concepts” book and slides, 6th edition

49

● Constraints, including functional dependencies, are costly to check in practice unless they pertain to only one relation

● If it is sufficient to test only those dependencies on each individual relation of a decomposition in order to ensure that all functional dependencies hold, then that decomposition is dependency preserving

● Because it is not always possible to achieve both BCNF and dependency preservation, we consider a weaker normal form, known as third normal form

Page 50: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Lecture Outline

• Relational Database Design • First Normal Form (1NF) • Functional Dependencies • Boyce-Codd (BCNF) • Third Normal Form (Intro)

50

Page 51: CMSC 461, Database Management Systems Lecture 11 - Chapter ...jsleem1/courses/461/spr... · Lecture 11 - Chapter 8 Relational Database Design Part 1 These slides are based on “Database

Third Normal Form

Based on and image from “Database System Concepts” book and slides, 6th edition

51