26 views

Uploaded by philip_arthur_1

- Understanding SQL Joins
- Relational Calculus
- Chap8-SQL-99 Schema Definition, Basic Constraints, and Queries
- A Structured Approach to SQL Query Design
- DB Systems - Data Modeling
- DBMS
- Ralated Terms of DBMS
- The Advantages and Disadvantages of Data Model
- Joins
- functional dependency
- 35610302 Teradata Joins Types
- CS205_Syllabus_Fall11
- TD_Join
- 5 Web
- Introduction of different Informatica Transformation
- Chapter 12
- KorthDB6_ch1
- Relation Model
- Syllabus
- DBMS

You are on page 1of 62

Algebra

Outline (1)

Introduction

Select Operation

Project Operation

Sequence of Operation and Rename Operation

Union Operation

Intersection Operation

Minus Operation

Cartesian Product

Outline (2)

Binary Relational Operations

Join Operation

Variation of Join: Equijoin Operation

Variation of Join: Natural Join Operation

A Complete Set of Relational Algebra

Division Operation

Query Tree

Generalized Projection

Aggregate Functions and Grouping

Recursive Closure Operations

Outer Join Operation

Outer Union Operation

Introduction (1)

manipulate the database (aside from defining database

structure and constraints)

relational model.

These operations specify basic retrieval requests.

The result is a new relation

relational algebra expression.

The result is also a relation that represents the result of

database query (or retrieval request)

Introduction (2)

Provides a formal foundation for relational model

operations.

Used as a basis for implementing and optimizing queries

in RDBMS.

Some of its concepts are incorporated into the SQL

standar query language for RDBMSs

part of the relational data model.

Introduction (3)

groups:

Group one includes set operations from mathematical

set theory (UNION, INTERSECTION, SET,

DIFFERENCE, and CARTESIAN PRODUCT)

Group two consists of operations developed specifically

for relational databases (SELECT, PROJECT, and

JOIN)

algebra because there are some common database

requests that cannot be performed with the original one.

(e.g., aggregate functions: operations that summarize

data from tables)

algebra due to their importance to many database

applications.

Unary Relational Operation

[SELECT Operation] (1)

operation operates on single relations

relation that satisfies a certain selection condition.

It filters the tuples and keeps only tuples that satisfy the

selection condition.

row1

row2

…

rowN

Unary Relational Operation

[SELECT Operation] (2)

The σ (sigma) symbol is to denote the SELECT operation.

The <selection condition> is a Boolean expression

specified on the attributes of relation R.

The Boolean expression inside the <selection condition>

is made up of a number of clauses of the form:

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

or

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

Unary Relational Operation

[SELECT Operation] (3)

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

The <constant value> is a constant value from the attribute

domain.

such as AND, OR, and NOT.

(cond1 AND cond2) is TRUE if both (cond1) and (cond1)

are TRUE; otherwise, it is FALSE.

(cond1 OR cond2) is TRUE if either (cond1) or (cond2) or

both are TRUE; otherwise, it is FALSE.

(NOT cond) is TRUE if cond is FALSE; otherwise, it is

FALSE.

Unary Relational Operation

[SELECT Operation] (4)

σDno=4(EMPLOYEE) ……..(ex.01)

In ex.01, we want to select (σ) from a EMPLOYEE relation

with a selection condition of “Dno=4”.

The attribute name is “Dno” ;

The comparison op is “=“ and;

The constant value is “4”.

Unary Relational Operation

[SELECT Operation] (5)

Salary>30000)(EMPLOYEE) ……..(ex.02)

with a selection condition of “(Dno=4 AND Salary>25000)

OR (Dno=5 AND Salary>30000)”.

condition with these Boolean operators: AND and OR.

Salary>25000) OR (Dno=5 AND Salary>30000);

Unary Relational Operation

[SELECT Operation] (5)

The attribute name are “Dno” and “Salary” ;

The comparison op is “=“ and “>” ;

The constant value is “4” and “25000”.

The attribute name are “Dno” and “Salary” ;

The comparison op is “=” and “>” ;

The constant value is “5” and “30000”.

Unary Relational Operation

[SELECT Operation] (6)

{=,<,≤,>,≥,≠} only apply to domains of ordered values,

