SlideShare a Scribd company logo
1 of 35
Download to read offline
Introduction to Relational Algebra
 RDBMS is an enhancement to DBMS. It is a system in which:
The data is perceived by the user as tables; and
 The operators (or operations) at the user's disposal include atleast
RESTRICT (SELECT), PROJECT and JOIN.
Relational Database Management System
(RDBMS)
Recall: Database Management System (DBMS)
•DBMS is an application, which holds user data
permanently and then provide different operations on
this data e.g., retrieval of data, insertion of data, updation
of data etc.
•It is a computerized system whose overall purpose is to
maintain information and to make that information
available on demand.
 Relational Database:
A database in which data is perceived as tables
 It is a prescription for a way of representing data by
means of tables and a prescription for a way of
manipulating such a representation using some operators.
 It is an abstract theory of data that is based on certain
aspects of mathematics like set theory and predicate
logic.
 The principles of relational model were originally laid
down in 1969-70 by Dr. E.F.Codd.
 Relation
 Tuple, Attribute
 Cardianlity, Degree
 Domain
tuples
attributes
ID Name Age Department
S1 Ahmad 23 Sales
S2 Salman 34 Marketing
S3 Karim 21 Sales
S4 Tariq 29 Admin
S5 Sadiq 32 Sales
Employee Relation
Cardinality
Degree
 There are no duplicate tuples
 Tuples are unordered
 Attributes are unordered
 All attributes have atomic values
 The sequence of attributes within a relation is of no
importance.
 Candidate key
◦ General definition:
 A set of attributes which can uniquely identify each row in the table
◦ Relational Model Definition:
 Let R be a relation. Then candidate key for R is a subset of the set
of attributes of R say K, such that:
1 Uniqueness Property:
no two distinct tuples of R have the same value for K.
2 Irreducibility property:
no proper subset of K has the uniqueness propert.
Possible Candidate Keys:
• ID
• NIC
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
 Primary key
◦ is a unique identifier for the table, that is , a column or
column combination with the property that, at any given time,
no two rows of the table contain same value in that column or
column combination.
◦ One of the candidate keys
 Alternate Keys
◦ All candidate keys other than primary key are called alternate
keys
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
Primary Key: ID
Alternate Key: NIC
 Foreign key
◦ General definition:
 A set of attributes in a table whose values are taken from the values
of candidate key of some other table
◦ Relational Model Definition:
 Let R2 be a relation. Then a foreign key in R2 is a subset is a subset
of the set of attributes of R2, say FK, such that:
1 there exists a base relation R1 (R1 and R2 not necessarily distinct)
with a candidate key CK and
2 for all time, each value of FK in the current value of R2 is identical
to the value of CK in some tuple in the current value of R1
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
Department Location
Sales Floor1
Marketing Floor3
Admin Floor5
Same values
 Referenced tuple or Target tuple
 Referencing relation
 Referenced relation or Target relation
 Simple key vs Composite key
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
Department Location
Sales Floor1
Marketing Floor3
Admin Floor5
Target tuple
Referencing relation Target relation
 Integrity constraints ensure that the data is
accurate. Every attribute has an associated
domain, there are constraints(called domain
constraints) that form restrictions on the set
of values allowed for the attributes of
relations.
 Null represents a value for an attribute that
is currently unknown or is not applicable for
this tuple.
 In a base relation no attribute of a primary
key can be null.
 A primary key is a minimal identifier that is
used to identify tuples uniquely. This means
that no subset of primary key is sufficient to
provide unique identification of tuples.
 If a foreign key exists in a relation, the
foreign key value must match a candidate
key value of some tuple in its home
relation.
 Possible cases: update,delete
 Operators
◦ Relational Algebra consists of eight operators:
 Four traditional set operators: UNION, INTERSECTION,
DIFFERENCE, CARTESIAN PRODUCT
 Four special relational operators: RESTRICTION, PROJECTION,
JOIN, DIVISION
 Operands
