2 the relational model and normalization (cont.)3).pdf · the table and a primary key can be ......

18
1 §2 – The Relational Model and Normalization (cont.) Fen Wang [email protected] Lecture 6 IT468 DB @ ITAM 2 Agenda The foundation of the relational model Characteristics of relations The basic relational terminology Primary, candidate, and composite keys The purpose and use of surrogate keys How foreign keys represent relationships The meaning of functional dependencies Possible insertion, deletion, and update anomalies Apply a process for normalizing relations

Upload: phamkien

Post on 13-Mar-2018

238 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

1

§2 – The Relational Model and Normalization (cont.)

Fen Wang

[email protected]

Lecture 6

IT468 DB @ ITAM

2

Agenda

• The foundation of the relational model

• Characteristics of relations

• The basic relational terminology

• Primary, candidate, and composite keys

• The purpose and use of surrogate keys

• How foreign keys represent relationships

• The meaning of functional dependencies

• Possible insertion, deletion, and update anomalies

• Apply a process for normalizing relations

Page 2: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

2

3

Functional Dependency

• A functional dependency occurs when one attribute (or group of attributes) determines the value of another attribute in the same table

StudentID StudentName

StudentID (DormName, DormRoom, Fee)

• The attribute on the left side of the functional dependency is called the determinant

• Functional dependencies may be based on equations:

ExtendedPrice = Quantity X UnitPrice

(Quantity, UnitPrice) ExtendedPrice

• Function dependencies are not equations!

4

Functional Dependencies That Are Not Equations

ObjectColor Weight

ObjectColor Shape

ObjectColor (Weight, Shape)

Page 3: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

3

5

Candidate/Primary Keys and Functional Dependency

• By definition… A candidate key of a relation will functionally determine all other attributes in the row

• Likewise, by definition… A primary key of a relation will functionally determine all other attributes in the row

(EmployeeID)

(EmpLastName, EmpPhone)

6

Functional Dependencies in the REP Table

Does any column determine the value of another?

Page 4: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

4

7

Functional Dependencies in the REP Table (cont.)

RepNum (LastName, FirstName, Address, PayClass, Rate)

(LastName, FirstName) (RepNum, Address, PayClass, Rate)

PayClass Rate

Instances in a relation can NOT prove that a functional dependency exists; however, they can be used to

demonstrate that a functional dependency does NOT exist.

8

Functional Dependencies in the REP Table (cont.)

With the new dataset given, the 2nd possible functional dependency can now be rejected:

(LastName, FirstName) (RepNum, Address, PayClass, Rate)

Page 5: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

5

IT468 DB @ ITAM 9

Functional Dependencies in the ORDER_ITEM Table

What are the functional dependencies in this table?

IT468 DB @ ITAM 10

Functional Dependencies in the ORDER_ITEM Table

(OrderNumber, SKU)

(Quantity, Price, ExtendedPrice)

(Quantity, Price) (ExtendedPrice)

Page 6: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

6

11

Functional Dependencies in the Campus Building Scenario

• Which of the following functional dependency diagrams accurately represents the following situation:

– A campus has many buildings.

– Each building has a unique name.

– Each building has many rooms.

– All rooms in any given building are numbered sequentially starting at "101".

– Each room has a certain capacity although many rooms in the same building or different buildings may have the same capacity.

– Each room is assigned to a single department.

– A department may have many rooms in one or more buildings, each with the same or different capacities.

12

Functional Dependencies in the Campus Building Scenario (cont.)

• Which of the following functional dependency diagrams accurately represents the situation (described in the preceding slide):

A) BuildingName → (RoomNumber, Capacity, Department)

B) RoomNumber → (BuildingName, Department, Capacity)

C) (Department, Capacity) → (BuildingName, RoomNumber)

D) (BuildingName, RoomNumber) → (Capacity, Department)

Sample answer:

D

Page 7: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

7

Agenda

• The foundation of the relational model

• Characteristics of relations

• The basic relational terminology

• Primary, candidate, and composite keys

• The purpose and use of surrogate keys

• How foreign keys represent relationships

• The meaning of functional dependencies

• Possible insertion, deletion, and update anomalies

• Apply a process for normalizing relations

Modification Problems (cont.)

• What are the modification problems in this PROJECT_EMPLOYEE table?

Page 8: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

8

Modification Problems (cont.)

• What are the modification problems in this PROJECT_EMPLOYEE table?

– Update: Changes to employee data may need to be made in several rows.

– Delete: Deleting data for a project or employee deletes project hours data as well.

– Insert: Adding a new project record with missing data on employee or project hours

The Source of Modification Problems

• Why do these problems exist?

– When a table contains data from multiple themes, it is subject to modification problems

• This results in data duplication and an unnecessary dependency between the entities

Page 9: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

9

17

Normalization

• A process of analyzing a relation to ensure that it is well formed

– To be a well-formed relation, every determinant must be a candidate key

– Any relation that is not well formed should be broken into two or more well-formed relations

IT468 DB @ ITAM

Normal Forms