such as numeric or date domains.

operators that can be apply is the set of {=,≠}. Example of

unordered values domain is the domain of Color = {„red‟,

„blue‟, „green‟, „white‟, „yellow‟, …}.

operators, such as in domain of character string may allow

the comparison operator SUBSTRING_OF.

Unary Relational Operation

[SELECT Operation] (7)

Operation (its number of attributes) is the same as the

degree of R because SELECT Operation is a horizontal

partition.

less than or equal to the number of tuples in R.

The nature of SELECT Operation is commutative:

σ<cond1>(σ<cond2>(R)) = σ<cond2>(σ<cond1>(R))

Unary Relational Operation

[SELECT Operation] (8)

applied in any order and any cascade of SELECT

operations can then be combine into a single SELECT

operations with conjunctive (AND) condition.

AND … ANDσ<cond2>(R)

Unary Relational Operation

[PROJECT Operation] (1)

table and discards other columns, unlike the SELECT

Operation that selects some rows and discard other rows.

relation.

row1

row2

…

rowN

Unary Relational Operation

[PROJECT Operation] (2)

π<attribute list>(R)

The pi (π) symbol is to denote the PROJECT Operation.

attributes of relation R.

specified in the <attribute list> and represented in the

same order as they appear in the list.

Unary Relational Operation

[PROJECT Operation] (3)

Π (EMPLOYEE)…(ex.03)

Lname, Fname, Salary

attributes from relation R.

Salary. These are the attributes that we want to project

(select)

employee;

Unary Relational Operation

[PROJECT Operation] (4)

number of attributes in <attribute list>.

tuples, which is known as duplicate elimination.

duplicates.

multiset or bag of tuples rather than a set. Multiset or

bag of tuples are not permitted in formal relation model

but permitted in practice.

Unary Relational Operation

[PROJECT Operation] (5)

Operation is always less than or equal to the number of

tuples in R.

resulting relation has the same number of tuples as R.

<list1>, otherwise the left-hand side is an incorrect

expression.

Unary Relational Operation

[Sequence of Operations and the RENAME

Operation] (1)

operations one after the other.

These can be achieved in a single relational algebra

expression by nesting the opeations.

πFname, Lname, Salary(σDno=5(EMPLOYEE))

or

Apply one operation at a time and create intermediate

result relations.

DEP5_EMPS σDno=5(EMPLOYEE)

RESULT πFname, Lname, Salary(DEP5_EMPS)

For the latter example, we must assign a name for each

operation.

Unary Relational Operation

[Sequence of Operations and the RENAME

Operation] (2)

We could also rename the attributes in the intermediate and

result relations

TEMP σDno=5(EMPLOYEE)

R(First_name, Last_name, Salary) πFname, Lname, Salary(TEMP)

complex operation, such as UNION and JOIN.

OR

ρS(R) …(ro.2)

OR

Unary Relational Operation

[Sequence of Operations and the RENAME

Operation] (3)

Operation.

attributes.

Relational Algebra Operations from Set

Theory

[UNION, INTERSECTION, MINUS Operations] (1)

mathematical operations on sets.

those operations must have the same type of tuples

(known as union compatibility).

compatible if they have the same degree n and if dom(Ai)

= dom(Bi) for 1 ≤ i ≤ n (same number of attributes and each

pair has the same domain).

Relational Algebra Operations from Set

Theory

[UNION, INTERSECTION, MINUS Operations] (2)

relations R and S:

Notation: R S

The result includes all tuples that are either in R or in S

or in both R and S. (Duplicates are eliminated).

compatible relations R and S:

Notation: R S

The result includes all tuples that are in both R and S.

Defintion of MINUS Operation on two union-compatible

relations R and S:

Notation: R − S

The result includes all tuples that are in R but not in S.

Relational Algebra Operations from Set

Theory

[UNION, INTERSECTION, MINUS Operations] (3)

commutative operations:

R S S R and R S S R

The nature of MINUS Operation is not commutative:

RS SR

INTERSECTION can

be expressed in the form of UNION

and MINUS:

R

S R S R S S R

Relational Algebra Operations from Set

Theory

[The CARTESIAN PRODUCT] (1)

Denoted by ×

have tobe union compatible.

