SlideShare a Scribd company logo
Advanced Database Theory
Lecture 2: Relational Algebra-I
MS Semester-III
Dr. Syed Asad Raza Kazmi
Computer Science Department
GC University Lahore
February 13, 2017
Relational Database Schemas
Relational Algebra
Relation Schemas and Relational Database Schemas
Relation Schemas and Relational Database Schemas
• A k-ary relation schema
R(A1, A2, . . . , AK ) is a set {A1, A2, . . . , Ak }
of k attributes.
• COURSE(course-no, course-name, term, instructor, room, time)
• CITY-INFO(name, state, population)
Thus, a k-ary relation schema is a blueprint, a template for some k-ary relation.
• An instance of a relation schema is a relation conforming to the schema
(arities match; also, in DBMS, data types of attributes match).
• A relational database schema is a set of relation schemas
Ri (A1, A2, . . . , Ak )
for 1 ⩽ i ⩽ m.
• A relational database instance of a relational schema is a set of relations Ri each
of which is an instance of the relation schema
Ri , 1 ⩽ i ⩽ m.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relational Database Schemas - Examples
Example 1 (Relational Database Schemas - Examples)
• BANKING relational database schema with relation schemas
• CHECKING-ACCOUNT(branch, acc-no, cust-id, balance)
• SAVINGS-ACCOUNT(branch, acc-no, cust-id, balance)
• CUSTOMER(cust-id, name, address, phone, email)
• . . .
• UNIVERSITY relational database schema with relation schemas
• STUDENT(student-id, student-name, major, status)
• FACULTY(faculty-id, faculty-name, dpt, title, salary)
• COURSE(course-no, course-name, term, instructor)
• ENROLLS(student-id, course-no, term)
• . . .
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Schemas vs. Instances
Keep in mind that there is a clear distinction between
• relation schemas and instances of relation schemas and between
• relational database schemas and relational database instances.
Syntactic Notion:
.
• Relation Schema
.
• Relational Database Schema
Semantic Notion
(discrete mathematics notion)
• Instance of a relation schema (i.e., a
relation)
• Relational database instance (i.e., a
database)
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Programming Languages Paradigms
Programming Languages Paradigms
There are two main paradigms of programming languages: imperative (or
procedural) languages and declarative languages.
1 Imperative (Procedural) Languages: programs are expressed by specifying
how the task is to be accomplished (sequence of operations).
Example 2
FORTRAN, C,...
2 Declarative Languages: programs are expressed by specifying what has to
be accomplished (as opposed to how).
Example 3
LISP (functional programming), PROLOG (logic programming),...
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Query Languages for the Relational Data Model
Query Languages for the Relational Data Model
Codd introduced two different query languages for the relational data model:
1 Relational Algebra, which is a procedural language.
• It is an algebraic formalism in which queries are expressed by applying a
sequence of operations to relations.
2 Relational Calculus, which is a declarative language.
• It is a logical formalism in which queries are expressed as formulas of
first-order logic.
Codd’s Theorem:
Relational Algebra and Relational Calculus are essentially equivalent in
terms of expressive power.
(but what does this really mean?)
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Declarative vs Procedural
Declarative vs Procedural
• In our queries, we ask what we want to see in the output.
• But we do not say how we want to get this output.
• Thus, query languages are declarative: they specify what is needed in the
output, but do not say how to get it.
• Database system figures out how to get the result, and gives it to the user.
• Database system operates internally with different, procedural languages,
which specify how to get the result.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Declarative vs Procedural: example
Declarative vs Procedural: example
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Declarative vs Procedural: another example
Declarative vs Procedural: another example
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Declarative vs Procedural
Declarative vs Procedural
• Theoretical languages:
1 Declarative: relational calculus, rule-based queries
2 Procedural: relational algebra
• Practical languages: mix of both, but mostly one uses declarative features.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Why Study the Relational Model?
Why Study the Relational Model?
• Most widely used model.
• Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
• “Legacy systems” in older models
• E.G., IBMs IMS
• Recent competitor: object-oriented model
• ObjectStore, Versant, Ontos
• A synthesis emerging: object-relational model
• Informix Universal Server, UniSQL, O2, Oracle, DB2
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relational Database: Definitions
Relational Database: Definitions
• Relational database: a set of relations
• Relation: made up of 2 parts:
1 Instance : a table, with rows and columns.
# Rows = cardinality, # fields = degree / arity.
2 Schema : specifies name of relation, plus name and type of each column.
Example 4
Students(sid: string, name: string, login: string, age: integer, gpa: real).
• Can think of a relation as a set of rows or tuples
(i.e., all rows are distinct).
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Example Instance of Students Relation
Example Instance of Students Relation
• Cardinality = 3, degree = 5, all rows distinct
• Do all columns in a relation instance have to be distinct?
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relations and Attributes
Figure 1: Anatomy of Relation
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relation Film
Figure 2: Relational Model
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Terminology
Terminology
attributes distinct columns in a relation. Each attribute has a associated
domain.
cardinality number of rows in a table
degree number of columns in a table
domain a set of different values with the same property types (”data type”).
The NULL value is possible if not explicitly stated otherwise.
relation a table (visual view) with a unique name. a two dimensional,
inhomogeneous matrix (mathematical view). The ordering of tuples
in a relation is important.
schema schema of a relation refers to the permanent characteristics of a
relation
tuple the set of values in a row. Those values are instances of the
attribute domain.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Tabular Representation
Tabular Representation
The table
A B C
1 3 2
1 4 1
2 4 2
2 3 1
is shorthand for the following set of tuples:







