217 views

Uploaded by Minh_Tran_7326

- 01Chapter_DBMS
- Oracle Ilearning
- Database Objective Type 96 Questions
- Sym Case
- SQL Examples
- tybca90_1
- Chapter 05
- Database Vocabulary
- Online Mat
- m3 Relational Model Part1
- Business Artifact Modeling a Framework for Business Artifacts in Traditional Database Systems
- Ug Rptstd Fin
- DB Exam
- IT
- Guida Ufficiale Cloud Ant
- What is Information Syste1
- Lec01 Intro Final
- SAP IQ1
- Online Analytical Processing.ppt
- Aarsh RDBMS Oracle Basic Concept

You are on page 1of 40

Silberschatz Text: 6

What are Relational Algebra, Relational Calculus?

Relational Algebra

– A basic set of operations to manipulate the database

– Procedural, specify how to retrieve, incorporated in SQL

– Provides formal foundation for relational model operations

– Used as a basis for implementing and optimizing queries in

RDBMS

Relational Calculus

– A higher-level declarative notation for specifying queries

– Non-procedural, specify only what is to be retrieved

– Firm basis in mathematical logic

– Tuple Calculus: operates over the rows

– Domain Calculus: operates over the columns

R.Algebra Slide -2

Topics to cover

Basic Relational Algebra Operations

– Set (∪, ∩, –, X)

– Specific for relational database (σ, π, ⋈ , ρ, ÷)

– Unary (single relation) and Binary (2 relations)

Additional Relational Algebra Operations

– Aggregate Functions and Grouping ℑ

– Recursive Closure Operations

– OUTER JOIN Operations ]><[

– The OUTER UNION Operation

Tuple Relational Calculus

– SQL based on Relational calculus

Domain Relational Calculus

– QBE based on Domain Calculus

R.Algebra Slide -3

Relational Algebra Operations

1. SET OPERATIONS

UNION ∪, INTERSECTION ∩

SET DIFFERENCE (MINUS) –

CARTESIAN (CROSS JOIN) PRODUCT X

2. SPECIFIC OPERATIONS

SELECT σ, PROJECT π (unary operations)

JOIN ⋈ (various types, =, θ, *,) (binary operations)

OTHERS

Sequence, rename ρ (unary operations)

Division ÷ (binary operations)

R.Algebra Slide -4

SELECT OPERATION

UNARY implies it is applied to a single relation and to each tuple individually

Select a subset of tuples from a relation that satisfy a selection condition

σ(selection condition)(R)

σ sigma = SELECT operation

selection condition = Boolean expression

<attribute name> <comparison op> <constant value>, or

<attribute name> <comparison op> <attribute name>

<comparison op> = {=,<, ≤,>, ≥, ≠}

<constant value> = constant value from the attribute domain

Can connect clauses by AND, OR and NOT

The degree of the resulting SELECT operation is same as the degree of R

Number of resulting tuples <= number of tuples in R

Commutative, i.e, apply in any order: σ(cond1) ( σ(cond2)(R)) = σ(cond2) ( σ(cond1)(R))

Can combine a cascade of SELECT operations into a single SELECT operation

with a conjunctive (AND) condition

σ<cond1> ( σ<cond2> ( ..(σ<condn>(R))…)) = σ(cond1) AND <cond2> AND … AND <condn>(R)

R.Algebra Slide -5

PROJECT OPERATION

UNARY implies it is applied to a single relation and to each tuple individually

Select certain columns (attributes) from the table and discards the other columns

π <attribute list>(R)

π i = PROJECT operation

e.g. π LNAME, FNAME, SALARY (EMPLOYEE)

Project (choose) only attributes specified in <attribute list> in same order as they

appear

Duplicate elimination, remove duplicate non-key only attributes of R

Number of resulting tuples <= number of tuples in R

If projection list is a superkey of R, the resulting relation has the same number of

tuples as R

If <list 2> contains attributes in <list1>: π <list1> (π <list2> (R)) = π <list1> (R)

Commutative does not hold on PROJECT operation

R.Algebra Slide -6

Results of SELECT and PROJECT operations.

Refer to COMPANY database schema

SELECT

(a): σ(DNO=4 AND SALARY>25000) OR (DNO=5 AND SLARY>30000)(EMPLOYEE).

PROJECT

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

(c): πSEX, SALARY(EMPLOYEE).

appearing instead of 2

Fig 1

R.Algebra Slide -7

SEQUENCES OF OPERATIONS

expression:

– e.g. 2a: π FNAME, LNAME, SALARY (σ DNO=5 (EMPLOYEE))

create intermediate result relations, giving a name to each

intermediate relation:

– RESULT ← π FNAME, LNAME, SALARY (DEP5_EMPS )

R.Algebra Slide -8

Results of a sequence of operations.

(a) πFNAME, LNAME, SALARY(σDNO=5(EMPLOYEE)).

(b) Using intermediate relations and renaming of attributes.

Fig 2

R.Algebra Slide -9

RENAME OPERATIONS

● Sometimes the attributes in the intermediate result relations are

renamed

– TEMP ← σ DNO=5 (EMPLOYEE)

– ρS(FIRSTNAME, LASTNAME, SALARY) ← π FNAME, LNAME, SLARAY (TEMP )

● RENAME Operation

– ρ (rho)

– S is new relation name

– B1, B2, …, Bn are new attribute names

– ρS(B1, B2, …, Bn)(R) rename both relation and attribute

– ρS(R) renames relation only

– ρ(B1, B2, …, Bn)(R) renames attributes only

R.Algebra Slide -10

SET OPERATIONS

– R ∪ S, result in new relation that includes all tuples that

are either in R or in S or in both, duplicate tuples are

eliminated

– R ∩ S, result in new relation that includes all tuples that

are in both R and S

– R - S, result in new relation that includes all tuples that

are in R but not in S

Results of the UNION operation

Example: to retrieve SSN of all employees who either work in department 5

or directly supervise an employee who works in department 5

– RESULT 1: SSN of all employees who work in department 5

– RESULT 2: SSN of all supervisors in department 5

– RESULT: UNION Operation - SSN either in RESULT 1 or RESULT 2

R.Algebra Slide -12

SET OPERATIONS

– same degree n for R(A1, A2, …, An) and S(B1, B2, …, Bn)

– dom (Ai) = dom(Bi)

Commutative operations:

– R ∪ S = S ∪ R and R ∩ S = S ∩ R

Associative operations:

– R ∪ ( S ∪ T) = (R ∪ S) ∪ T

SET operations UNION, INTERSECTION, MINUS

(b) STUDENT ∪ INSTRUCTOR.

(c) STUDENT ∩ INSTRUCTOR.

(d) STUDENT – INSTRUCTOR.

(e) INSTRUCTOR – STUDENT

Fig 4

R.Algebra Slide -14

SET Operation:

The CARTESIAN PRODUCT (CROSS JOIN)

CROSS JOIN = X

R(A1, A2, …, An) X S(B1, B2, …, Bm),

B2, …, Bm), in that order