member (tuple) from one relation (set) with every member

(tuple) from the other relation (set)

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

Q(A1,A1,…,An,B1,B2,…,Bm)

Relational Algebra Operations from Set

Theory

[The CARTESIAN PRODUCT] (2)

combination of tuples (one from R and one from S).

then RxS will have nR * nS tuples.

of the previous concept.

concatenating all possible combinations of tuples from n

underlying relations.

Relational Algebra Operations from Set

Theory

[The CARTESIAN PRODUCT] (3)

combined attributes of two relations.

specifying an appropriate selection condition.

quite common to identifed and select related tuples from two

relations. Thus, a special operation called JOIN was created to

specify previous sequence as a single operation.

Binary Relational Operations

[JOIN] (1)

JOIN operation denoted by ><

JOIN operation is used to combine related tuples from two

relations into single tuples. In database JOIN operation is

important because it allows us to process relationship among

relations.

Example of JOIN operation, retrieve name of manager of each

departement:

><

DEPT_MGR DEPARTEMENTMgr_ssn=SsnEMPLOYEE

RESULT πDname, Lname, Fname(DEPT_MGR)

Binary Relational Operations

[JOIN] (2)

The general form of JOIN operation on two relations

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

><

R<join condition>S

The result is a relation Q with n+m attributes Q(A1,A2,…An,

B1,B2,…,Bm) in that order. Q has one tuple for each combination

of tuples (one from R and one from S) – whenever the

satisfies the join condition.

combination

that JOIN only combines tuples that satisfy the join condition

unlike CARTESIAN PRODUCT that combines all tuples.

Binary Relational Operations

[JOIN] (3)

A general join condition is of the form:

<condition>AND<condition>AND…AND<condition>

is an attribute of S, Ai and Bj have the same domain, and

θ(theta) is one of the comparison operator {=,<,≤,>,≥,≠}.

This general join condition is called THETA JOIN. Tuples with join

attributes equal NULL do not appear in the result (does not

necessarily preserve all of the information in the participating

relations).

Binary Relational Operations

[VARIATION of JOIN: The EQUIJOIN] (1)

A JOIN operation that only use an equal / {=} comparison

operator.

always one or more pairs of attributes that have identical

values in every tuple. Hence, this is because of the equality join

condition specified on these attributes.

superfluous.

Binary Relational Operations

[VARIATION of JOIN: The NATURAL JOIN] (1)

To get rid of the second (superfluous) attribute found in

EQUIJOIN operation, we introduced the next variation of JOIN

operation called NATURAL JOIN.

Denoted by *.

join attributes) have the same name in both relations. If this is

not the case, a renaming operation is applied first.

Dnumber attribute but in PROJECT table use the name Dnum.

So we need to rename the operation before applying the

NATURAL JOIN operation.

Binary Relational Operations

[VARIATION of JOIN: The NATURAL JOIN] (2)

Example of NATURAL JOIN, DEPARTEMENT table has

Dnumber attribute but in PROJECT table use the name Dnum.

So we need to rename the operation before applying the

NATURAL JOIN operation.

PROJ_DEPT PROJECT *

ρ(Dname,Dnum,Mgr_ssn,Mgr_start_date)(DEPARTEMENT)

DEPT ρ(Dname,Dnum,Mgr_ssn,Mgr_start_date)(DEPARTEMENT)

would only one join attribute is kept.

Binary Relational Operations

[VARIATION of JOIN: The NATURAL JOIN] (3)

In general, NATURAL JOIN is performed by equating all

attribute pairs that have the same name in the two relations.

There can be a list of join attributes from each relation, and each

corresponding pair must have the same name.

JOIN:

Q R * (<list1>),(<list2>)S

from S. The lists are used to form equality comparison

conditions between pairs of corresponding attributes and the

conditions are then ANDed together. Only the list corresponding

to attributes of the first relation R is kept in the result Q.

Binary Relational Operations

[VARIATION of JOIN: The NATURAL JOIN] (4)

If no combination of tuples satisfies the join condition then the

result of JOIN operation is an empty relation with zero tuples.

condition>S will have between zero and nR * nS tuples.

><

Join selectivity, a property of each join condition, is a ratio of

the expected size of the join result divided by the maximum

