Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Database Systems

1. DATABASE FOR COMPANY


All examples discussed below refer to the COMPANY
database shown here.

2
2. RELATIONAL ALGEBRA
The basic set of operations for the relational model is
known as the relational algebra. These operations
enable a user to specify basic retrieval requests.
The result of retrieval is a new relation, which may
have been formed from one or more relations. The
algebra operations thus produce new relations,
which can be further manipulated using operations of
the same algebra.
A sequence of relational algebra operations forms a
relational algebra expression, whose result will also
be a relation that represents the result of a database
query (or retrieval request).
3
3. UNARY RELATIONAL OPERATIONS
SELECT Operation

o SELECT operation is used to select a subset of


the tuples from a relation that satisfy a selection
condition.
o It is a filter that keeps only those tuples that
satisfy a certain condition –those satisfying the
condition are selected while others are
discarded.

4
3. UNARY RELATIONAL OPERATIONS
SELECT Operation
o Example: To select the EMPLOYEE tuples whose
department number is four or those whose salary is
greater than $30,000, the following notation is used:
 σDNO = 4 (EMPLOYEE)
 σSALARY > 30,000 (EMPLOYEE)
o In general, the select operation is denoted by
σ<selection condition>(R) where the symbol σ(sigma) is
used to denote the select operator, and the
selection condition is a Boolean expression
specified on the attributes of relation R
5
3. UNARY RELATIONAL OPERATIONS
SELECT Operation
o SELECT Operation Properties
 The SELECT operation σ<selection condition>(R) produces a
relation S that has the same schema as R.
 The SELECT operation σ is commutative;
 σ<selection condition1>(σ<selection condition2>(R)) = σ<selection
condition2>(σ<selection condition1>(R))

6
3. UNARY RELATIONAL OPERATIONS
SELECT Operation
o SELECT Operation Properties
 A cascaded SELECT operation may be applied in any
order;
 σ<selection condition1>(σ<selection condition2>(σ<selection
condition3> (R))) = σ<selection condition2>(σ<selection
condition3> (σ<selection condition1>(R)))
 A cascaded SELECT operation may be replaced by a
single selection with a conjunction of all conditions;
 σ<selection condition1>(σ<selection condition2>(σ<selection condition3>(R))) =
σ<selection condition1> AND σ<selection condition2> AND σ<selection condition3>(R)
7
3. UNARY RELATIONAL OPERATIONS
PROJECT Operation

o This operation selects certain columns from the


table and discards the other columns.
o The PROJECT creates a vertical partitioning –
one with the needed columns (attributes)
containing results of the operation and other
containing the discarded Columns.

8
3. UNARY RELATIONAL OPERATIONS
PROJECT Operation
o Example: To list each employee’s first and last name
and salary, the following is used:
 πLNAME, FNAME, SALARY (EMPLOYEE).
o The general form of the project operation is π<attribute
(R) where π (pi) is the symbol used to represent
list>
the project operation and <attribute list> is the
desired list of attributes from the attributes of
relation R.
o The project operation removes any duplicate
tuples, so the result of the project operation is a set
of tuples and hence a valid relation.
9
3. UNARY RELATIONAL OPERATIONS
PROJECT Operation
o PROJECT Operation Properties
 The number of tuples in the result of
projection π <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 is equal to the
number of tuples in R.
 π <list1> (π <list2> (R) ) = π <list1> (R) as long as
<list2> contains the attributes in <list1>

10
3. UNARY RELATIONAL OPERATIONS
 Example
(a) σ(DNO=4 AND SALARY>2500) OR (DNO=5 And SALARY>30000) (EMPLOYEE).

(b) πLNAME, FNAME, SALARY (EMPLOYEE).

11
3. UNARY RELATIONAL OPERATIONS
Rename Operation
o 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.
o In the latter case, we must give names to the
relations that hold the intermediate results.

12
3. UNARY RELATIONAL OPERATIONS
Rename Operation
o 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.
o We can write a single relational algebra expression:
 π FNAME, LNAME, SALARY(σ DNO=5(EMPLOYEE))
o OR we can explicitly show the sequence of operations,
giving a name to each intermediate relation:
 DEP5_EMPS ← σ DNO=5(EMPLOYEE)
 RESULT ← π FNAME, LNAME, SALARY (DEP5_EMPS)

13
3. UNARY RELATIONAL OPERATIONS
Rename Operation
o Example
(a) π FNAME, LNAME, SALARY (σ DNO=5 (EMPLOYEE)).

(b) The same expression using intermediate relations and


renaming of attribute.

14
3. SET OPERATIONS
Type Compatibility
o The operand relations R1 (A1, A2, ..., An) and
R2(B1, B2, ..., Bn) must have
 the same number of attributes, and
 the domains of corresponding attributes