◦ Relations are the operands used in Relational Algebra
 Closure property of Relations
◦ output from some relational operator is always a relation.
 UNION
◦ the union of two type-compatible relations A and B (A
UNION B) is a relation with the same heading as each of
A and B and with a body consisting of the set of all
tuples belonging to A and B.
◦ relations must have same heading
◦ same tuples are included once
A
B
 UNION
◦ Returns a relation consisting of all tuples appearing in
either or both relations
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S4 Tariq 29 Admin 245-71-325370
A
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
B
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
A UNION B
 INTERSECTION
◦ the intersection of two type-compatible relations A and
B (A INTERSECT B) is a relation with the same heading as
each of A and B and with a body consisting of the set of
all tuples belonging to both A and B.
◦ relations must have same heading
A
B
 INTERSECTION
◦ Returns a relation consisting of all tuples appearing
in both of two specified relations
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S4 Tariq 29 Admin 245-71-325370
A
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
B
ID Name Age Department NIC
S4 Tariq 29 Admin 245-71-325370
A INTERSECT B
 DIFFERENCE
◦ the difference between two type-compatible relations A
and B, in that order (A MINUS B) is a relation with the
same heading as each of A and B and with a body
consisting of the set of all tuples belonging to A and not
to B.
◦ relations must have same heading
◦ direction of operation does matter
A
B
 DIFFERENCE
◦ Returns a relation consisting of all tuples appearing in
the first and not in the second of two specified relations
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S4 Tariq 29 Admin 245-71-325370
A
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
B
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
A MINUS B
ID Name Age Department NIC
S3 Karim 21 Sales 255-79-256369
S5 Sadiq 32 Sales 245-68-345371
B MINUS A
 PRODUCT (CARTESIAN PRODUCT)
◦ the product of two relations A and B with no common
attribute, (A TIMES B) is a relation with a same heading as
set of all attributes in each of A and B and with a body
consisting of the set of all tuples such that each resulting
tuple is combination of a tuple from A and a tuple from
B.
◦ Cardinality of resulting relation is equal to the product of
the cardinalities of A and B.
◦ Degree of resulting relation is equal to the sum of the
degrees of A and B.
 Returns a relation consisting of all possible tuples that are
a combination of two tuples, one from each of two
specified relations.
 PRODUCT (CARTESIAN PRODUCT)
ID Name Age
S1 Ahmad 23
S2 Salman 34
S4 Tariq 29
A
Department NIC
Sales 255-79-256369
Admin 245-71-325370
Sales 245-68-345371
B
ID Name Age Department NIC
S1 Ahmad 23 Sales 255-79-256369
S1 Ahmad 23 Admin 245-71-325370
S1 Ahmad 23 Sales 245-68-345371
S2 Salman 34 Sales 255-79-256369
S2 Salman 34 Admin 245-71-325370
S2 Salman 34 Sales 245-68-345371
S4 Tariq 29 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S4 Tariq 29 Sales 245-68-345371
A TIMES B
 Q1: Identify the possible candidate keys in the
given relation?
 Q2: There are 4 candidate keys identified in a
relation each having 12 elements. What would
be the cardinality of the relation. Also write the
lowest possible value of degree for that relation.
 Q3: Explain meaning of RESTRICTED and
CASCADE in case of update for a foreign key.
 Q4: Give an example of repeating group from
keeping in view the relation given above.
ID LAST_NAME FIRST_NAME USERID SALARY
1 Patel Ralph rpatel 795
2 Dancs Betty bdancs 860
3 Biri Ben bbiri 1100
4 Newman Chad cnewman 750
5 Ropeburn Audry aropebur 1550
 Commutative
◦ Union, Intersect and Times are Commutative:
 A UNION B is equal to B UNION A
 A INTERSECT B is equal to B INTERSECT A
 A TIMES B is equal to B TIMES A
◦ MINUS is not Commutative i.e.,
 A MINUS B is not equal to B MINUS A
 Associative