size nR * nS tuples.

If no join condition and all combinations of tuples qualify then

the JOIN operation degenerates into a CARTESIAN

PRODUCT.

JOINS, a JOIN operation that combine data from multiple

relations so that related information can be presented in a single

table.

Binary Relational Operations

[VARIATION of JOIN: The NATURAL JOIN] (5)

NATURAL JOIN or EQUIJOIN operation can be applied among

multiple tables, leading to an n-way join.

PROJECT >< DEPARTEMENT ><

Dnum Dnumber EMPLOYEE

Mgr _ ssn Ssn

Binary Relational Operations

[A Complete Set of Relational Algebra

Operation] (1)

These are the complete set of relational algebra operation:

{, ,,,}

a sequence of operations from the original set.

INTERSECTION

MINUS operation, like so:

The JOIN operation that we previously discussed can be

expressed as a CARTESIAN PRODUCT followed by a SELECT

operation:

R

Binary Relational Operations

[The DIVISION Operation] (1)

The DIVISION operation is useful for a special kind of query that

sometimes occurs in database application.

Denoted by:

Example: retrieve the names of employees who work on all the

projects that „John Smith‟ works on.

First step get list of project numbers that „John Smith‟

works on:

Fname ' John' ANDLname ' Smith' EMPLOYEE

SMITH

SMITH

_ PNOS PnoWORKS _ON >< SMITH

Essn Ssn

Binary Relational Operations

[The DIVISION Operation] (2)

Second step create a relation that includes a tuple <Pno,

Essn> where Ssn = Essn and the project number is Pno:

Final step apply DIVISION operation to the two relations,

which gives result the employee‟s Social Security Numbers:

RESULT Fname, LnameSSNS EMPLOYEE

Binary Relational Operations

[The DIVISION Operation] (3)

Generally, DIVISION is applied to two relations R(Z) ÷ S(X),

where X Z

that are not attribute of S.

The result of DIVISION is a relation T(Y) that includes a tuple t if

tuples t appear in R with tR[Y] = t, and with tR[X] = tS for every

R

tuple tS in S.

t must appear in R in combination with every tuple in S.

relation by selecting those tuples in the result that match all

values present in the denominator.

Binary Relational Operations

[The DIVISION Operation] (4)

See picture below, X={A}, Y={B}, and Z={A,B}. Tuples (values)

b1 and b4 appear in R in combination with all three tuples in S,

thus they are the result of DIVISION.

Binary Relational Operations

[The DIVISION Operation] (5)

The DIVISION operation can be express as a sequence of π,

×, and − as follows:

T1 Y (R)

T2 Y S T1 R

T T1 T2

Notation for Query Trees (1)

Query Tree, or query evaluation tree, or query execution tree is

a notation that is used typically in relational systems to

represent queries internally.

it is used as a possible data structure for the internal

representation of the query in an RDBMS.

corresponds to a relational algebra expression. As leaf nodes of

the tree is the input relations and the relation algebra operations

is in the internal nodes.

project number, the controlling department number, and the

departement manager‟s last name, address, and birth date‟. The

relational algebra for this is:

Dnum Dnumber(DEPARTEMENT )) >< Mgr _ ssn Ssn(EMPLOYEE ))

><

Notation for Query Trees (2)

The query tree for the previous relational algebra is:

(3

><

) _ ssn E.Ssn

D.Mgr

(2

><

P.Dnum

) D.Dnumber EMPLOYE

E E

(1

P.Plocation

) ' Stafford' DEPARTEMENT

D

PROJEC

P T

Notation for Query Trees (3)

In general, a query tree gives a good visual representation and

understanding of the query in terms of the relational operations

it uses.

expressing queries in relational algebra.

Additional Relational Operations

application for RDBMSs – cannot be performed with the original

relational algebra operations that we previously learned.

these requests.

relational algebra.

Additional Relational Operations

[Generalized Projection] (1)

Extends the original projection operation which allows function

of attributes to be included in the projection list.

F , F ,...,F (R)

1 2 n

may involve constants.

where computer values have to be produced in columns.

Additional Relational Operations

[Generalized Projection] (2)

Consider this example, we have a relation:

Net Salary = Salary – Deduction,