R X S will have nr x nS tuples

Fig 5a

R.Algebra Slide -15

SET Operation:

The CARTESIAN PRODUCT (CROSS JOIN)

Example: to retrieve a list of names of each female employees dependents

EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)

EMP_DEPENDENTS ← EMPNAMES X DEPENDENT

ACTUAL_DEPENDENTS ← σ SSN=ESSN (EMP_DEPENDENTS)

RESULT ← π FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPENDENTS )

Fig 5a

R.Algebra Slide -16

SET Operation: Cross JOIN

EMP_DEPENDENTS ← EMPNAMES X DEPENDENT

included in the result Fig 5b

R.Algebra Slide -17

SET Operation:

SELECT tuple(s) and PROJECT desired attributes from the

CROSS JOIN result EMP_DEPENDENTS

ACTUAL_DEPENDENTS ← σ SSN=ESSN (EMP_DEPENDENTS)

RESULT ← π FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPENDENTS )

Wallace, has a dependent whose name is Abner.

Fig 5c

R.Algebra Slide -18

BINARY JOIN OPERATIONS – INNER JOINS

Only matching tuples of inner joins ⋈ are kept in result relation

THETAJOIN θ, ⋈ θ

– R(A1, A2, …, An) JOIN <join condition> S(B1, B2, …, Bm), result

= Q (A1,, An, B1,, Bm)

– Only combination of tuples that satisfies join condition

appears in the result set

– Each <join condition> is of the form Ai θ Bi, where Ai and Bi

have the same domain. θ (theta) is one of the comparison

operators {=,<, ≤,>, ≥, ≠}

EQUIJOIN

– Similar to theta-join but only comparison operator used in the

<join condition> is =

– Join result carries superfluous attributes

R.Algebra Slide -19

BINARY JOIN OPERATIONS – INNER JOINS

NATURALJOIN *

– similar to EQUIJOIN, but superfluous attributes are removed

– Both join attributes must have same name, if not, apply

renaming operation first

– Q ← R * (<List1>), (<List2>) S

tables,

– e.g. 3 way join:

– (( PROJECT JOIN DNUMB=DNUMBER DEPARTMENT)) JOIN

MGRSSN=SSN EMPLOYEE

BINARY THETAJOIN θ :

JOIN result is the combine related tuples from 2 relations into single tuples

DEPT_MGR ← DEPARTMENT JOIN ⋈ MGRSSN=SSN EMPLOYEE .

EMP_DEPENDENTS ← EMPNAMES X DEPENDENT

ACTUAL_DEPENDENTS ← σ SSN=ESSN (EMP_DEPENDENTS)

ACTUAL_DEPENDENTS ← EMPNAMES JOIN ⋈ SSN=ESSN DEPENDENT

Fig 6

R.Algebra Slide -21

Results of two NATURAL JOIN * operations.

(a) RENAME attributes before doing JOIN

DEPT ← ρ(DNAME,DNUM,MGRSSN,MGRSTARTDATE)(DEPARTMENT)

(b) DEPT_LOCS ← DEPARTMENT * DEPT_LOCATIONS.

Join attribute

Fig 7

R.Algebra Slide -22

The DIVISION ÷ operation

R(Z) ÷ S(X), where X ⊆ Z, Y=Z-X (i.e. set of attributes of R that are not attributes of S)

T1 ← π Y(R)

T2 ← π Y((S X T1) – R)

T ← T1 – T2

SQL does not implement DIVISION directly, it has a round about way to

dealing with it.

Fig 8b

R.Algebra Slide -23

The DIVISION ÷ operation

Example Fig 8a: Retrieve the names of employees who work on all the projects

that “John Smith” works on:

Step 1: retrieve list of PNO that John Smith works on

SMITH ← σ FNAME=‘JOHN’ AND LANME=‘SMITH’(EMPLOYEE)

SMITH_PNOS ← π PNO (WORKS_ON JOIN ⋈ ESSN=SSN SMITH)

Step 2: Create a relation to include a tuple <PNO, ESSN>

SSN_PNOS ← π ESSN, PNO (WORKS_ON)

Step 3: Divide SSN_PNOS by SMITH_PNOS to get the desired SSNS

SSNS (SSN) ← SSN_PNOS ÷ SMITH_PNOS

RESULT ← π ENAME, LNAME (SSNS * EMPLOYEE)

T←R÷S

Fig 8

R.Algebra Slide -24

Additional Relational Operations

Some common database requests that cannot be performed with

basic algebra operations (in the previous list) would require the

following additional Relational algebra operations:

Recursive Closure Operations

OUTER JOIN Operations

The OUTER UNION Operation

AGGREGATE FUNCTION / GROUPING operation

where:

grouping attributes: list of attributes of relation R

function list: list of <function> such as SUM,

AVERAGE, MAXIUM, MINIUM, COUNT and

<attributes> pairs

The AGGREGATE FUNCTION operation.

Example: fig 9a,retrieve each department number, number of employees in the

department and their average salary: specify List of attributes names with rename

of resulting relation will each be the concatenation

of the function name with the attribute name

SALARY(EMPLOYEE)

specified, functions applied to all tuples

Fig 9

R.Algebra Slide -27

Recursive Closure Operations

Query:

Specify the SSNs of all employees

supervised by employee whose name is

James Borg. (i.e. Borg’s division)

SUPERVISION (SSN1, SSN2) ← π SSN, SUPERSSN (EMPLOYEE)

RESULT1(SSN) ← π SSN1(SUPERVISION JOIN ⋈ SSN2=SSN BORG_SSN )

Fig 10

R.Algebra Slide -28

LEFT, RIGHT, FULL OUTER JOINS

LEFT OUTER JOIN

– keep every tuple in the first, or left, relation R in R ]><| S, if

no matching tuple is found in S, then attributes of S in the join

result are filled or “padded” with null values