• Tables are categorized as a normal form based on which modification anomalies or other problems that they are subject to:

Page 10: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

10

IT468 DB @ ITAM

Normal Forms

• 1NF – A table that qualifies as a relation is in 1NF

• 2NF – A relation is in 2NF if all of its nonkey attributes are dependent on all of the primary key

• 3NF – A relation is in 3NF if it is in 2NF and has no functional dependencies between non-key attributes

• Boyce-Codd Normal Form (BCNF) – A relation is in BCNF if every determinant is a candidate key

“I swear to construct my tables so that all nonkey columns are dependent on the key, the whole key and nothing but the key, so help me Codd.”

IT468 DB @ ITAM

Normal Forms (cont.)

• Normalization

– 2NF is better than 1NF; 3NF is better than 2NF, and BCNF is better than 3NF

– For most business database design purposes, BCNF is as high as we need to go in normalization process

– Highest level of normalization is not always most desirable

Page 11: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

11

IT468 DB @ ITAM

The Normalization Process: An Example

• Example: A customer invoice from Pine Valley Furniture Company

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

• The table representing the invoice

Table with repeating groups, not in 1st normal form

Note: this is NOT a relation

Page 12: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

12

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

Conversion to First Normal Form

• A table is in 1NF if the following two constraints both apply:

– There’re no repeating groups in the table

– A primary key can be defined to uniquely identify each row in the table

Smart tip: if a table qualifies to be a relation, it is in 1NF. In other words,

a relation is a 1NF table!

IT468 DB @ ITAM 24

Table with no repeating groups and unique rows, in 1NF

Note: this is a relation, but not a well-structured one

The Normalization Process: An Example (cont.)

Page 13: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

13

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

Conversion to First Normal Form (Summary)

• A table is in 1NF when there’re no repeating groups in the table and a primary key can be defined to uniquely identify each row in the table

• Some may contain partial dependencies

– Dependencies based on only part of the primary key

– Still subject to data redundancies

INVOICE (Order_ID, Order_date, Customer_ID, Customer_Name,

Customer_Address,Product_ID, Product_Description,

Product_Finish, Unit_Price, Ordered_Quantity)

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

Conversion to Second Normal Form

• Table is in second normal form (2NF) when:

– It is in 1NF and

– It includes no partial dependencies:

• No attribute is dependent on only portion of primary key

• called full functional dependency

– At this point, many anomalies have been eliminated

Page 14: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

14

IT468 DB @ ITAM 27

Order_ID Order_Date, Customer_ID, Customer_Name, Customer_Address

Therefore, NOT in 2nd Normal Form

Product_ID Product_Description, Product_Finish, Unit_Price

Functional dependency diagram for INVOICE

The Normalization Process: An Example (cont.)

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

Conversion to Second Normal Form

1. Identify all partial dependencies

2. Place the columns of the functional dependency in a new table of their own

3. Make the determinant the primary key of the new table

4. Leave a copy of the determinant as a foreign key in the original table

Page 15: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

15

IT468 DB @ ITAM 29

Partial dependencies are removed, but there

are still transitive dependencies

Getting it into Second Normal Form

Removing partial dependencies

The Normalization Process: An Example (cont.)

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

Conversion to Second Normal Form (Summary)

• 2nd Normal Form: If each nonkey attribute is functionally dependent on the whole primary key

• No attribute is dependent on only portion of primary key

• called full functional dependency

• Some tables still contain transitive dependencies

– Dependencies among non-key attributes

Page 16: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

16

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

Conversion to Third Normal Form

• A table is in third normal form (3NF) when both of the following are true:

– It is in 2NF

– It contains no transitive dependencies

• i.e., no functional (transitive) dependencies between two (or more) non-key attributes

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

Conversion to Third Normal Form

1. Identify all transitive dependencies

2. Place the columns of the functional dependency in a new table of their own

3. Make the non-key determinant the primary key of the new table

4. Leave a copy of the non-key determinant as a foreign key in the original table

Page 17: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

17

IT468 DB @ ITAM 33

Transitive dependencies are removed

Removing transitive dependencies

Getting it into Third Normal Form

The Normalization Process: An Example (cont.)

IT468 DB @ ITAM

The Normalization Process: An Example (cont.)

• The 3NF tables representing the original customer invoice

Page 18: 2 The Relational Model and Normalization (cont.)3).pdf · the table and a primary key can be ... Leave a copy of the determinant as a foreign key in the ... The Boyce-Codd Normal

18

IT468 DB @ ITAM

The Boyce-Codd Normal Form (BCNF)

• Every determinant in table is a candidate key

– A candidate key of a relation will functionally determine all other attributes in the row

• When table contains only one candidate key, the 3NF and the BCNF are equivalent

• Most designers consider the BCNF as a special case of 3NF

• Any modification problem arising from functional dependencies are removed

IT468 DB @ ITAM 36

Before next class

• Practice more with MS Access 2010 and MS Visio Professional

• Continue working on Project D1

• Continue working on Assignment#2 (due 10PM on Friday)

• Read Textbook Chapter 2 – Functional Dependencies