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

Chapter Seven

Query languages
Allow manipulation and retrieval of data from a database.
Relational model supports simple, powerful QLs:
 Strong formal foundation based on logic.
 Allows for much optimization.
Query Languages != programming languages!
 QLs not intended to be used for complex calculations.
 QLs support easy, efficient access to large data sets.
Formal Relational Query Languages
Two mathematical Query Languages form the basis
for “real” languages (e.g. SQL):
Relational Algebra: More operational, very useful for
representing execution plans.
Relational Calculus: Lets users describe what they want,
rather than how to compute it. (Non-operational,
declarative.)
Understanding Algebra & Calculus is key to
understanding SQL, query processing!
Preliminaries
A query is applied to relation instances, and the result
of a query is also a relation instance (called closer
property).
Schemas of input relations for a query are fixed (but
query will run regardless of instance!)
The schema for the result of a given query is also
fixed! Determined by definition of query language
constructs.
Relational Algebra
Similar to normal algebra (as in 2+3*x-y), except we
use relations as values instead of numbers, and the
operations and operators are different.
A procedural query language; Based on algebraic
concepts (Algebra – studies quantities, refers to
relations and properties by symbols)
Not used as a query language in actual DBMSs. (SQL
instead.)
The inner, lower-level operations of a relational DBMS
are, or are similar to, relational algebra operations.
Operations in Relational Algebra
 Six fundamental operations:
 select (unary)
 project (unary)
 rename (unary)
 cartesian product (binary)
 union (binary)
 set-difference (binary)
 Several other operations, defined in terms of the fundamental
operations:
 set-intersection
 natural join
 division
 assignment
Notations
Operation Symbol
Operation Symbol

Projection Cartesian product

Selection Join

Left outer join


Renaming

Right outer join


Union

Intersection Full outer join

Assignment Semijoin
Select Operation
Works on a single relation R and defines a relation
that contains only those tuples (rows) of R that satisfy
the specified condition (predicate).
Notation: p(R)
p is called the selection predicate
Formally, select operation is defined as:
p(r) = {t | t  r(R) and p(t)}
 Where p is a formula in propositional calculus consisting of
terms connected by:  (and),  (or),  (not)
Result has the same attributes as the base relation.
Select Operation
Each term is one of:
<attribute> op <attribute>
<attribute> op <constant>

where op is one of: =, , >, <, , 


 An attribute with unordered domain allows only
the comparison operations =, .
Examples
1. List all employees working in department No. 4.
DNO=4(EMPLOYEE)
2. List all employees getting salary more than 3000
SALARY>3000(EMPLOYEE)
3. List all employees who are working in department 4 and getting salary
more than 25000 or work in department 5 and make over 30000.
(DNO=4 AND SALARY>25000) OR (DNO=5 AND SALARY>30000) (EMPLOYEE)
Project Operation
Works on a single relation R and defines a relation that
contains a vertical subset of R, extracting the values of
specified attributes and eliminating duplicates.
Notation: A1, A2, …, Ak (R)
where A1, A2 ,... are attribute names and R is a relation
name.
The result is defined as the relation of k columns
obtained by erasing the columns that are not listed.
Duplicate rows removed from result, since relations
are sets.
Example
List FNAME, LNAME and SALARY of all employees

FNAME, FNAME, SALARY (EMPLOYEE)


Assignment Operation
The assignment operation () provides a convenient
way to express complex queries, write query as a
sequential program consisting of a series of
assignments followed by an expression whose value is
displayed as a result of the query.
Assignment must always be made to a temporary
relation variable.
Example:

TEMPFNAME, FNAME, SALARY (EMPLOYEE)
Rename Operation
Allows us to rename, and therefore to refer to, the
results of relational-algebra expressions.
Allows us to refer to a relation by more than one
name.
It can be used to rename either the relation name, or
the attribute names, or both.
Example

TEMP(FIRSTNAME, LASTNAME, SALARY) FNAME, FNAME, SALARY (EMPLOYEE)