◦ Union, Intersect and Times are Associative:
 (A UNION B) UNION C is equal to A UNION (B UNION C)
 (A INTERSECT B) INTERSECT C is equal to A INTERSECT (B
INERSECT C)
 (A TIMES B) TIMES C is equal to A TIMES (B TIMES C)
◦ MINUS is not Associative i.e.,
 (A MINUS B) MINUS C is not equal to A MINUS (B MINUS C)
 RESTRICTION
◦ is actually abbreviation for θ-restriction, where “θ” stands for
any simple scalar comparison operator(=,<,>,=)
◦ θ-restriction of relation A on attributes X and Y( in that order)
A where X θ Y
is a relation with the same heading as A and with a
body consisting of the set of all tuples of A such that the
condition “X θ Y” evaluates true for those tuples.
◦ X and Y must be defined on the same domain
◦ operator must make sense for that domain
A
 RESTRICTION
◦ Returns a relation consisting of all tuples from a
specified relation that satisfy a specified condition.
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
A
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S3 Karim 21 Sales 255-79-256369
S5 Sadiq 32 Sales 245-68-345371
A WHERE Department=“Sales”
 PROJECTION
◦ the projection of relation A on X,Y,….,Z (where each of
X,Y,….,Z is an attribute of A) is a relation with heading
{X,Y,…,Z } and body consisting of the set of all tuples
{X:x,Y:y,…,Z:z } such that a tuple appears in A with X-
value x, Y-value y,…., Z-value z.
◦ projection yields a vertical subset of relation
A
 PROJECTION
◦ Returns a relation consisting of all tuples that remain as
(sub) tuples in a specified relation after specified
attributes have been eliminated
ID Name Age Department NIC
S1 Ahmad 23 Sales 245-77-245367
S2 Salman 34 Marketing 234-66-245368
S3 Karim 21 Sales 255-79-256369
S4 Tariq 29 Admin 245-71-325370
S5 Sadiq 32 Sales 245-68-345371
A
Name Age Department
Ahmad 23 Sales
Karim 21 Sales
Sadiq 32 Sales
A where
Department=“Sales”
[Name,Department]
Name
Ahmad
Salman
Karim
Tariq
Sadiq
A [Name]
 JOIN(NATURAL JOIN)
◦ Let relations A and B have headings [X,…,Y] and
[X,…,Z] respectively, then
A JOIN B
is a relation with heading [X,…,Y,…,Z] and with a
body consisting of the set of all tuples [X:x,Y:y,Z:z]
such that a tuple appears in A with X-value x and Y-
value y and in B with X-value x and Z-value z.
◦ there should be some common attribute(s)
a1 b1
a2 b2
a3 b2
b1 c1
b2 c2
b3 c3
a1 b1 c1
a2 b2 c2
a3 b2 c2
JOIN
 JOIN(NATURAL JOIN)
◦ Returns a relation consisting of all possible tuples that are
combination of two tuples, one from each of the two
specified relations, such that two tuples contributing to
any given combination have a common value for the
common attributes (and that value appears just once)
ID Name
S1 Ahmad
S2 Salman
S3 Karim
A
ID Subject
S1 Math
S2 Urdu
S1 English
B
ID Name Subject
S1 Ahmad Math
S1 Ahmad English
S2 Salman Urdu
A JOIN B
 θ-JOIN
◦ Let relations A and B have no attribute names in
common (as in Cartesian Product), and let θ be as
defined in restriction. Then the θ- JOIN of relation A
on attribute X with relation B on attribute Y is defined
by the result of the expression
(A TIMES B) where X θ Y
◦ it is a relation with same heading as Cartesian Product
of A and B and with a body consisting of the set of all
those tuples belonging to that Cartesian Product of
that evaluate true for X θ Y.
◦ if θ is “equals”, the θ-JOIN is called an EQUIJOIN.
a1 b1
a2 b2
a3 b2
b1 c1
b2 c2
b3 c3
a2 b2 b1 c1
a3 b2 b1 c1
θ-JOIN
 θ- JOIN