Bonus = 2000 * Years_service, and

Tax = 0.25 * Salary

will give result like so:

(Ssn, Salary Deduction, 2000* Years_ service, 0.25 * Salary(EMPLOYEE ))

Additional Relational Operations

[Aggregate Functions and Grouping] (1)

Mathematical aggregate functions on collections of values

from the database cannot be specified by the original relational

algebra.

summarize information from the database tuples (SUM,

AVERAGE, MAXIMUM, and MINIMUM) and COUNT function

for counting tuples or values.

alegbra is:

where <grouping_attributes> is a list of attributes of the relation

specified in R, and <function_list> is a list of

(<function><attribute>) pairs. In each pair could be one of these

functions: SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT.

Additional Relational Operations

[Aggregate Functions and Grouping] (2)

The resulting relation has the grouping attributes plus one

attribute for each element in the function list.

employees in the departement, and their average salary:

R(Dno, No _ of _ employees, Average _ sal)(DnoCOUNTSsn, AVERAGESalary(EMPLOYEE ))

Additional Relational Operations

[Recursive Closure Operations] (1)

Recursive Closure is another operation that can be specified

using the original relational algebra.

type, such as the relationship between an employee and a

supervisor (this relationship described by foreign key Super_ssn

of the EMPLOYEE relation).

– at level one – by the employee e whose name is „James Borg‟:

SUPERVISION (Ssn1, Ssn2) Ssn, Super_ ssn(EMPLOYEE )

RESULT1(SSN)

Ssn1(SUPERVISION >< Ssn2 SsnBORG _ SSN)

Additional Relational Operations

[Recursive Closure Operations] (2)

Example: retrieve all employees supervised by Borg at level 2

(all employee e‟‟ supervised by some employee e‟ who is directly

supervised by Borg). Apply another JOIN to the result of the first

query:

RESULT2(Ssn)

Ssn1(SUPERVISION >< Ssn2 SsnRESULT1)

Example: to get both sets of employees supervised at levels 1

and 2 by „James Borg‟ apply UNION operation:

RESULT RESULT2 RESULT1

Additional Relational Operations

[Recursive Closure Operations] (3)

Additional Relational Operations

[OUTER JOIN Operations] (1)

On previous JOIN operations:

Tuples without a matching (or related) tuples are eliminated.

Tuples with NULL values in the join attributes are also

eliminated.

generate a report based on all the information in the component

relations then the loss of information would give an inaccurate

report.

which tuples from two tables are to be combined by matching

corresponding rows, but without losing any tuples for lack of

matching values.

Additional Relational Operations

[OUTER JOIN Operations] (2)

Example: list all employees names and also the name of the

departements they manage (if they happen to manage a

departement, if not then it would be indicated as NULL). Apply

LEFT OUTER JOIN which is denoted by : ><

TEMP (EMPLOYEE >< Ssn Mgr _ ssnDEPARTEMENT )

RESULT

Fname, Minit, Lname, Dname(TEMP)

LEFT OUTER JOIN keeps every tuple in the first or left

relation, if no matching tuple is found in the right side then

the attributes on the right side of the join are filled or padded

with NULL values.

Additional Relational Operations

[OUTER JOIN Operations] (3)

The RIGHT OUTER JOIN, denoted by ><

keeps every tuple

,

in the second or right side of relation.

The FULL OUTER JOIN, denoted by ><

, keeps all tuple in

both the left and the right side relations when no matching

tuples are found, padding them with NULL values as needed.

Additional Relational Operations

[OUTER UNION Operations] (1)

Developed to take the union of tuples from two relations if the

relations are not union compatible.

are partially compatible, meaning that only some of their

attributes, say X, are union compatible.

compatible are represented only once in the result, and those

which are not union compatible from either relation are also kept

in the result.

Two tuples t1 in R and t2 S are said match if t1[X] = t2[X], and are

considered to represent the same entity of relationship instance.

These will be combined into a single tuple in the result T. No

matching tuple in the other relation are padded with NULL.

Additional Relational Operations

[OUTER UNION Operations] (2)

Example: an OUTER JOIN can be applied to two relations

whose schemas are:

STUDENT(Name, Ssn, Departement, Advisor)