Sequence of Operations
If we want to apply more than one relation operator
one of the others, we can write them as a single
relation algebra expression by nesting operations, or
we can apply one operation at a time and create an
intermediate result relation.
Example
Example
List FNAME, LNAME and SALARY of all those
employees who work in department 5.

FNAME, FNAME, SALARY (DNO=5(EMPLOYEE))
Example
Alternatively, we can write a sequence of operations
as:

TEMP  DNO=5(EMPLOYEE)

R(FIRSTNAME, LASTNAME, SALARY)  FNAME, FNAME, SALARY (TEMP)


Union Operation
Union of two relations R and S defines a relation that
contains all the tuples of R or S, or both R and S, duplicate
tuples being eliminated.
Notation: R  S
Defined as:

R  S = {t | t  r(R) or t  s(S)}
For R  S to be valid:
 R, S must have the same arity (same number of attributes)
 The attribute domains must be compatible (e.g., 2nd column of R
deals with the same type of values as does the 2nd column of S etc.)
Example
List SSN of all employees who either work in
department 5 or directly supervise an employee in
department 5.
TEMP  DNO=5(EMPLOYEE)
RESULT1  SSN(TEMP)
RESULT2  SUPERSSN(TEMP)
RESULT  RESULT1  RESULT2
Difference Operator
Notation: R – S
Defined as:

R – S = {t | t  r(R) and t  s(S)}

Set differences must be taken between


compatible relations.
R and S must have the same arity
Attribute domains of R and S must be compatible
Example
List all those ESSN who work on PNO 1 and not on
PNO 2.

TEMP1  (PNO=1)(WORKS_ON)
RESULT1  SSN(TEMP1)
TEMP2  (PNO=2)(WORKS_ON)
RESULT2  SSN(TEMP2)
RESULT  RESULT1 – RESULT2
Set Intersection Operation
Defines a relation consisting of the set of all
tuples that are in both R and S.
Notation: r  s
Defined as:
R  s = {t | t  r and t  s}
Assumes:
R and S have the same arity
attributes of R and S are compatible
Note: r  s = r - (r - s)
Example
List all those managers who have at least one
dependent

MGRS(SSN)  MGRSSN(DEPARTMENT)
EMPS_WITH_DEPS(SSN)  ESSN(DEPENDENT)
MGRS_WITH_DEPS  MGRS  EMP_WITH_DEPS
Cartesian Product Operation
Defines a relation that is the concatenation of every
tuple of relation R with every tuple of relation S.
Notation: r  s
Defined as:

r x s = {t q | t  r and q  s}
Assume that attribute names of r(R) and s(S) are
disjoint.
If attributes of r(R) and s(S) are not disjoint, then
renaming must be used
Example
R S
StID Name Address Sec_no C_Code
980001 Ali Ma 1 ITCS385
980002 Ahmed Mu 2 ITCS100
3 ITCs101

RxS

StID Name Address Sec_no C_Code


980001 Ali Ma 1 ITCS385
980001 Ali Ma 2 ITCS100
980001 Ali Ma 3 ITCs101
980002 Ahmed Mu 1 ITCS385
980002 Ahmed Mu 2 ITCS100
980002 Ahmed Mu 3 ITCs101
Example
Retrieve for each female employee a list of the names of
her dependents.

FEMALE_EMP  SEX=’F’ (EMPLOYEE)
EMPNAMES  FNAME, LNAME, SSN(FEMALE_EMP)
EMP_DEPENDENTS  EMPNAMES x DEPENDENT
ACTUAL_DEPENDENTS  SSN=ESSN(EMP_DEPENDENTS)
RESULTFNAME,LNAME,DEPENDENT-NAME(ACTUAL_DEPENDENTS)
Join Operation
Notation: r s
Used to combine related tuples from two relations into
single tuples.
General form of join (also called theta join) is:
r <join condition> s