must be compatible; dom(Ai) = dom(Bi) for
i=1, 2, ..., n.
o The resulting relation has the same attribute
names as the first operand relation R1 (by
convention).
15
3. SET OPERATIONS
UNION Operation
o The result of this operation, denoted by R ∪ S, is a
relation that includes all tuples that are in R or in S or
in both R and S. Duplicate tuples are eliminated.
o Example: To retrieve the social security numbers of all
employees who either work in department 5 or directly
supervise an employee who works in department 5, we
can use the union operation as follows:

16
3. SET OPERATIONS
UNION Operation

 DEP5_EMPS ← σDNO=5 (EMPLOYEE)

 RESULT1 ← π SSN(DEP5_EMPS)

 RESULT2 (SSN) ← π SUPERSSN (DEP5_EMPS)

 RESULT ← RESULT1 ∪ RESULT2

 The union operation produces the tuples that are in


either RESULT1 or RESULT2 or both. The two operands
must be “type compatible”.
17
3. SET OPERATIONS
UNION Operation
o Example
 STUDENT ∪ INSTRUCTOR

18
3. SET OPERATIONS
INTERSECTION OPERATION
o The result of this operation, denoted by R Ç S, is
a relation that includes all tuples that are in both
R and S.
o The two operands must be "type compatible“
o Example: STUDENT Ç INSTRUCTOR

19
3. SET OPERATIONS
Set Difference (or MINUS) Operation
o The result of this operation, denoted by R - S, is
a relation that includes all tuples that are in R
but not in S.
o The two operands must be "type compatible“

20
3. SET OPERATIONS
Set Difference (or MINUS) Operation
o Example: The figure shows the names of
students who are not instructors, and the names
of instructors who are not students.
STUDENT - INSTRUCTOR

INSTRUCTOR - STUDENT

21
3. SET OPERATIONS
Notice that both union and intersection are
commutative operations;
R È S = S È R, and R Ç S = S Ç R
Both union and intersection can be treated as n-ary
operations applicable to any number of relations as
both are associative operations;
R È (S È T) = (R È S) È T, and
(R Ç S) Ç T = R Ç (S Ç T)
The minus operation is not commutative;
R – S ≠ S – R

22
3. SET OPERATIONS
CARTESIAN (or cross product) Operation
o This operation is used to combine tuples from
two relations in a combinatorial fashion.
o In general, the result of R(A1, A2, . . ., An) x S(B1,
B2, . . ., Bm) is a relation Q with degree n + m
attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in
that order.
o The resulting relation Q has one tuple for each
combination of tuples—one from R and one
from S.

23
3. SET OPERATIONS
CARTESIAN (or cross product) Operation
o Hence, if R has nR tuples (denoted as |R| = nR ), and S
has nS tuples, then
o | R x S | will have nR * nS tuples.
o The two operands do NOT have to be "type
compatible”
o Example: retrieve a list of names of each female
employee’s dependents.
 FEMALE_EMPS ← σ SEX=’F’(EMPLOYEE)
 EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)
 EMP_DEPENDENTS ← EMPNAMES x DEPENDENT
o EMP_DEPENDENTS will contain every combination of
EMPNAMES and DEPENDENT
 whether or not they are actually related 24
3. SET OPERATIONS
To keep only combinations where the DEPENDENT is
related to the EMPLOYEE, we add a SELECT operation
as follows
o Example: retrieve a list of names of each female
employee’s dependents.
 FEMALE_EMPS ← σ SEX=’F’(EMPLOYEE)
 EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)
 EMP_DEPENDENTS ← EMPNAMES x DEPENDENT
 ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS)
 RESULT ← πFname, Lname, Dependent_name(ACTUAL_DEPENDENTS)
RESULT will now contain the name of female
employees and their dependents

25
3. SET OPERATIONS
CARTESIAN (or cross product) Operation

FEMALE_EMPS ← σ SEX=’F’ (EMPLOYEE)

26
3. SET OPERATIONS
CARTESIAN (or cross product) Operation
EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)

DEPENDENT table:

27
3. SET OPERATIONS
CARTESIAN (or cross product) Operation
EMP_DEPENDENTS ← EMPNAMES x DEPENDENT

28
3. SET OPERATIONS
CARTESIAN (or cross product) Operation

ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS)

RESULT ← πFname, Lname, Dependent_name (ACTUAL_DEPENDENTS)

29
4. BINARY RELATIONAL OPERATIONS
JOIN Operation
o The sequence of Cartesian product followed by select is
used quite commonly to identify and select related tuples
from two relations, a special operation, called JOIN. It is
denoted by a
o This operation is very important for any relational
database with more than a single relation, because it
allows us to process relationships among relations.
o The general form of a join operation on two relations
R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
R S
<join condition>

o where R and S can be any relations that result from


general relational algebra expressions. 30
4. BINARY RELATIONAL OPERATIONS
JOIN Operation
o Example: Suppose that we want to retrieve the name of
the manager of each department. To get the manager’s
name, we need to combine each DEPARTMENT tuple
with the EMPLOYEE tuple whose SSN value matches the
MGRSSN value in the department tuple. We do this by
using the join operation.
o DEPT_MGR ← DEPARTMENT MGRSSN=SSN EMPLOYEE