{A : 1, B : 3, C : 2},
{A : 1, B : 4, C : 1},
{A : 2, B : 4, C : 2},
{A : 2, B : 3, C : 1}







.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Notation
Notation
• Every tuple is a total function from a set of attributes to the set of
constants.
• Therefore, if t = {A : 1, B : 3, C : 2},
then t(B) = 3 and t[{A, C}] = {A : 1, C : 2}.
• Note that t(B) is a constant, and t[{A, C}] a tuple.
• In database theory, we often omit curly brackets ({}) and union symbols (∪)
in the notation of sets. For example, if A, B, C, D are attributes and
X = {A, B}, then XCD denotes the set {A, B, C, D}.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Algebraic Operators
Algebraic Operators
• Unary operators: Select, Project, Rename
• Binary operators: Join, Union, Difference
• Unary operators take in a single relation; binary operators take in two
relations.
NOTE
Every operator returns a single relation.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Algebraic Operators Symbols
Algebraic Operators Symbols
σ selection
π projection
∪ union
− difference
× cartesian product
ρ rename
./ join
n left semi join
o right semi join
∩ intersection
÷ division
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relational Algebra
Definition 5 (Relational Algebra)
Relation Algebra is a procedural relationally complete language. A language that
can define any relation definable in relational calculus is relationally complete.
Using this algebra we perform set operations on relations.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relations: terminology
Relations: terminology
• Degree of a relation: how long the tuples are, or how many columns the table has
• In the first example (name,email) degree of the relation is 2
• In the second example (name,email,telephone) degree of the relation is 3
• Often relations of degree 2 are called binary, relations of degree 3 are called
ternary etc.
• Cardinality of the relation: how many different tuples are there, or how many different
rows the table has.
Example-1
This is a relation between people and email
addresses
NAME Email
Hameed ham@gcu.edu.pk
Ali ali@gcu.edu.pk
Baber bab@gcu.edu.pk
Example-2
This is a relation between people, email
addresses and phone numbers
NAME Email Phone
Hameed ham@gcu.edu.pk 123456
Ali ali@gcu.edu.pk 4567843
Baber bab@gcu.edu.pk 0336245
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relations
Relations
• Each value in the first column is a name, each value in the second column is
an email address.
• In general, each column has a
domain a set from which all possible values can come
Example-1
This is a relation between people and email addresses
NAME Email
Hameed ham@gcu.edu.pk
Ali ali@gcu.edu.pk
Baber bab@gcu.edu.pk
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Relational Model:
Data Manipulation
Data Manipulation
• Data is represented as relations.
• Manipulation of data (query and update operations) corresponds to
operations on relations
• Relational algebra describes those operations. They take relations as
arguments and produce new relations.
• Think of numbers and corresponding operators +, −, or booleans and
corresponding operators &, |, ! (and, or, not).
• Relational algebra contains two kinds of operators: common set-theoretic
ones and operators specific to relations
(for example projecting on one of the columns).
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Union
Union
• Standard set-theoretic definition of union:
A ∪ B = {x : x ∈ A or x ∈ B}
Example
{a, b, c} ∪ {a, d, e} = {a, b, c, d, e}
• For relations, we want the result to be a relation again: a set
R ⊆ D1 × D2 × · · · × Dn
for some n and domains D1, D2, . . . , Dn
(or, in other words, a proper table, with each column associated with a single
domain of values).
• So we require in order to take a union of relations R and S that R and S have the
same number of columns and that corresponding columns have the same domains.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Union-compatible relations
Definition 6 (Union-compatible relations)
Two relations R and S are union-compatible if they have the same number of
columns and corresponding columns have the same domains.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Example: not union-compatible
not union-compatible
Example-1
This is a relation between people and email
addresses
NAME Email
Hameed ham@gcu.edu.pk
Ali ali@gcu.edu.pk
Baber bab@gcu.edu.pk
Example-2
This is a relation between people, email
addresses and phone numbers
NAME Email Phone
Hameed ham@gcu.edu.pk 123456
Ali ali@gcu.edu.pk 4567843
Baber bab@gcu.edu.pk 0336245
different number of columns
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Example: not union-compatible
not union-compatible
Example-1
NAME Email
Hameed ham@gcu.edu.pk
Ali ali@gcu.edu.pk
Baber bab@gcu.edu.pk
Example-2
NAME DOB
Hameed 1984
Ali 1983
Baber 1990
different domains for the second column
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Example: union-compatible
union-compatible
Example-1
NAME DOB
Aleena 1987
Fatima 1994
Marium 1995
Example-2
NAME DOB
Hameed 1984
Ali 1983
Baber 1990
union-compatible: Same number of Column and having same type
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Union of two relations
Union of two relations
• Let R and S be two union-compatible relations. Then their union R ∪ S is a
relation which contains tuples from both relations:
R ∪ S = {x : x ∈ R or x ∈ S}.
NOTE
• Note that union is a partial operation on relations: it is only defined for
some (compatible) relations, not for all of them.
• Similar to division for numbers (result of division by 0 is not defined).
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Union
Union
R A B C
1 3 5
1 4 5
S A B C
1 4 5
2 3 6
R ∪ S A B C
1 3 5
1 4 5
2 3 6
• R ∪ S is only allowed if R and S have exactly the same attributes.
• In SQL, (SELECT * FROM R) UNION (SELECT * FROM S) only requires
that R and S have the same number of attributes. The result takes the
attributes of R.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Difference of Two Relations
Difference of Two Relations
Let R and S be two union-compatible relations. Then their difference R - S is a
relation which contains tuples which are in R but not in S:
R − S = {x : x ∈ R and x 6∈ S}.
NOTE
Note that difference is also a partial operation on relations.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Difference
Difference
R A B C
1 3 5
1 4 5
S A B C
1 4 5
2 3 6
R − S A B C
1 3 5
• R − S is only allowed if R and S have exactly the same attributes.
• In SQL, (SELECT * FROM R) MINUS (SELECT * FROM S) only requires
that R and S have the same number of attributes. The result takes the
attributes of R.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Intersection of two relations
Intersection of Two Relations
Let R and S be two union-compatible relations. Then their intersection a relation
R ∩ S which contains tuples which are both in R and S:
R ∩ S = {x : x ∈ R and x ∈ S}
NOTE
1 Note that intersection is also a partial operation on relations.
2 Set-intersection is defined in terms of set-difference:
r ∩ s = r − (r − s)
3 Thus, set-intersection must follow the same compatibility rules as
set-difference: same arity, corresponding domains.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Cartesian Product
Definition 7 (Cartesian Product)
1 Cartesian product is a total operation on relations.
2 Usual set theoretic definition of product:
R × S = {< x, y >: x ∈ R, y ∈ S}
Example 8
Under the standard definition, if
< Cheese, 1.34 >∈ R and < Soap, 1.00 >∈ S
then
<< Cheese, 1.34 >, < Soap, 1.00 >>∈ R × S
(the result is a pair of tuples).
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Extended Cartesian product
Extended Cartesian product
• Extended Cartesian product flattens the result in a 4-element tuple:
< Cheese, 1.34, Soap, 1.00 >
Definition 9 (Extended Cartesian Product of Relations)
Let R be a relation with column domains {A1, . . . , An} and
S a relation with column domains {B1, . . . , Bm}
Then their Extended Cartesian product R × S is a relation
R × S = {< c1, . . . , cn, cn+1, . . . , cn+m >:< c1, . . . , cn >∈ R, < cn+1, . . . , cn+m >∈ S}
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Example
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
The Projection Operator
The Projection Operator
Motivation:
It is often the case that, given a table R, one wants to:
• Rearrange the order of the columns
• Suppress some columns
• Do both of the above.
Fact
Fact: The Projection Operation is tailored for this task
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Projection
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Projection
Projection
Let R be a relation with n columns, and X is a set of column identifiers (at the
moment, we will use numbers, but later we will give then names, like Email, or
Telephone). Then projection of R on X is a new relation
πX (R)
which only has columns from X.
Example 10
For example,
π1,2(R)
is a table with only the 1st and 2nd columns from R.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Projection
Definition 11 (Projection)
The projection is an operation to reduce a relation containing containing only
specified columns. All duplicate result tuples will be removed.
Projection Syntax
project <source relation name>
over <list of attribute names>
giving <result relation name>
The list of attribute names shall be given as comma separated list.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Project
Project
R A B C
1 3 1
1 3 2
1 4 3
1 4 4
2 3 5
π{A,B}(R) A B
1 3
1 4
2 3
• Since relations are sets, duplicates are removed.
• Note that SELECT A, B FROM R in SQL does not remove duplicates.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Selection
Selection
• Chooses tuples that satisfy some condition
• σc (R) : only leaves tuples t for which c(t) is true
• Conditions: conjunctions of
1 R.A = R.A0
-two attributes are equal
2 R.A = constant the value of an attribute is a given constant
Same as above but with 6= instead of =
Example 12
• Movies.Actor=Movies.Director
Movies.Actor 6= Nicholson
Movies.Actor=Movies.Director ∧ Movies.Actor=Nicholson
• Provides the user with a view of data by hiding tuples that do not satisfy the
condition the user wants.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Selection
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Select
Select
R A B C
1 3 2
1 4 1
2 4 2
2 3 1
σA=“1”(R) A B C
1 3 2
1 4 1
σA=C (R) A B C
1 4 1
2 4 2
• This is like SELECT * FROM R WHERE A="1" in SQL.
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
Combining Selection and Projection
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
PROJECT Operation Properties
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 in the
result of PROJECT is equal to the number of tuples in R
• PROJECT is not commutative
π<list1>(π<list2>(R)) = π<list1>(R)
as long as < list2 > contains the attributes in < list1 >
Dr. Syed Asad Raza Kazmi Advanced Database Theory
Relational Database Schemas
Relational Algebra
References I
• Slides of Relational Databases, Logic, and Complexity by Phokion G. Kolaitis; University of California, Santa Cruz & IBM Research-Almaden
• Foundations of Databases Relational Query Languages; Slides from Werner Nutt, Thomas Eiter and Leonid Libkin
• Foundation of Database by Serge Abiteboul, Richard Hull, Victor Vianu
Dr. Syed Asad Raza Kazmi Advanced Database Theory
After all... tomorrow is another day.
(Scarlett O’Hara, “Gone with the Wind”)