If R(A1,A2,…,An) and S(B1,B2,…,Bm), Then result of a join
is relation Q(A1,A2,…,An,B1,B2,…,Bm) – Q has one tuple
for each combination of tuples – one from r and one from
s, whenever the combination condition is satisfied.
Join Operation
Result  r A1=B1 s
Can be written as:

Temp  r  s
Result A1=B1(Temp)
Can be replaced with a single JOIN operation

ACTUAL_DEPENDENTS EMPNAMES SSN=ESSN DEPENDENT
Equijoin and Natural Join
Equijoin
If the condition in the join operator is only equality, it
is called, equijoin.
Natural Join
Natural join is basically an equijoin followed by
removal of the superfluous attributes.
The two tables must have at least one common
attribute.
Example
Retrieve the name and address of all employees who
work for the ‘Research’ department.

Modification of the Database
The content of the database may be modified
using the following operations:
Deletion
Insertion
Updating
All these operations are expressed using the
assignment operator.
Deletion
A delete request is expressed similarly to a query,
except instead of displaying tuples to the user, the
selected tuples are removed from the database.
We can delete only whole tuples; cannot delete
values on only particular attributes
A deletion is expressed in relational algebra by:
rr–E
where r is a relation and E is a relational algebra
query.
Examples
Example 1: Delete all account records in the
Perryridge branch.

account  account –  branch-name = “Perryridge” (account)

Example 2: Delete all loan records with amount in the
range of 0 to 50

loan  loan –  amount  0 and amount  50 (loan)


Insertion
To insert data into a relation, we either:
 specify a tuple to be inserted
 write a query whose result is a set of tuples to be inserted
In relational algebra, an insertion is expressed by:

rrE

 where r is a relation and E is a relational algebra expression.


 The insertion of a single tuple is expressed by letting E be a
constant relation containing one tuple.
Examples
Insert information in the database specifying that
Smith has $1200 in account A-973 at the Perryridge
branch.

account  account  {(“Perryridge”, A-973, 1200)}
depositor  depositor  {(“Smith”, A-973)}
Updating
A mechanism to change a value in a tuple without
charging all values in the tuple
Use the generalized projection operator to do this task

r F1, F2, …, Fi (r)
 Each F, is either the ith attribute of r, if the ith attribute is
not updated, or, if the attribute is to be updated
Fi is an expression, involving only constants and the
attributes of r, which gives the new value for the attribute
Example
Make interest payments by increasing all balances by
5 percent.

account   AN, BN, BAL * 1.05 (account)

where AN, BN and BAL stand for account-number,


branch-name and balance, respectively.
More Examples
nr name salary

1 John 100

5 Sarah 300

7 Tom 100

SQL Result Relational algebra

salary
select salary
100
salary(E)
from E
300

nr salary

select nr, salary 1 100


nr, salary(E)
from E 5 300

7 100
E
nr name salary
1 John 100
5 Sarah 300
7 Tom 100

SQL Result Relational algebra

select *
nr name salary
from E 1 John 100 salary < 200(E)
where salary < 200
7 Tom 100
select *
from E nr name salary
where salary < 200 7 Tom 100 salary < 200 and nr >= 7 (E)
and nr >= 7
E
nr name salary
1 John 100
5 Sarah 300
7 Tom 100

SQL Result Relational algebra

name, salary ( salary < 200(E))


select name, salary name salary
from E John 100 or, step by step, using an intermediate result
where salary < 200 Temp <- salary < 200(E)
Tom 100
Result <- name, salary(Temp)
E
nr name salary
1 John 100
5 Sarah 300
7 Tom 100

D
dnr dname
A Marketing
B Sales
C Legal

SQL Result Relational algebra


enr ename dept dnr dname
dept = dnr (E X D)
select * 1 Bill A A Marketing
from E, D
where dept = dnr 2 Sarah C C Legal or, using the equivalent join operation
3 John A A Marketing E dept = dnr D

You might also like