17
The Relational The Relational Model Model Chapter Two Chapter Two Functional Functional Dependency Dependency

The Relational Model Chapter Two Functional Dependency

  • View
    225

  • Download
    2

Embed Size (px)

Citation preview

The Relational The Relational ModelModel

Chapter TwoChapter Two

Functional Functional DependencyDependency

2

Functional DependencyFunctional Dependency

A relationship between attributes in A relationship between attributes in which one attribute (or group of which one attribute (or group of attributes) determines the value of attributes) determines the value of another attribute in the same tableanother attribute in the same table

Illustration…Illustration… The price of one cookie can determine The price of one cookie can determine

the price of a box of 12 cookiesthe price of a box of 12 cookies

(CookiePrice, Qty) BoxPrice

3

DeterminantsDeterminants

The attribute (or attributes) that we The attribute (or attributes) that we use as the starting point (the use as the starting point (the variable on the left side of the variable on the left side of the equation) is called a equation) is called a determinantdeterminant

(CookiePrice, Qty) BoxPrice

Determinant

4

Functional Dependency Functional Dependency ExampleExample

Given different color objects:Given different color objects:Red objects – 5 poundsRed objects – 5 poundsBlue objects – 3 poundsBlue objects – 3 poundsYellow objects – 7 poundsYellow objects – 7 pounds

If you look in a bag and see an object, the If you look in a bag and see an object, the weight of the bag is weight of the bag is determineddetermined by the by the color of the object.color of the object.

ObjectColor ObjectColor Weight Weight

5

Functional Dependency Functional Dependency ExampleExample

Given different color objects:Given different color objects:Red objects – Ball shapeRed objects – Ball shapeBlue objects – Cube shapeBlue objects – Cube shapeYellow objects – Cube shapeYellow objects – Cube shape

The ObjectColor also determines shape.The ObjectColor also determines shape.

ObjectColor ObjectColor Shape Shape

6

Functional Dependency Functional Dependency ExampleExample

So, ObjectColor determines both So, ObjectColor determines both Weight and Shape.Weight and Shape.

ObjectColor ObjectColor (Weight, Shape) (Weight, Shape)

7

Functional Dependency Functional Dependency ExampleExample

Represent this data as a Relational Represent this data as a Relational Table…Table…

OBJECT (OBJECT (ObjectColorObjectColor, Weight, Shape), Weight, Shape)

ObjectColObjectColoror

WeighWeightt

ShaShapepe

RedRed 55 BallBall

BlueBlue 33 CubCubee

YellowYellow 77 CubCubee

8

Candidate/Primary Keys Candidate/Primary Keys and Functional and Functional

DependencyDependency By definition…By definition…

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

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

9

Primary Key and Primary Key and Functional Functional

Dependency ExampleDependency Example(EmployeeID)

(EmpLastName, EmpPhone)

(ProjectID) (ProjectName, StartDate)

10

NormalizationNormalization

NormalizationNormalization is a process of is a process of analyzing a relation to ensure that it analyzing a relation to ensure that it is is well-formedwell-formed

More specifically, if a relation is More specifically, if a relation is normalized (well-formed), rows can normalized (well-formed), rows can be inserted, deleted, or modified be inserted, deleted, or modified without creating update anomalieswithout creating update anomalies

11

Normalization ExampleNormalization Example Given: ADVISER-LIST (AdviserID, Given: ADVISER-LIST (AdviserID,

AdviserName, Dept, Phone, Office, AdviserName, Dept, Phone, Office, StudentNum, StudentName)StudentNum, StudentName)

Valid Relational table by the rules defined Valid Relational table by the rules defined earlier!earlier!

So StudentNum So StudentNum (AdviserID, (AdviserID, AdviserName, Dept, Phone, Office, AdviserName, Dept, Phone, Office, StudentName)StudentName)

Relation: ADVISER-LIST (AdviserID, Relation: ADVISER-LIST (AdviserID, AdviserName, Dept, Phone, Office, AdviserName, Dept, Phone, Office, StudentNumStudentNum, StudentName), StudentName)

12

Normalization ExampleNormalization Example

An Advisor’s phone number can appear in An Advisor’s phone number can appear in multiple rows. Take out advisor information.multiple rows. Take out advisor information.

Functional Dependency: Functional Dependency:

AdviserID AdviserID (AdviserName, Dept, Phone, (AdviserName, Dept, Phone, Office)Office)

This Relation is poorly formed because it has a This Relation is poorly formed because it has a functional dependency that does NOT involve functional dependency that does NOT involve the primary key!the primary key!

i.e. AdviserID is a determinant of a functional i.e. AdviserID is a determinant of a functional dependency but it is NOT a candidate key!dependency but it is NOT a candidate key!

13

Normalization PrinciplesNormalization Principles

Relational design principles for Relational design principles for normalized relations:normalized relations: To be a well-formed relation, every To be a well-formed relation, every

determinantdeterminant must be a must be a candidate keycandidate key Any relation that is not well formed Any relation that is not well formed

should be broken into two or more well-should be broken into two or more well-formed relations.formed relations.

14

Normalization ProcessNormalization Process Normalization is the process of Normalization is the process of

examining relations and modifying them examining relations and modifying them to make them well-formed.to make them well-formed.

You can categorize problems into You can categorize problems into different types called normal forms.different types called normal forms.

Any relation that has characteristics Any relation that has characteristics presented in Fig 2-1 is called First presented in Fig 2-1 is called First Normal Form.Normal Form.

Others: Second, Third, Boyce-Codd, Others: Second, Third, Boyce-Codd, Fourth, Fifth and domain/key normal Fourth, Fifth and domain/key normal form.form.

15

Normalization ProcessNormalization Process

1.1. Identify all candidate keys of the Identify all candidate keys of the relation.relation.

2.2. Identify all functional dependencies Identify all functional dependencies in the relation.in the relation.

3.3. Exampine the determinants of the Exampine the determinants of the functional dependencies. If any functional dependencies. If any determinant is NOT a candidate determinant is NOT a candidate key, the relation has normalization key, the relation has normalization problems. In this case…problems. In this case…

16

Normalization ProcessNormalization Process

3a. Place the columns of the functional 3a. Place the columns of the functional dependency in a new relation of their dependency in a new relation of their own.own.

3b. Make the determinant the functional 3b. Make the determinant the functional dependency of the primary key of the dependency of the primary key of the new relation.new relation.

3c. Leave a copy of the determinant as a 3c. Leave a copy of the determinant as a foreign key in the original relation.foreign key in the original relation.

3d. Create a referential integrity constraint 3d. Create a referential integrity constraint between the original relation and the between the original relation and the new relation.new relation.

17

Normalization ProcessNormalization Process

4. Repeat step 3 as many times as 4. Repeat step 3 as many times as necessary until every determinant necessary until every determinant of every relation is a candidate key.of every relation is a candidate key.