More Related Content

PPTX
316_16SCCCS4_2020052505222431.pptdatabasex
PPTX
Lecture-2 - Relational Model.pptx
PPT
relational algebra and it's implementation
PPT
Relational Algebra
PPTX
Relational Model,relational calulus.pptx
PPTX
L4_Relational DatabasesDatabase Systems and Programming
PPTX
L4_Relational Databases Database Systems and Programming
PPT
Query Decomposition and data localization
316_16SCCCS4_2020052505222431.pptdatabasex
Lecture-2 - Relational Model.pptx
relational algebra and it's implementation
Relational Algebra
Relational Model,relational calulus.pptx
L4_Relational DatabasesDatabase Systems and Programming
L4_Relational Databases Database Systems and Programming
Query Decomposition and data localization

Similar to Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf (20)

PPTX
RDBMS
PPT
Knowledge Discovery Query Language (KDQL)
PPTX
DBMS Module-2 notes for engineering BE vtu
PDF
Query Optimization - Brandon Latronica
PPTX
Relational model
PPTX
Relational Algebra in DBMS power ppoint pesenetation
PPTX
R programming
PPTX
Islamic University Previous Year Question Solution 2019 (ADBMS)
PDF
DBMS & Data Models - In Introduction
PDF
Relational Database Model Database Management system
PDF
19IS305_U2_LP4_LM4-22-23.pdf
PPT
Dbms relational model
PDF
3_Relational_Model.pdf
PDF
Intro to relational model. Database Systems
PPTX
Quick Revision on DATA BASE MANAGEMENT SYSTEMS concepts.pptx
PPTX
Reasoning of database consistency through description logics
PPT
2 rel-algebra
DOCX
Mi0034 database management systems
PPT
The Relational Model represents data and their relationships through a collec...
PPTX
Relational Algebra in Database Systems.pptx
RDBMS
Knowledge Discovery Query Language (KDQL)
DBMS Module-2 notes for engineering BE vtu
Query Optimization - Brandon Latronica
Relational model
Relational Algebra in DBMS power ppoint pesenetation
R programming
Islamic University Previous Year Question Solution 2019 (ADBMS)
DBMS & Data Models - In Introduction
Relational Database Model Database Management system
19IS305_U2_LP4_LM4-22-23.pdf
Dbms relational model
3_Relational_Model.pdf
Intro to relational model. Database Systems
Quick Revision on DATA BASE MANAGEMENT SYSTEMS concepts.pptx
Reasoning of database consistency through description logics
2 rel-algebra
Mi0034 database management systems
The Relational Model represents data and their relationships through a collec...
Relational Algebra in Database Systems.pptx
Ad