– keep every tuple in the second, or right, relation R in R |><[ S

– keep ALL tuples in both the left and right relations R ]><[ S

when no matching tuples are found, padding them with null

values as needed.

The result of a LEFT OUTER JOIN operation.

Example: Retrieve a list of employee names and also the name of the departments

they manage if they happen to manage a department, indicate by null for those not

managing a department. In example here, a Left Outer JOIN keeps every tuple in the

‘left’ relation, ie. EMPLOYEE. There are Right Outer JOIN and Full Outer JOIN.

Fig 11

R.Algebra Slide -30

OUTER UNION operation

UNION of tuples from 2 relations with partially UNION compatible attributes,

e.g. R(X, Y) and S (X, Z)

UNION compatible attributes are represented once in the result, others are kept in

the result relation T (X, Y, Z)

Example:

Apply OUTER UNION to 2 relations STUDENT (Name, SSN, Department,

Advisor) and INSTRUCTOR(Name, SSN, Department, Rank).

Result relation:

STUDENT_OR_INSTRUCTOR (Name, SSN, Department, Advisor, Rank)

Tuples with same (Name, SSN, Department) will appear once only

Tuples only in STUDENT will have a null for the Rank attribute

Tuples only in INSTRUCTOR will have a null for the Advisor attribute

What is Relational Calculus?

Provide higher-level declarative notation for specifying

relational queries.

– e.g. form new relations that is specified in terms of variables over

rows (tuples) and columns (domains)

– Based on predicate calculus

– No order of operations optimize queries to form new relations

(non-procedural language)

– SQL based on Relational calculus

– QBE base on Domain Calculus

What is Tuple Relational Calculus?

Only one declarative expression to specify a retrieval request,

emphasis on what is to be retrieved, not how to retrieve

query

any retrieval in basic relational algebra can be expressed in

relational calculus

connected via the logical operator AND, OR, and NOT

Tuple Relational Calculus

Tuple Variables and Range Relations

Expressions, Formulas

Existential and Universal Quantifiers

– Queries examples using Ex-Quantifier

Transforming Universal and Existential

Quantifiers

Using the Universal Quantifier

Safe Expressions

R.Algebra Slide -34

Tuple Variables and Range Relations

Example of a simple tuple relational calculus query

– {t | COND(t)}

where t is a tuple variable

COND(t) is a conditional expression involving t

result = all tuples of t that satisfy COND(t)

{t | EMPLOYEE(t) and t.SALARY>50000}

{t.FANME, t.LNAME | EMPLOYEE(t) and t.SALARY>50000}

{t.BDATE, t.ADDRESS | EMPLOYEE(t) AND t.FNAME=‘John’ AND

t.MINIT=‘B’ AND t.LNAME=‘Smith’}

Expressions and Formulas

General expression:

– {t1.Aj, t2.Ak, …, tn.Am, | COND(t1, t2, …., tn, tn+1, tn+2, …., tn+m )}

where t1, t2, …., tn, tn+1, tn+2, …., tn+m are tuple variables

atoms below evaluates to either TRUE or FALSE. Atoms are

connected via the logical operators AND, OR, NOT.

– R(ti )

where R is relation name and ti is a tuple variable

– t1.A op tj.B

where op is one of the comparison operators in {=,<, ≤,>, ≥, ≠}

A is an attribute of the relation on which t1 ranges

B is an attribute of the relation on which tj ranges

– t1.A op C or C op tj.B

where C is a constant value

R.Algebra Slide -36

Existential and Universal Quantifiers

1. Every atom is a formula

– F1 AND F2 is TRUE if both F1 and F2 are TRUE, otherwise , it is FALSE

– F1 OR F2 is FALSE if both F1 and F2 are TRUE, otherwise, it is TRUE

– NOT (F1) is TRUE if F1 is FALSE; it is FALSE if F1 is TRUE

– NOT (F2) is TRUE if F2 is FALSE; it is FALSE if F2 is TRUE

– The formula is TRUE if (F) evaluates to TRUE for some (at least one) tuple

assigned to free occurrences of t in F

– The formula is TRUE if (F) evaluates to TRUE for every (in the universe)

tuple assigned to free occurrences of t in F

Existential and Universal Quantifiers

Tuple variable t is bound if it is quantified, otherwise it is free

Can transform one type of quantifier into the other with NOT; AND, OR.

Example:

– Query 1

Retrieve name, address of all employees work for department “Research”

Q1: {t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) AND (∃ d)

(DEPARTMENT(d) AND d.NAME = ‘Research’ AND d.NUMBER=t.DNO)}

– t is the only free variable (on left of | ) where d is bounded by the existential

quantifier.

– Conditions EMPLOYEE (t) and DEPARTMENT (d) specify range relations

for t and d.

– SELECT condition is d.DNAME =‘Research”

– JOIN condition is d.NUMBER=t.DNO

What is Domain Relational Calculus?

Rather than variable range over tuples, such as the case of

Tuple Calculus, Domain Calculus uses variables range

over single values from the domains of attributes

domain relational calculus.

General expression:

– {x1, x2,,…, xn, | COND(x1, x2,,…, xn,xn+1,xn+2, …., xn+m )}

where x1, x2,,…, xn,xn+1,xn+2, …., xn+m are domain

variables that range over domains (of attributes) and

COND is a condition or formula of the domain

relational calculus

Domain Relational Calculus Query Example

Query 1