ID Name
S1 Ahmad
S2 Salman
S3 Karim
A
ID Subject
S1 Math
S2 Urdu
S1 English
B
IDA Name IDB Subject
S2 Salman S1 Math
S2 Salman S1 English
S3 Karim S1 Math
S3 Karim S1 English
S3 Karim S2 Urdu
(A TIMES B) where IDA > IDB
IDA Name IDB Subject
S1 Ahmad S1 Math
S1 Ahmad S1 English
S2 Salman S2 Urdu
(A TIMES B) where IDA = IDB
 DIVISION
◦ Let relations A and B have headings
{ x1, x2, …, xm, y1, y2,…, yn} and
{y1, y2,…, yn}
respectively; i.e., attributes y1, y2,…, yn are common,
A additionally has attributes x1, x2, …, xm , B has no
other attributes. Then division of A by B
A DIVIDEDBY B
is a relation with heading x1, x2, …, xm and
body consisting of the set of all tuples
{X1:x1,X2:x2,…,Xm:xm } such that a tuple
{X1:x1,X2:x2,…,Xm:xm, Y1:y1,Y2:y2,…,Yn:yn } appears in A
for all tuples {Y1:y1,Y2:y2,…,Yn:yn } appearing in B.
◦ A is called dividend(DEND) and B is called divisor(DOR)
a1 b1
a2 b2
a1 b2
b1
b2
a1
DIVIDEDBY
 DIVISION
◦ Take two relations, one binary and one unary, and
returns a relation consisting of all values of one
attribute of binary relation that match(in the other
attribute) all the values in the unary relation.
ID
S1
S2
BID Subject
S1 Math
S2 Urdu
S1 English
S1 Urdu
A
Subject
Urdu
A DIVIDEBY B
 Some Facts
◦ If A and B have no common attribute names in
common, A JOIN B is equal to A TIMES B
◦ In EQUIJOIN, if one of the two attributes having
common values is eliminated (may be through
projection), then the result is equal to the natural join.
◦ JOIN is both commutative and associative i.e.,
A JOIN B = B JOIN A
A JOIN (B JOIN C) = (A JOIN B) JOIN C
 Consider relations A and B such that
◦ Cardinalty of A= 12, Degree of A= 4
◦ Cardinalty of B= 10, Degree of B= 4
◦ Cardinalty of A UNION B = 20
what would be the value of following:
◦ Cardinalty and Degree of A INTERSECT B
◦ Cardinalty and Degree of A TIMES B
◦ Degree of A UNION B
◦ Cardinalty and Degree of A MINUS B
◦ Cardinalty and Degree of B MINUS A

More Related Content

Similar to Relational algebra

group_linkage@www15
group_linkage@www15group_linkage@www15
group_linkage@www15
Pei Li
 
Slowly changing dimensions informatica
Slowly changing dimensions informatica Slowly changing dimensions informatica
Slowly changing dimensions informatica
InformaticaTrainingClasses
 
07.02 relational union intersection
07.02 relational union intersection07.02 relational union intersection
07.02 relational union intersection
Bishal Ghimire
 
Chapter3 the relational data model and the relation database constraints part2
Chapter3 the relational data model and the relation database constraints part2Chapter3 the relational data model and the relation database constraints part2
Chapter3 the relational data model and the relation database constraints part2
eidah20
 

Similar to Relational algebra (16)

NMEC RD_UNIT 1.ppt
NMEC RD_UNIT 1.pptNMEC RD_UNIT 1.ppt
NMEC RD_UNIT 1.ppt
 
group_linkage@www15
group_linkage@www15group_linkage@www15
group_linkage@www15
 
Relational model
Relational modelRelational model
Relational model
 
Normalization.ppt
Normalization.pptNormalization.ppt
Normalization.ppt
 
