Lecture 5
Lecture 5
Relational Algebra,
Functional Dependencies and
Normalization
(Chapter 8 and 14)
[email protected]
Connection to previous lectures
ER/EER Relational Model How good is your model?
2(44)
Outline
• Functional Dependencies
• Normalization: 1NF,2NF,3NF
• Break 10 min
• Relational Algebra Operations
3(44)
Informal Guidelines for Relation Databases
5(44)
Informal Guidelines for Relation Databases
Example
Employee_Department
7(44)
Informal Guidelines for Relation
Databases Example 1 No Spurious Rows
11(44)
Methodology for testing and improving
Relation Database
• Using the Normalization of Relations
• The normalization process take a relation database (or schema) through a
series of tests to certify whether it satisfies a certain normal form.
• All the tables in a database can be in one of the normal forms.
• There are six normal forms, but we will look at the first three, which are:
• First normal norm (1NF)
• Second normal norm (2NF)
• Third normal norm (3NF)
• The normal form of a table refers to the highest normal form condition
that it meets, and hence indicates the degree to which it has been
normalized
12(44)
Department table is not in 1NF
13(44)
Normalizing nested relations to 1NF
14(44)
Second Normal Form( 2NF)
Normalizing EMP_PROJ into 2NF relations
• A database (relation schema) R is in 2NF if every
nonprime attribute A in R is fully functionally
dependent on the primary key of R.
• Fully functionally dependent means PK are single-
valued attribute or is a composite PK, then each
non-key attribute must be fully dependent on the
entire PK and not on a subset of the PK (i.e., there
must be no partial dependency).
• Normalization to 2NF:
• The relation must first be in 1NF. We can not
jump from non 1NF to 2NF without first to
normalize to 1NF. So, non NF → 1NF →2NF
• Decompose and set up a new table for each
partial key with its dependent attribute(s).
15(44)
Third Normal Form (3NF)
Example: EMP_DEPT in 2NF form.
Hence it has non-key attributes FDs. Non-key attribute
• A database (relation schema) R is in 3NF if it Dnumber uniquely identifies other non-key attributes
satisfies 2NF and no noprime attribute of R is Dname and Dmgr_snn. Thus violate the 3NF constrain
transitively dependent on the primary key.
• More precisely: a non-key attribute may not be
functionally dependent on another non-key
attribute.
• Normalization to 3NF:
• The relation must first be in 2NF. We can
not jump from non 1NF to 3NF without first
to normalize to 2NF. So, non NF → 1NF
→2NF →3NF ED1 table have single-valued primary key Ssn which uniquely
• Decompose and set up a relation that identifies all other attributes. No FDs between non-key
includes the nonkey attribute(s) that Attributes
functionally determine(s) other nonkey
Same for ED2
attribute(s).
16(44)
Summary of Three Normal Forms
17(44)
Break 10 min
18(44)
Relational Algebra
• Relational algebra is the basic set of operations for the relational model
• These operations enable a user to specify basic retrieval requests (or queries)
• The result of an operation is a new relation, which may have been formed
from one or more input relations
• The algebra operations thus produce new relations
• These can be further manipulated using operations of the same algebra
• A sequence of relational algebra operations forms a relational algebra
expression
• The result of a relational algebra expression is also a relation that represents the
result of a database query (or retrieval request)
• Relation (formal name) means table (informal name)
19(44)
Relational Algebra Overview
• Relational Algebra consists of several groups of operations
• Unary Relational Operations
• SELECT (symbol: s (sigma))
• PROJECT (symbol: p (pi))
• RENAME (symbol: r (rho))
• Relational Algebra Operations From Set Theory
• UNION ( È ), INTERSECTION ( Ç ), DIFFERENCE (or MINUS, – )
• CARTESIAN PRODUCT ( x )
• Binary Relational Operations
• JOIN (several variations of JOIN exist)
• DIVISION
• Additional Relational Operations
• OUTER JOINS, OUTER UNION
• AGGREGATE FUNCTIONS (These compute summary of information: for example, SUM, COUNT,
AVG, MIN, MAX)
20(44)
Company Schema
21(44)
Unary Relational Operations: SELECT
• The SELECT operation (denoted by s (sigma)) is used to select a subset of the tuples from a
relation based on a selection condition.
• The selection condition acts as a filter
• Keeps only those tuples that satisfy the qualifying condition
• Tuples satisfying the condition are selected whereas the other tuples are discarded
(filtered out)
• Examples:
• Select the EMPLOYEE tuples whose department number is 4:
s DNO = 4 (EMPLOYEE)
• Select the employee tuples whose salary is greater than $30,000:
s SALARY > 30,000 (EMPLOYEE)
22(44)
Unary Relational Operations: SELECT
• In general, the select operation is denoted by s <selection condition>(R)
where
• the symbol s (sigma) is used to denote the select operator
• the selection condition is a Boolean (conditional) expression specified on the attributes
of relation R
• tuples that make the condition true are selected
• appear in the result of the operation
• tuples that make the condition false are filtered out
• discarded from the result of the operation
23(44)
Unary Relational Operations: PROJECT
• PROJECT Operation is denoted by p (pi)
• This operation keeps certain columns (attributes) from a relation and
discards the other columns.
• PROJECT creates a vertical partitioning
• The list of specified columns (attributes) is kept in each tuple
• The other attributes in each tuple are discarded
• Example: To list each employee’s first and last name and salary, the
following is used:
pLNAME, FNAME,SALARY(EMPLOYEE)
24(44)
Unary Relational Operations: PROJECT (cont.)
• The general form of the project operation is:
p<attribute list>(R)
• p (pi) is the symbol used to represent the project operation
• <attribute list> is the desired list of attributes from relation R.
• The project operation removes any duplicate tuples
• This is because the result of the project operation must be a set of tuples
• Mathematical sets do not allow duplicate elements.
• PROJECT Operation Properties
• The number of tuples in the result of projection p<list>(R) is always less or equal to the
number of tuples in R
• If the list of attributes includes a key of R, then the number of tuples in the result of PROJECT
is equal to the number of tuples in R
• PROJECT is not commutative
• p <list1> (p <list2> (R) ) = p <list1> (R) as long as <list2> contains the attributes in <list1>
25(44)
Examples of applying SELECT and PROJECT
operations
26(44)
Relational Algebra Expressions
• We may want to apply several relational algebra operations one after
the other
• Either we can write the operations as a single relational algebra expression
by nesting the operations, or
• We can apply one operation at a time and create intermediate result
relations.
• In the latter case, we must give names to the relations that hold
the intermediate results.
27(44)
Single expression versus sequence of
relational operations (Example)
• To retrieve the first name, last name, and salary of all employees who work in
department number 5, we must apply a select and a project operation
• We can write a single relational algebra expression as follows:
• pFNAME, LNAME, SALARY(s DNO=5(EMPLOYEE))
• OR We can explicitly show the sequence of operations, giving a name to each
intermediate relation:
• DEP5_EMPS ¬ s DNO=5(EMPLOYEE)
• RESULT ¬ p FNAME, LNAME, SALARY (DEP5_EMPS)
28(44)
Unary Relational Operations: RENAME
• The RENAME operator is denoted by r (rho)
• In some cases, we may want to rename the attributes of a relation or the relation
name or both
• Useful when a query requires multiple operations
• Necessary in some cases (see JOIN operation later)
• The general RENAME operation r can be expressed by any of the following forms:
• rS (B1, B2, …, Bn )(R) changes both:
• the relation name to S, and
• the column (attribute) names to B1, B1, …..Bn
• rS(R) changes:
• the relation name only to S
• r(B1, B2, …, Bn )(R) changes:
• the column (attribute) names only to B1, B1, …..Bn
29(44)
Unary Relational Operations: RENAME
(continued)
• For convenience, we also use a shorthand for renaming attributes in
an intermediate relation:
• If we write:
• RESULT ¬ p FNAME, LNAME, SALARY (DEP5_EMPS)
• RESULT will have the same attribute names as DEP5_EMPS (same
attributes as EMPLOYEE)
• If we write:
• RESULT (F, M, L, S, B, A, SX, SAL, SU, DNO)¬ r RESULT (F.M.L.S.B,A,SX,SAL,SU,
DNO)(DEP5_EMPS)
• The 10 attributes of DEP5_EMPS are renamed to F, M, L, S, B, A, SX,
SAL, SU, DNO, respectively
Note: the ¬ symbol is an assignment operator
30(44)
Relational Algebra Operations from
Set Theory: UNION
• UNION Operation
• Binary operation, denoted by È
• The result of R È S, is a relation that includes all tuples that are either in R or
in S or in both R and S
• Duplicate tuples are eliminated
• The two operand relations R and S must be “type compatible” (or UNION
compatible)
• R and S must have same number of attributes
• Each pair of corresponding attributes must be type compatible (have same or
compatible domains)
31(44)
Relational Algebra Operations from
Set Theory: UNION
• Example:
• To retrieve the social security
numbers of all employees who either
work in department 5 (RESULT1
below) or directly supervise an
employee who works in department 5
(RESULT2 below)
• We can use the UNION operation as
follows:
DEP5_EMPS ¬ sDNO=5 (EMPLOYEE)
RESULT1 ¬ p SSN(DEP5_EMPS)
RESULT2(SSN) ¬ pSUPERSSN(DEP5_EMPS)
RESULT ¬ RESULT1 È RESULT2
• The union operation produces the
tuples that are in either RESULT1 or
RESULT2 or both
32(44)
Relational Algebra Operations from Set
Theory: INTERSECTION
• INTERSECTION is denoted by Ç
• The result of the operation R Ç S, is a relation that includes
all tuples that are in both R and S
• The attribute names in the result will be the same as the
attribute names in R
• The two operand relations R and S must be “type
compatible”
33(44)
Example to illustrate the result of UNION,
INTERSECT, and DIFFERENCE
34(44)
Key terms (for Exam)
• A functional dependency(FD) is a constraint between two sets of attributes
from the database.
• A normalization is a process to determining how much redundancy exists
in a database. This process involves analysis of relational schema based on
their functional dependencies and primary key to minimizing redundancy,
insertions, edition, and update anomalies.
• The normal form of a relation/table refers to the highest normal form
condition that is meets, and indicates the degree to which it has been
normalized.
• There are six normal forms, but only three most commonly used are: 1NF,
2NF, and 3NF
• Th content and understanding of table 14.1 (slide 17)
35(44)