– Retrieve name, address of all employees work for department “Research”

– Q1: {qsv | (∃ Z) (∃ l) (∃ m) (EMPLOYEE(qrstuvwxyz) AND

DEPARTMENT (lmno) AND l=‘RESEARCH’ AND m=z}

variables: q=FNAME, s=LNAME, v=ADDRESS where qrstuvwxyz are

the 10 variables for the EMPLOYEE relation, one to range over the

domain of each attribute in order, and lmno are the 4 variables for the

DEPARTMENT relation

– m=z is a join condition (joining on DNUMBER and DNO)

– l=‘research’ is a selection condition relates a domain variable to a

constant, in this case DNAME=“RESEARCH”

– the existential qualifiers (∃ Z) (∃ l) (∃ m) will provide the tuples that will

hold the condition of this formula to be TRUE

- 01Chapter_DBMSUploaded byshivanshubansal
- Oracle IlearningUploaded byriskis
- Database Objective Type 96 QuestionsUploaded byMohana Ramasamy
- Sym CaseUploaded byrajesh_34
- SQL ExamplesUploaded bygubeguru
- tybca90_1Uploaded byVikas Tadvi
- Chapter 05Uploaded byDjan Lang
- Database VocabularyUploaded byDarin Brooks
- Online MatUploaded byanshika_dhingra
- m3 Relational Model Part1Uploaded byAditya Sapate
- Business Artifact Modeling a Framework for Business Artifacts in Traditional Database SystemsUploaded byAnonymous 4BsMjh
- Ug Rptstd FinUploaded byKumar Satti
- DB ExamUploaded byBrian Makala
- ITUploaded byDong-Kyun Ryu
- Guida Ufficiale Cloud AntUploaded byers1st
- What is Information Syste1Uploaded bychssg
- Lec01 Intro FinalUploaded byDavid John
- SAP IQ1Uploaded byJagadeep Reddy Bhumireddy
- Online Analytical Processing.pptUploaded byPrachi Patel
- Aarsh RDBMS Oracle Basic ConceptUploaded byAARSH
- rdbmsUploaded byAfeefa
- 3rd Sem SyllabusUploaded byKrishnan Marimuthu
- crime.docxUploaded bySarvesh Kumar
- DBMS Package For Class ProjectUploaded bySushil Saini
- dcUploaded byNagarjun Dasari
- Key PrimaryUploaded byMegha Kashyap
- Head First Javascript.22Uploaded bymannie
- OracleSQLTuning-1Uploaded bymaity.20053667
- 10 Steps to a Successful InterviewUploaded bySreevatsanadig
- New Microsoft Word Document (2)Uploaded byrsreddy434

- Network Database Model.docxUploaded byArfaqammar
- Algebra Questions and AnswersUploaded byGer Cahill
- Math 3Uploaded byhassan_m2222
- Rdbms Versus Ordbms Versus OodbmsUploaded byGaurav
- Math 3Uploaded byhassan_m2222
- Chapter 3Uploaded bySeham123123
- Dbms AssignmentUploaded byAkshada Kaphale
- queryUploaded byMoses Kabete
- PBD03Uploaded byRetno Aulia Vinarti
- Assignment -IUploaded byjyothibellary2754
- A254053496_23413_25_2018_Lecture 3 DBMS Database Models.pptUploaded bySreedhar Kumargoud
- Elm04_05Uploaded byburmansoft
- Elmasri 6e_ISM 07Uploaded byfizixme123
- Relational Algebra ExanplesUploaded byhweoo
- Formal Relational QueryUploaded bySaarthak Vadhera
- DBMS Unitwise Important QuestionsUploaded bySantosh Kumar Marumamula
- Sol Practice1Uploaded byNitin Ainani
- Relational Database Design by ER- and EER-to-Relational MappingUploaded byAhmad Shdifat
- ch2Uploaded byIshan Jain
- rdbmstestUploaded bySalomy
- Chapter (4)Uploaded byodai1gor
- algebraanswersUploaded byAmit Kumar
- Cis611 Lecture Notes AlgebraUploaded byLuthweedblow
- Lecture 2dfavfbagfbvUploaded bySahan Harinda Nagodawithana
- 03_RAlgebra.pdfUploaded byOm Prakash Mahato
- Query Processing ExerciseUploaded bySeptian D. Indradi
- Dr.osama Lecture 11 RevisionUploaded bychodar
- MCS-023 Solved Assignment 2013-14Uploaded byBabji Babu
- INFT 2301 - Chapter 3Uploaded byHuseyn Alkaramov
- ENCh06(2)Uploaded byJyoti Gupta