31
4. BINARY RELATIONAL OPERATIONS
EQUIJOIN Operation: The most common use of join involves
join conditions with equality comparisons (=) only. Such a join,
is called an EQUIJOIN. In the result of an EQUIJOIN we always
have one or more pairs of attributes (whose names need not be
identical) that have identical values in every tuple. The JOIN
seen in the previous example was EQUIJOIN.
NATURAL JOIN Operation: Because one of each pair of
attributes with identical values is superfluous, a new operation
called natural join—denoted by *—was created to get rid of the
second (superfluous) attribute in an EQUIJOIN condition. The
standard definition of natural join requires that the two join
attributes, or each pair of corresponding join attributes, have the
same name in both relations. If this is not the case, a renaming
operation is applied first.

32
4. BINARY RELATIONAL OPERATIONS
Example: To apply a natural join on the DNUMBER
attributes of DEPARTMENT and DEPT_LOCATIONS, it is
sufficient to write:
DEPT_LOCS ←DEPARTMENT *DEPT_LOCATIONS

33
5. ADDITIONAL RELATIONAL OPERATIONS
Aggregate Functions and Grouping
A type of request that cannot be expressed in the basic
relational algebra is to specify mathematical aggregate
functions on collections of values from the database.
Examples of such functions include retrieving the average
or total salary of all employees or the total number of
employee tuples. These functions are used in simple
statistical queries that summarize information from the
database tuples.
Common functions applied to collections of numeric
values include SUM, AVERAGE, MAXIMUM, and
MINIMUM. The COUNT function is used for counting
tuples or values.
34
5. ADDITIONAL RELATIONAL OPERATIONS
Aggregate Functions and Grouping
We can define an AGGREGATE FUNCTION operation,
using the symbol ℱ (pronounced script F), to specify these
types of requests as follows:
<grouping attributes> ℱ <function list> (R)
<grouping attributes> is a list of attributes of the relation
specified in R.
<function list> is a list of (<function> <attribute>) pairs. In
each such pair, <function> is one of the allowed functions,
such as SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT,
and <attribute> is an attribute of the relation specified by R.

35
5. ADDITIONAL RELATIONAL OPERATIONS
Aggregate Functions and Grouping
 ρR(Dno, No_of_employees, Average_sal)(Dno ℱ COUNT Ssn, AVERAGE
Salary (EMPLOYEE)).

 Dno ℱ COUNT Ssn, AVERAGE Salary(EMPLOYEE).

 ℱ COUNT Ssn, AVERAGE Salary(EMPLOYEE).


o if no grouping attributes are specified, the
functions are applied to all the tuples in the
relation

36
5. ADDITIONAL RELATIONAL OPERATIONS
Aggregate Functions and Grouping
ℱMAX Salary(Employee) retrieves the maximum salary value from
the Employee relation
ℱ (Employee) retrieves the minimum Salary value from
MIN Salary

the Employee relation


ℱ (Employee) retrieves the sum of the Salary from the
SUM Salary

Employee relation
 ℱ
DNO (Employee) groups employees by DNO
COUNT SSN, AVERAGE Salary

(department number) and computes the count of employees and


average salary per department.[ Note: count just counts the
number of rows, without removing duplicates]

37
6. EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA

38
6. EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA
Q1: Retrieve the name and address of all employees who work
for the ‘Research’ department.

Q2: For every project located in ‘Stafford’, list the project


number, the controlling department number, and the
department manager’s last name, address, and birth date.

39
6. EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA
Q1: Retrieve the name and address of all employees who work
for the ‘Research’ department.
o RESEARCH_DEPT ← σDNAME = ’Research’ (DEPARTMENT)
o RESEARCH_EMPS ←(RESEARCH_DEPT DNUMBER= DNO EMPLOYEE)
o RESULT ←πFNAME, LNAME, ADDRESS(RESEARCH_EMPS)
Q2: For every project located in ‘Stafford’, list the project
number, the controlling department number, and the
department manager’s last name, address, and birth date.

40
6. EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA
Q1: Retrieve the name and address of all employees who work
for the ‘Research’ department.
o RESEARCH_DEPT ← σDNAME = ’Research’ (DEPARTMENT)
o RESEARCH_EMPS ←(RESEARCH_DEPT DNUMBER= DNO EMPLOYEE)
o RESULT ←πFNAME, LNAME, ADDRESS(RESEARCH_EMPS)
Q2: For every project located in ‘Stafford’, list the project
number, the controlling department number, and the
department manager’s last name, address, and birth date.
o STAFFORD_PROJS ← σPLOCATION=‘Stafford’ (PROJECT)
o CONTR_DEPTS ← (STAFFORD_PROJS DNUM=DNUMBER DEPARTMENT)
o PROJ_DEPT_MGRS ← (CONTR_DEPTS MGR_SSN=SSN EMPLOYEE)
o RESULT ← πPNUMBER, DNUM, LNAME, ADDRESS, BDATE(PROJ_DEPT_MGRS)

41

You might also like