Dbms chapter vii
Dbms chapter viiDbms chapter vii
Dbms chapter vii
 
Dbms relational data model and sql queries
Dbms relational data model and sql queries Dbms relational data model and sql queries
Dbms relational data model and sql queries
 
Slowly changing dimensions informatica
Slowly changing dimensions informatica Slowly changing dimensions informatica
Slowly changing dimensions informatica
 
uniT 4 (1).pptx
uniT 4 (1).pptxuniT 4 (1).pptx
uniT 4 (1).pptx
 
Relational model
Relational modelRelational model
Relational model
 
Relational Algebra and it's Operations pptx
Relational Algebra and it's Operations pptxRelational Algebra and it's Operations pptx
Relational Algebra and it's Operations pptx
 
Normalization.riz
Normalization.rizNormalization.riz
Normalization.riz
 
07.02 relational union intersection
07.02 relational union intersection07.02 relational union intersection
07.02 relational union intersection
 
Chapter3 the relational data model and the relation database constraints part2
Chapter3 the relational data model and the relation database constraints part2Chapter3 the relational data model and the relation database constraints part2
Chapter3 the relational data model and the relation database constraints part2
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Unit 04 dbms
Unit 04 dbmsUnit 04 dbms
Unit 04 dbms
 
IT6701 Information Management Unit-I
IT6701 Information Management Unit-IIT6701 Information Management Unit-I
IT6701 Information Management Unit-I
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Relational algebra

  • 2.  RDBMS is an enhancement to DBMS. It is a system in which: The data is perceived by the user as tables; and  The operators (or operations) at the user's disposal include atleast RESTRICT (SELECT), PROJECT and JOIN. Relational Database Management System (RDBMS) Recall: Database Management System (DBMS) •DBMS is an application, which holds user data permanently and then provide different operations on this data e.g., retrieval of data, insertion of data, updation of data etc. •It is a computerized system whose overall purpose is to maintain information and to make that information available on demand.  Relational Database: A database in which data is perceived as tables
  • 3.  It is a prescription for a way of representing data by means of tables and a prescription for a way of manipulating such a representation using some operators.  It is an abstract theory of data that is based on certain aspects of mathematics like set theory and predicate logic.  The principles of relational model were originally laid down in 1969-70 by Dr. E.F.Codd.
  • 4.  Relation  Tuple, Attribute  Cardianlity, Degree  Domain tuples attributes ID Name Age Department S1 Ahmad 23 Sales S2 Salman 34 Marketing S3 Karim 21 Sales S4 Tariq 29 Admin S5 Sadiq 32 Sales Employee Relation Cardinality Degree
  • 5.  There are no duplicate tuples  Tuples are unordered  Attributes are unordered  All attributes have atomic values  The sequence of attributes within a relation is of no importance.
  • 6.  Candidate key ◦ General definition:  A set of attributes which can uniquely identify each row in the table ◦ Relational Model Definition:  Let R be a relation. Then candidate key for R is a subset of the set of attributes of R say K, such that: 1 Uniqueness Property: no two distinct tuples of R have the same value for K. 2 Irreducibility property: no proper subset of K has the uniqueness propert. Possible Candidate Keys: • ID • NIC ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371
  • 7.  Primary key ◦ is a unique identifier for the table, that is , a column or column combination with the property that, at any given time, no two rows of the table contain same value in that column or column combination. ◦ One of the candidate keys  Alternate Keys ◦ All candidate keys other than primary key are called alternate keys ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 Primary Key: ID Alternate Key: NIC
  • 8.  Foreign key ◦ General definition:  A set of attributes in a table whose values are taken from the values of candidate key of some other table ◦ Relational Model Definition:  Let R2 be a relation. Then a foreign key in R2 is a subset is a subset of the set of attributes of R2, say FK, such that: 1 there exists a base relation R1 (R1 and R2 not necessarily distinct) with a candidate key CK and 2 for all time, each value of FK in the current value of R2 is identical to the value of CK in some tuple in the current value of R1 ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 Department Location Sales Floor1 Marketing Floor3 Admin Floor5 Same values
  • 9.  Referenced tuple or Target tuple  Referencing relation  Referenced relation or Target relation  Simple key vs Composite key ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 Department Location Sales Floor1 Marketing Floor3 Admin Floor5 Target tuple Referencing relation Target relation
  • 10.  Integrity constraints ensure that the data is accurate. Every attribute has an associated domain, there are constraints(called domain constraints) that form restrictions on the set of values allowed for the attributes of relations.  Null represents a value for an attribute that is currently unknown or is not applicable for this tuple.
  • 11.  In a base relation no attribute of a primary key can be null.  A primary key is a minimal identifier that is used to identify tuples uniquely. This means that no subset of primary key is sufficient to provide unique identification of tuples.
  • 12.  If a foreign key exists in a relation, the foreign key value must match a candidate key value of some tuple in its home relation.  Possible cases: update,delete
  • 13.  Operators ◦ Relational Algebra consists of eight operators:  Four traditional set operators: UNION, INTERSECTION, DIFFERENCE, CARTESIAN PRODUCT  Four special relational operators: RESTRICTION, PROJECTION, JOIN, DIVISION  Operands ◦ Relations are the operands used in Relational Algebra  Closure property of Relations ◦ output from some relational operator is always a relation.
  • 14.  UNION ◦ the union of two type-compatible relations A and B (A UNION B) is a relation with the same heading as each of A and B and with a body consisting of the set of all tuples belonging to A and B. ◦ relations must have same heading ◦ same tuples are included once A B
  • 15.  UNION ◦ Returns a relation consisting of all tuples appearing in either or both relations ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S4 Tariq 29 Admin 245-71-325370 A ID Name Age Department NIC S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 B ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 A UNION B
  • 16.  INTERSECTION ◦ the intersection of two type-compatible relations A and B (A INTERSECT B) is a relation with the same heading as each of A and B and with a body consisting of the set of all tuples belonging to both A and B. ◦ relations must have same heading A B
  • 17.  INTERSECTION ◦ Returns a relation consisting of all tuples appearing in both of two specified relations ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S4 Tariq 29 Admin 245-71-325370 A ID Name Age Department NIC S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 B ID Name Age Department NIC S4 Tariq 29 Admin 245-71-325370 A INTERSECT B
  • 18.  DIFFERENCE ◦ the difference between two type-compatible relations A and B, in that order (A MINUS B) is a relation with the same heading as each of A and B and with a body consisting of the set of all tuples belonging to A and not to B. ◦ relations must have same heading ◦ direction of operation does matter A B
  • 19.  DIFFERENCE ◦ Returns a relation consisting of all tuples appearing in the first and not in the second of two specified relations ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S4 Tariq 29 Admin 245-71-325370 A ID Name Age Department NIC S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 B ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 A MINUS B ID Name Age Department NIC S3 Karim 21 Sales 255-79-256369 S5 Sadiq 32 Sales 245-68-345371 B MINUS A
  • 20.  PRODUCT (CARTESIAN PRODUCT) ◦ the product of two relations A and B with no common attribute, (A TIMES B) is a relation with a same heading as set of all attributes in each of A and B and with a body consisting of the set of all tuples such that each resulting tuple is combination of a tuple from A and a tuple from B. ◦ Cardinality of resulting relation is equal to the product of the cardinalities of A and B. ◦ Degree of resulting relation is equal to the sum of the degrees of A and B.  Returns a relation consisting of all possible tuples that are a combination of two tuples, one from each of two specified relations.
  • 21.  PRODUCT (CARTESIAN PRODUCT) ID Name Age S1 Ahmad 23 S2 Salman 34 S4 Tariq 29 A Department NIC Sales 255-79-256369 Admin 245-71-325370 Sales 245-68-345371 B ID Name Age Department NIC S1 Ahmad 23 Sales 255-79-256369 S1 Ahmad 23 Admin 245-71-325370 S1 Ahmad 23 Sales 245-68-345371 S2 Salman 34 Sales 255-79-256369 S2 Salman 34 Admin 245-71-325370 S2 Salman 34 Sales 245-68-345371 S4 Tariq 29 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S4 Tariq 29 Sales 245-68-345371 A TIMES B
  • 22.  Q1: Identify the possible candidate keys in the given relation?  Q2: There are 4 candidate keys identified in a relation each having 12 elements. What would be the cardinality of the relation. Also write the lowest possible value of degree for that relation.  Q3: Explain meaning of RESTRICTED and CASCADE in case of update for a foreign key.  Q4: Give an example of repeating group from keeping in view the relation given above. ID LAST_NAME FIRST_NAME USERID SALARY 1 Patel Ralph rpatel 795 2 Dancs Betty bdancs 860 3 Biri Ben bbiri 1100 4 Newman Chad cnewman 750 5 Ropeburn Audry aropebur 1550
  • 23.  Commutative ◦ Union, Intersect and Times are Commutative:  A UNION B is equal to B UNION A  A INTERSECT B is equal to B INTERSECT A  A TIMES B is equal to B TIMES A ◦ MINUS is not Commutative i.e.,  A MINUS B is not equal to B MINUS A  Associative ◦ Union, Intersect and Times are Associative:  (A UNION B) UNION C is equal to A UNION (B UNION C)  (A INTERSECT B) INTERSECT C is equal to A INTERSECT (B INERSECT C)  (A TIMES B) TIMES C is equal to A TIMES (B TIMES C) ◦ MINUS is not Associative i.e.,  (A MINUS B) MINUS C is not equal to A MINUS (B MINUS C)
  • 24.  RESTRICTION ◦ is actually abbreviation for θ-restriction, where “θ” stands for any simple scalar comparison operator(=,<,>,=) ◦ θ-restriction of relation A on attributes X and Y( in that order) A where X θ Y is a relation with the same heading as A and with a body consisting of the set of all tuples of A such that the condition “X θ Y” evaluates true for those tuples. ◦ X and Y must be defined on the same domain ◦ operator must make sense for that domain A
  • 25.  RESTRICTION ◦ Returns a relation consisting of all tuples from a specified relation that satisfy a specified condition. ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 A ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S3 Karim 21 Sales 255-79-256369 S5 Sadiq 32 Sales 245-68-345371 A WHERE Department=“Sales”
  • 26.  PROJECTION ◦ the projection of relation A on X,Y,….,Z (where each of X,Y,….,Z is an attribute of A) is a relation with heading {X,Y,…,Z } and body consisting of the set of all tuples {X:x,Y:y,…,Z:z } such that a tuple appears in A with X- value x, Y-value y,…., Z-value z. ◦ projection yields a vertical subset of relation A
  • 27.  PROJECTION ◦ Returns a relation consisting of all tuples that remain as (sub) tuples in a specified relation after specified attributes have been eliminated ID Name Age Department NIC S1 Ahmad 23 Sales 245-77-245367 S2 Salman 34 Marketing 234-66-245368 S3 Karim 21 Sales 255-79-256369 S4 Tariq 29 Admin 245-71-325370 S5 Sadiq 32 Sales 245-68-345371 A Name Age Department Ahmad 23 Sales Karim 21 Sales Sadiq 32 Sales A where Department=“Sales” [Name,Department] Name Ahmad Salman Karim Tariq Sadiq A [Name]
  • 28.  JOIN(NATURAL JOIN) ◦ Let relations A and B have headings [X,…,Y] and [X,…,Z] respectively, then A JOIN B is a relation with heading [X,…,Y,…,Z] and with a body consisting of the set of all tuples [X:x,Y:y,Z:z] such that a tuple appears in A with X-value x and Y- value y and in B with X-value x and Z-value z. ◦ there should be some common attribute(s) a1 b1 a2 b2 a3 b2 b1 c1 b2 c2 b3 c3 a1 b1 c1 a2 b2 c2 a3 b2 c2 JOIN
  • 29.  JOIN(NATURAL JOIN) ◦ Returns a relation consisting of all possible tuples that are combination of two tuples, one from each of the two specified relations, such that two tuples contributing to any given combination have a common value for the common attributes (and that value appears just once) ID Name S1 Ahmad S2 Salman S3 Karim A ID Subject S1 Math S2 Urdu S1 English B ID Name Subject S1 Ahmad Math S1 Ahmad English S2 Salman Urdu A JOIN B
  • 30.  θ-JOIN ◦ Let relations A and B have no attribute names in common (as in Cartesian Product), and let θ be as defined in restriction. Then the θ- JOIN of relation A on attribute X with relation B on attribute Y is defined by the result of the expression (A TIMES B) where X θ Y ◦ it is a relation with same heading as Cartesian Product of A and B and with a body consisting of the set of all those tuples belonging to that Cartesian Product of that evaluate true for X θ Y. ◦ if θ is “equals”, the θ-JOIN is called an EQUIJOIN. a1 b1 a2 b2 a3 b2 b1 c1 b2 c2 b3 c3 a2 b2 b1 c1 a3 b2 b1 c1 θ-JOIN
  • 31.  θ- JOIN ID Name S1 Ahmad S2 Salman S3 Karim A ID Subject S1 Math S2 Urdu S1 English B IDA Name IDB Subject S2 Salman S1 Math S2 Salman S1 English S3 Karim S1 Math S3 Karim S1 English S3 Karim S2 Urdu (A TIMES B) where IDA > IDB IDA Name IDB Subject S1 Ahmad S1 Math S1 Ahmad S1 English S2 Salman S2 Urdu (A TIMES B) where IDA = IDB
  • 32.  DIVISION ◦ Let relations A and B have headings { x1, x2, …, xm, y1, y2,…, yn} and {y1, y2,…, yn} respectively; i.e., attributes y1, y2,…, yn are common, A additionally has attributes x1, x2, …, xm , B has no other attributes. Then division of A by B A DIVIDEDBY B is a relation with heading x1, x2, …, xm and body consisting of the set of all tuples {X1:x1,X2:x2,…,Xm:xm } such that a tuple {X1:x1,X2:x2,…,Xm:xm, Y1:y1,Y2:y2,…,Yn:yn } appears in A for all tuples {Y1:y1,Y2:y2,…,Yn:yn } appearing in B. ◦ A is called dividend(DEND) and B is called divisor(DOR) a1 b1 a2 b2 a1 b2 b1 b2 a1 DIVIDEDBY
  • 33.  DIVISION ◦ Take two relations, one binary and one unary, and returns a relation consisting of all values of one attribute of binary relation that match(in the other attribute) all the values in the unary relation. ID S1 S2 BID Subject S1 Math S2 Urdu S1 English S1 Urdu A Subject Urdu A DIVIDEBY B
  • 34.  Some Facts ◦ If A and B have no common attribute names in common, A JOIN B is equal to A TIMES B ◦ In EQUIJOIN, if one of the two attributes having common values is eliminated (may be through projection), then the result is equal to the natural join. ◦ JOIN is both commutative and associative i.e., A JOIN B = B JOIN A A JOIN (B JOIN C) = (A JOIN B) JOIN C
  • 35.  Consider relations A and B such that ◦ Cardinalty of A= 12, Degree of A= 4 ◦ Cardinalty of B= 10, Degree of B= 4 ◦ Cardinalty of A UNION B = 20 what would be the value of following: ◦ Cardinalty and Degree of A INTERSECT B ◦ Cardinalty and Degree of A TIMES B ◦ Degree of A UNION B ◦ Cardinalty and Degree of A MINUS B ◦ Cardinalty and Degree of B MINUS A