More from ssuserf86fba (16)

PDF
Lecture 1(i). ifs about information process cyle using ict concept
PDF
Lecture 1.pdf its about introduction of information and .
PPTX
Lecture of using iict concept in c++ basic
PDF
Input and output basic of c++ programming and escape sequences
PDF
detail of flowchart and algorithm that are used in programmingpdf
PDF
Introduction To C++ programming and its basic concepts
PDF
problem solving skills and its related all information
PDF
system and application software are used in computer.
PDF
its about computer storage and its managements how to manage the memory, in a...
PDF
computerarchitecturecachememory-170927134432.pdf
PDF
number system in introduction to computer language
PDF
introduction to computer and technology-Lecture-1.pdf
PPTX
cache memory and cloud computing technology
PDF
its about information process cycle and its components
PDF
cental processing unit and all its components
PPTX
Ipc (how we transfer the information end to end
Lecture 1(i). ifs about information process cyle using ict concept
Lecture 1.pdf its about introduction of information and .
Lecture of using iict concept in c++ basic
Input and output basic of c++ programming and escape sequences
detail of flowchart and algorithm that are used in programmingpdf
Introduction To C++ programming and its basic concepts
problem solving skills and its related all information
system and application software are used in computer.
its about computer storage and its managements how to manage the memory, in a...
computerarchitecturecachememory-170927134432.pdf
number system in introduction to computer language
introduction to computer and technology-Lecture-1.pdf
cache memory and cloud computing technology
its about information process cycle and its components
cental processing unit and all its components
Ipc (how we transfer the information end to end
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Lesson notes of climatology university.
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Cell Types and Its function , kingdom of life
O5-L3 Freight Transport Ops (International) V1.pdf
Pre independence Education in Inndia.pdf
Lesson notes of climatology university.
VCE English Exam - Section C Student Revision Booklet
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Insiders guide to clinical Medicine.pdf
O7-L3 Supply Chain Operations - ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
GDM (1) (1).pptx small presentation for students
human mycosis Human fungal infections are called human mycosis..pptx
Renaissance Architecture: A Journey from Faith to Humanism
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

Lecture-2-Relational-Algebra-and-SQL-Advanced-DataBase-Theory-MS.pdf

  • 1. Advanced Database Theory Lecture 2: Relational Algebra-I MS Semester-III Dr. Syed Asad Raza Kazmi Computer Science Department GC University Lahore February 13, 2017
  • 2. Relational Database Schemas Relational Algebra Relation Schemas and Relational Database Schemas Relation Schemas and Relational Database Schemas • A k-ary relation schema R(A1, A2, . . . , AK ) is a set {A1, A2, . . . , Ak } of k attributes. • COURSE(course-no, course-name, term, instructor, room, time) • CITY-INFO(name, state, population) Thus, a k-ary relation schema is a blueprint, a template for some k-ary relation. • An instance of a relation schema is a relation conforming to the schema (arities match; also, in DBMS, data types of attributes match). • A relational database schema is a set of relation schemas Ri (A1, A2, . . . , Ak ) for 1 ⩽ i ⩽ m. • A relational database instance of a relational schema is a set of relations Ri each of which is an instance of the relation schema Ri , 1 ⩽ i ⩽ m. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 3. Relational Database Schemas Relational Algebra Relational Database Schemas - Examples Example 1 (Relational Database Schemas - Examples) • BANKING relational database schema with relation schemas • CHECKING-ACCOUNT(branch, acc-no, cust-id, balance) • SAVINGS-ACCOUNT(branch, acc-no, cust-id, balance) • CUSTOMER(cust-id, name, address, phone, email) • . . . • UNIVERSITY relational database schema with relation schemas • STUDENT(student-id, student-name, major, status) • FACULTY(faculty-id, faculty-name, dpt, title, salary) • COURSE(course-no, course-name, term, instructor) • ENROLLS(student-id, course-no, term) • . . . Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 4. Relational Database Schemas Relational Algebra Schemas vs. Instances Keep in mind that there is a clear distinction between • relation schemas and instances of relation schemas and between • relational database schemas and relational database instances. Syntactic Notion: . • Relation Schema . • Relational Database Schema Semantic Notion (discrete mathematics notion) • Instance of a relation schema (i.e., a relation) • Relational database instance (i.e., a database) Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 5. Relational Database Schemas Relational Algebra Programming Languages Paradigms Programming Languages Paradigms There are two main paradigms of programming languages: imperative (or procedural) languages and declarative languages. 1 Imperative (Procedural) Languages: programs are expressed by specifying how the task is to be accomplished (sequence of operations). Example 2 FORTRAN, C,... 2 Declarative Languages: programs are expressed by specifying what has to be accomplished (as opposed to how). Example 3 LISP (functional programming), PROLOG (logic programming),... Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 6. Relational Database Schemas Relational Algebra Query Languages for the Relational Data Model Query Languages for the Relational Data Model Codd introduced two different query languages for the relational data model: 1 Relational Algebra, which is a procedural language. • It is an algebraic formalism in which queries are expressed by applying a sequence of operations to relations. 2 Relational Calculus, which is a declarative language. • It is a logical formalism in which queries are expressed as formulas of first-order logic. Codd’s Theorem: Relational Algebra and Relational Calculus are essentially equivalent in terms of expressive power. (but what does this really mean?) Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 7. Relational Database Schemas Relational Algebra Declarative vs Procedural Declarative vs Procedural • In our queries, we ask what we want to see in the output. • But we do not say how we want to get this output. • Thus, query languages are declarative: they specify what is needed in the output, but do not say how to get it. • Database system figures out how to get the result, and gives it to the user. • Database system operates internally with different, procedural languages, which specify how to get the result. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 8. Relational Database Schemas Relational Algebra Declarative vs Procedural: example Declarative vs Procedural: example Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 9. Relational Database Schemas Relational Algebra Declarative vs Procedural: another example Declarative vs Procedural: another example Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 10. Relational Database Schemas Relational Algebra Declarative vs Procedural Declarative vs Procedural • Theoretical languages: 1 Declarative: relational calculus, rule-based queries 2 Procedural: relational algebra • Practical languages: mix of both, but mostly one uses declarative features. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 11. Relational Database Schemas Relational Algebra Why Study the Relational Model? Why Study the Relational Model? • Most widely used model. • Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. • “Legacy systems” in older models • E.G., IBMs IMS • Recent competitor: object-oriented model • ObjectStore, Versant, Ontos • A synthesis emerging: object-relational model • Informix Universal Server, UniSQL, O2, Oracle, DB2 Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 12. Relational Database Schemas Relational Algebra Relational Database: Definitions Relational Database: Definitions • Relational database: a set of relations • Relation: made up of 2 parts: 1 Instance : a table, with rows and columns. # Rows = cardinality, # fields = degree / arity. 2 Schema : specifies name of relation, plus name and type of each column. Example 4 Students(sid: string, name: string, login: string, age: integer, gpa: real). • Can think of a relation as a set of rows or tuples (i.e., all rows are distinct). Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 13. Relational Database Schemas Relational Algebra Example Instance of Students Relation Example Instance of Students Relation • Cardinality = 3, degree = 5, all rows distinct • Do all columns in a relation instance have to be distinct? Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 14. Relational Database Schemas Relational Algebra Relations and Attributes Figure 1: Anatomy of Relation Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 15. Relational Database Schemas Relational Algebra Relation Film Figure 2: Relational Model Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 16. Relational Database Schemas Relational Algebra Terminology Terminology attributes distinct columns in a relation. Each attribute has a associated domain. cardinality number of rows in a table degree number of columns in a table domain a set of different values with the same property types (”data type”). The NULL value is possible if not explicitly stated otherwise. relation a table (visual view) with a unique name. a two dimensional, inhomogeneous matrix (mathematical view). The ordering of tuples in a relation is important. schema schema of a relation refers to the permanent characteristics of a relation tuple the set of values in a row. Those values are instances of the attribute domain. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 17. Relational Database Schemas Relational Algebra Tabular Representation Tabular Representation The table A B C 1 3 2 1 4 1 2 4 2 2 3 1 is shorthand for the following set of tuples:        {A : 1, B : 3, C : 2}, {A : 1, B : 4, C : 1}, {A : 2, B : 4, C : 2}, {A : 2, B : 3, C : 1}        . Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 18. Relational Database Schemas Relational Algebra Notation Notation • Every tuple is a total function from a set of attributes to the set of constants. • Therefore, if t = {A : 1, B : 3, C : 2}, then t(B) = 3 and t[{A, C}] = {A : 1, C : 2}. • Note that t(B) is a constant, and t[{A, C}] a tuple. • In database theory, we often omit curly brackets ({}) and union symbols (∪) in the notation of sets. For example, if A, B, C, D are attributes and X = {A, B}, then XCD denotes the set {A, B, C, D}. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 19. Relational Database Schemas Relational Algebra Algebraic Operators Algebraic Operators • Unary operators: Select, Project, Rename • Binary operators: Join, Union, Difference • Unary operators take in a single relation; binary operators take in two relations. NOTE Every operator returns a single relation. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 20. Relational Database Schemas Relational Algebra Algebraic Operators Symbols Algebraic Operators Symbols σ selection π projection ∪ union − difference × cartesian product ρ rename ./ join n left semi join o right semi join ∩ intersection ÷ division Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 21. Relational Database Schemas Relational Algebra Relational Algebra Definition 5 (Relational Algebra) Relation Algebra is a procedural relationally complete language. A language that can define any relation definable in relational calculus is relationally complete. Using this algebra we perform set operations on relations. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 22. Relational Database Schemas Relational Algebra Relations: terminology Relations: terminology • Degree of a relation: how long the tuples are, or how many columns the table has • In the first example (name,email) degree of the relation is 2 • In the second example (name,email,telephone) degree of the relation is 3 • Often relations of degree 2 are called binary, relations of degree 3 are called ternary etc. • Cardinality of the relation: how many different tuples are there, or how many different rows the table has. Example-1 This is a relation between people and email addresses NAME Email Hameed ham@gcu.edu.pk Ali ali@gcu.edu.pk Baber bab@gcu.edu.pk Example-2 This is a relation between people, email addresses and phone numbers NAME Email Phone Hameed ham@gcu.edu.pk 123456 Ali ali@gcu.edu.pk 4567843 Baber bab@gcu.edu.pk 0336245 Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 23. Relational Database Schemas Relational Algebra Relations Relations • Each value in the first column is a name, each value in the second column is an email address. • In general, each column has a domain a set from which all possible values can come Example-1 This is a relation between people and email addresses NAME Email Hameed ham@gcu.edu.pk Ali ali@gcu.edu.pk Baber bab@gcu.edu.pk Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 24. Relational Database Schemas Relational Algebra Relational Model: Data Manipulation Data Manipulation • Data is represented as relations. • Manipulation of data (query and update operations) corresponds to operations on relations • Relational algebra describes those operations. They take relations as arguments and produce new relations. • Think of numbers and corresponding operators +, −, or booleans and corresponding operators &, |, ! (and, or, not). • Relational algebra contains two kinds of operators: common set-theoretic ones and operators specific to relations (for example projecting on one of the columns). Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 25. Relational Database Schemas Relational Algebra Union Union • Standard set-theoretic definition of union: A ∪ B = {x : x ∈ A or x ∈ B} Example {a, b, c} ∪ {a, d, e} = {a, b, c, d, e} • For relations, we want the result to be a relation again: a set R ⊆ D1 × D2 × · · · × Dn for some n and domains D1, D2, . . . , Dn (or, in other words, a proper table, with each column associated with a single domain of values). • So we require in order to take a union of relations R and S that R and S have the same number of columns and that corresponding columns have the same domains. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 26. Relational Database Schemas Relational Algebra Union-compatible relations Definition 6 (Union-compatible relations) Two relations R and S are union-compatible if they have the same number of columns and corresponding columns have the same domains. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 27. Relational Database Schemas Relational Algebra Example: not union-compatible not union-compatible Example-1 This is a relation between people and email addresses NAME Email Hameed ham@gcu.edu.pk Ali ali@gcu.edu.pk Baber bab@gcu.edu.pk Example-2 This is a relation between people, email addresses and phone numbers NAME Email Phone Hameed ham@gcu.edu.pk 123456 Ali ali@gcu.edu.pk 4567843 Baber bab@gcu.edu.pk 0336245 different number of columns Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 28. Relational Database Schemas Relational Algebra Example: not union-compatible not union-compatible Example-1 NAME Email Hameed ham@gcu.edu.pk Ali ali@gcu.edu.pk Baber bab@gcu.edu.pk Example-2 NAME DOB Hameed 1984 Ali 1983 Baber 1990 different domains for the second column Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 29. Relational Database Schemas Relational Algebra Example: union-compatible union-compatible Example-1 NAME DOB Aleena 1987 Fatima 1994 Marium 1995 Example-2 NAME DOB Hameed 1984 Ali 1983 Baber 1990 union-compatible: Same number of Column and having same type Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 30. Relational Database Schemas Relational Algebra Union of two relations Union of two relations • Let R and S be two union-compatible relations. Then their union R ∪ S is a relation which contains tuples from both relations: R ∪ S = {x : x ∈ R or x ∈ S}. NOTE • Note that union is a partial operation on relations: it is only defined for some (compatible) relations, not for all of them. • Similar to division for numbers (result of division by 0 is not defined). Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 31. Relational Database Schemas Relational Algebra Union Union R A B C 1 3 5 1 4 5 S A B C 1 4 5 2 3 6 R ∪ S A B C 1 3 5 1 4 5 2 3 6 • R ∪ S is only allowed if R and S have exactly the same attributes. • In SQL, (SELECT * FROM R) UNION (SELECT * FROM S) only requires that R and S have the same number of attributes. The result takes the attributes of R. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 32. Relational Database Schemas Relational Algebra Difference of Two Relations Difference of Two Relations Let R and S be two union-compatible relations. Then their difference R - S is a relation which contains tuples which are in R but not in S: R − S = {x : x ∈ R and x 6∈ S}. NOTE Note that difference is also a partial operation on relations. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 33. Relational Database Schemas Relational Algebra Difference Difference R A B C 1 3 5 1 4 5 S A B C 1 4 5 2 3 6 R − S A B C 1 3 5 • R − S is only allowed if R and S have exactly the same attributes. • In SQL, (SELECT * FROM R) MINUS (SELECT * FROM S) only requires that R and S have the same number of attributes. The result takes the attributes of R. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 34. Relational Database Schemas Relational Algebra Intersection of two relations Intersection of Two Relations Let R and S be two union-compatible relations. Then their intersection a relation R ∩ S which contains tuples which are both in R and S: R ∩ S = {x : x ∈ R and x ∈ S} NOTE 1 Note that intersection is also a partial operation on relations. 2 Set-intersection is defined in terms of set-difference: r ∩ s = r − (r − s) 3 Thus, set-intersection must follow the same compatibility rules as set-difference: same arity, corresponding domains. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 35. Relational Database Schemas Relational Algebra Cartesian Product Definition 7 (Cartesian Product) 1 Cartesian product is a total operation on relations. 2 Usual set theoretic definition of product: R × S = {< x, y >: x ∈ R, y ∈ S} Example 8 Under the standard definition, if < Cheese, 1.34 >∈ R and < Soap, 1.00 >∈ S then << Cheese, 1.34 >, < Soap, 1.00 >>∈ R × S (the result is a pair of tuples). Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 36. Relational Database Schemas Relational Algebra Extended Cartesian product Extended Cartesian product • Extended Cartesian product flattens the result in a 4-element tuple: < Cheese, 1.34, Soap, 1.00 > Definition 9 (Extended Cartesian Product of Relations) Let R be a relation with column domains {A1, . . . , An} and S a relation with column domains {B1, . . . , Bm} Then their Extended Cartesian product R × S is a relation R × S = {< c1, . . . , cn, cn+1, . . . , cn+m >:< c1, . . . , cn >∈ R, < cn+1, . . . , cn+m >∈ S} Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 37. Relational Database Schemas Relational Algebra Example Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 38. Relational Database Schemas Relational Algebra The Projection Operator The Projection Operator Motivation: It is often the case that, given a table R, one wants to: • Rearrange the order of the columns • Suppress some columns • Do both of the above. Fact Fact: The Projection Operation is tailored for this task Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 39. Relational Database Schemas Relational Algebra Projection Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 40. Relational Database Schemas Relational Algebra Projection Projection Let R be a relation with n columns, and X is a set of column identifiers (at the moment, we will use numbers, but later we will give then names, like Email, or Telephone). Then projection of R on X is a new relation πX (R) which only has columns from X. Example 10 For example, π1,2(R) is a table with only the 1st and 2nd columns from R. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 41. Relational Database Schemas Relational Algebra Projection Definition 11 (Projection) The projection is an operation to reduce a relation containing containing only specified columns. All duplicate result tuples will be removed. Projection Syntax project <source relation name> over <list of attribute names> giving <result relation name> The list of attribute names shall be given as comma separated list. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 42. Relational Database Schemas Relational Algebra Project Project R A B C 1 3 1 1 3 2 1 4 3 1 4 4 2 3 5 π{A,B}(R) A B 1 3 1 4 2 3 • Since relations are sets, duplicates are removed. • Note that SELECT A, B FROM R in SQL does not remove duplicates. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 43. Relational Database Schemas Relational Algebra Selection Selection • Chooses tuples that satisfy some condition • σc (R) : only leaves tuples t for which c(t) is true • Conditions: conjunctions of 1 R.A = R.A0 -two attributes are equal 2 R.A = constant the value of an attribute is a given constant Same as above but with 6= instead of = Example 12 • Movies.Actor=Movies.Director Movies.Actor 6= Nicholson Movies.Actor=Movies.Director ∧ Movies.Actor=Nicholson • Provides the user with a view of data by hiding tuples that do not satisfy the condition the user wants. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 44. Relational Database Schemas Relational Algebra Selection Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 45. Relational Database Schemas Relational Algebra Select Select R A B C 1 3 2 1 4 1 2 4 2 2 3 1 σA=“1”(R) A B C 1 3 2 1 4 1 σA=C (R) A B C 1 4 1 2 4 2 • This is like SELECT * FROM R WHERE A="1" in SQL. Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 46. Relational Database Schemas Relational Algebra Combining Selection and Projection Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 47. Relational Database Schemas Relational Algebra PROJECT Operation Properties 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 in the result of PROJECT is equal to the number of tuples in R • PROJECT is not commutative π<list1>(π<list2>(R)) = π<list1>(R) as long as < list2 > contains the attributes in < list1 > Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 48. Relational Database Schemas Relational Algebra References I • Slides of Relational Databases, Logic, and Complexity by Phokion G. Kolaitis; University of California, Santa Cruz & IBM Research-Almaden • Foundations of Databases Relational Query Languages; Slides from Werner Nutt, Thomas Eiter and Leonid Libkin • Foundation of Database by Serge Abiteboul, Richard Hull, Victor Vianu Dr. Syed Asad Raza Kazmi Advanced Database Theory
  • 49. After all... tomorrow is another day. (Scarlett O’Hara, “Gone with the Wind”)