INSTRUCTOR(Name, Ssn, Departement, Rank)

into STUDENT_OR_INSTRUCTOR:

Advisor, Rank)

References

Systems, 5th edition

- Understanding SQL JoinsUploaded bydimpleguru
- Relational CalculusUploaded byshaily2011
- Chap8-SQL-99 Schema Definition, Basic Constraints, and QueriesUploaded bynomaddarcy
- A Structured Approach to SQL Query DesignUploaded byBrendan Furey
- DB Systems - Data ModelingUploaded byhoangtucuagio
- DBMSUploaded byValan Pradeep
- Ralated Terms of DBMSUploaded byHitesh Mohapatra
- The Advantages and Disadvantages of Data ModelUploaded byVijay Kumar
- JoinsUploaded byAdityaAgarwal
- functional dependencyUploaded bysmoethwtort
- 35610302 Teradata Joins TypesUploaded byPratik Lakhpatwala
- CS205_Syllabus_Fall11Uploaded byCharlesKessler
- TD_JoinUploaded byPriya Dash
- 5 WebUploaded bysansayana
- Introduction of different Informatica TransformationUploaded byAmit Rai
- Chapter 12Uploaded byasimasim488
- KorthDB6_ch1Uploaded byDiana Rodriguez
- Relation ModelUploaded bybaluchandrashekar2008
- SyllabusUploaded byNilesh Patil
- DBMSUploaded byVunnam Harish
- Final Mis DoneUploaded byAnuradha Ray
- 717.1 exam1Uploaded byMedSliman Arfaoui
- EAadhaar_552243806696_04032019124616_065982Uploaded byMohammad Rafiq Dar
- Join_SQLUploaded byVishal
- RDBMS_Day4Uploaded byHarika Manthena
- BD Aventure WorldsUploaded byVictor Ricardo Alcantara Esteves
- Enter Small Busi Map ConstUploaded bySainath Nutalapati
- ModelingRelationships_ORDBUploaded byPhước Lân Hà
- Assignment AnkushUploaded byEkampreet
- Ghani DBMS Relational_algebraUploaded byAbdul Ghani Khan

- CSE, IT, MCAUploaded byNithin Radhakrishnan
- pqt-anna-university-notes-rejinpaul.pdfUploaded byDavid Miller
- Field Data TypesUploaded bysavin3s2
- Microsoft Access Small Business SolutionsUploaded byCarlos Giacomazzi
- Gate Detailed SyllabusUploaded byNag Raju
- dbms pptUploaded byMd Nasir
- Lab1 Modeling ApproachUploaded byPao Renz Ong
- Cs6302 Dbms Notes RejinpaulUploaded byrewop sri
- Java Web Development with MongoDB (presented at Devoxx 2010)Uploaded byAlvin John Richards
- Physical Database Design Using OracleUploaded byAMEY
- SQL TechnologyUploaded byprodman8402
- Final Version_TE_IT_Syllabus_2015_Course.pdfUploaded byanbhute3484
- Essbase UserUploaded bykcireddy
- SYMCAUploaded byOmkar Borkar
- Database Design 2nd EditionUploaded byHanumantha Rao Mallela
- MCA 2012-13Uploaded bydineshgomber
- Chapter 2Uploaded bySachin Jain
- Nhibernate ReferenceUploaded byThan Dung
- IM-Ch09-Database-Design-Ed12.docUploaded byMohsin
- IBM Cognos Report Studio 10.2.1 User GuideUploaded bycognos
- mod4-ORDBMSUploaded byarunsmile
- Java Database Programming With JDBCUploaded byBalaji Lk
- 1.1 - Relational database concepts.pdfUploaded byTheDragon OfDestruction
- Map Reduce and New Software Stack Ch2Uploaded byVikash Kumar
- UGC-Question-Paper-of-NET-Exams-June-2013-SC-87-Computer-Science-Applications-Paper-II.pdfUploaded bymanpreetsingh3458417
- DBMSUploaded byNitin Nilesh
- Model_QP_DBMS-2Uploaded bySujith Thomas
- Oracle Data Modeler - Getting StartedUploaded byPedro Bizerra
- Foundations of DatabasesUploaded byTedy Gherasim
- Management Information SystemmissUploaded byprincesengar37