SlideShare une entreprise Scribd logo
1  sur  44
RELATIONAL ALGEBRA
AND TUPLE RELATIONAL
CALCULUS
9/27/2020 1
Relational Algebra
• Procedural Query Languages.
• Operation to be performed on exciting
relation to derive the result relation.
• Unary operation.
– Select
– Project
– Rename
• Binary operation.
– Cartesian product
– Union
– Set difference
• Additional Operation
– Intersection
– Join
– Division
– Assignment
9/27/2020 3
9/27/2020 4
9/27/2020 5
9/27/2020 6
9/27/2020 7
9/27/2020 8
9/27/2020 9
9/27/2020 10
9/27/2020 11
9/27/2020 12
9/27/2020 13
9/27/2020 14
9/27/2020 15
9/27/2020 16
9/27/2020 17
9/27/2020 18
9/27/2020 19
9/27/2020 20
9/27/2020 21
9/27/2020 22
Division Operation (Cont.)
 Property
 Let q – r  s
 Then q is the largest relation satisfying q x s  r
 Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S  R
r  s = R-S (r) –R-S ( (R-S (r) x s) – R-S,S(r))
To see why
 R-S,S(r) simply reorders attributes of r
 R-S(R-S (r) x s) – R-S,S(r)) gives those tuples t in
R-S (r) such that for some tuple u  s, tu  r.
9/27/2020 23
Division Operation – Example
Relations r, s: B
1
2
A B











1
2
3
1
1
1
3
4
6
1
2
r  s:
A


r
s
9/27/2020 24
Another Division Example
A B








a
a
a
a
a
a
a
a
C D








a
a
b
a
b
a
b
b
E
1
1
1
1
3
1
1
1
Relations r, s:
r  s:
D
a
b
E
1
1
A B


a
a
C


r
s
9/27/2020 25
Assignment Operation
 The assignment operation () provides a convenient way to express
complex queries.
 Write query as a sequential program consisting of
 a series of assignments
 followed by an expression whose value is displayed as a result
of the query.
 Assignment must always be made to a temporary relation variable.
 Example: Write r  s as
temp1  R-S (r)
temp2  R-S ((temp1 x s) – R-S,S (r))
result = temp1 – temp2
 The result to the right of the  is assigned to the relation variable on
the left of the .
 May use variable in subsequent expressions.
9/27/2020 26
Extended Relational-Algebra-
Operations
 Generalized Projection
 Outer Join
 Aggregate Functions
9/27/2020 27
Generalized Projection
 Extends the projection operation by allowing
arithmetic functions to be used in the projection
list.
 F1, F2, …, Fn(E)
 E is any relational-algebra expression
 Each of F1, F2, …, Fn are are arithmetic
expressions involving constants and attributes
in the schema of E.
 Given relation credit-info(customer-name,
limit, credit-balance), find how much more
each person can spend:
customer-name, limit – credit-balance (credit-info)
9/27/2020 28
Aggregate Functions and Operations
 Aggregation function takes a collection of values and
returns a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
 Aggregate operation in relational algebra
G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)
 E is any relational-algebra expression
 G1, G2 …, Gn is a list of attributes on which to group
(can be empty)
 Each Fi is an aggregate function
 Each Ai is an attribute name
9/27/2020 29
Aggregate Operation –
Example
 Relation r: A B








C
7
7
3
10
g sum(c) (r) sum-C
27
9/27/2020 30
Aggregate Operation –
Example
 Relation account grouped by
branch-name:
branch-name g sum(balance) (account)
branch-name account-number balance
Perryridge
Perryridge
Brighton
Brighton
Redwood
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
branch-name balance
Perryridge
Brighton
Redwood
1300
1500
700
9/27/2020 31
Aggregate Functions (Cont.)
 Result of aggregation does not have a
name
 Can use rename operation to give it a
name
 For convenience, we permit renaming
as part of aggregate operation
branch-name g sum(balance) as sum-balance (account)
9/27/2020 32
Outer Join
 An extension of the join operation that avoids loss of
information.
 Computes the join and then adds tuples form one
relation that does not match tuples in the other
relation to the result of the join.
 Uses null values:
 null signifies that the value is unknown or does
not exist
 All comparisons involving null are (roughly
speaking) false by definition.
 Will study precise meaning of comparisons
with nulls later
9/27/2020 33
Outer Join – Example
 Relation loan
 Relation borrower
customer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
3000
4000
1700
loan-number amount
L-170
L-230
L-260
branch-name
Downtown
Redwood
Perryridge
9/27/2020 34
Outer Join – Example
 Inner Join
loan Borrower
loan-number amount
L-170
L-230
3000
4000
customer-name
Jones
Smith
branch-name
Downtown
Redwood
Jones
Smith
null
loan-number amount
L-170
L-230
L-260
3000
4000
1700
customer-namebranch-name
Downtown
Redwood
Perryridge
 Left Outer Join
loan Borrower
loan Borrower
9/27/2020 35
Outer Join – Example
 Right Outer Join
loan borrower
loan borrower
 Full Outer Join
loan-number amount
L-170
L-230
L-155
3000
4000
null
customer-name
Jones
Smith
Hayes
branch-name
Downtown
Redwood
null
loan-number amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer-name
Jones
Smith
null
Hayes
branch-name
Downtown
Redwood
Perryridge
null
loan borrower
9/27/2020 36
Tuple Relational Calculus
 A nonprocedural query language, where each query
is of the form
{t | P (t) }
 It is the set of all tuples t such that
predicate P is true for t
 t is a tuple variable, t[A] denotes the value of tuple t
on attribute A
 t  r denotes that tuple t is in relation r
 P is a formula similar to that of the
predicate calculus
9/27/2020 37
Predicate Calculus Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., , , , , , )
3. Set of connectives: and (), or (v)‚ not ()
4. Implication (): x  y, if x is true, then y is true
5. Set of quantifiers:
  t  r (Q(t))  ”there exists” a tuple in t in relation r
such that predicate Q(t) is true
 t r (Q(t)) Q is true “for all” tuples t in relation r
s[x] Ѳ u[y] : s, u are tuple variables. x,y are attributes
and Ѳ is comparison operator.
s[x] Ѳ c , where c is a constant.
9/27/2020 38
Banking Example
 branch (branch-name, branch-city, assets)
 customer (customer-name, customer-
street, customer-city)
 account (account-number, branch-name,
balance)
 loan (loan-number, branch-name, amount)
 depositor (customer-name, account-
number)
 borrower (customer-name, loan-number)
Example Queries
 Find all the tuples in loan
relation
 {t | t  loan}
 Find those tuples for which
loans of greater than 1200
 {t | t  loan  t [amount]  1200}
9/27/2020 39
9/27/2020 40
Example Queries
Find the loan number for each loan of an amount greater
than $1200
The set of all tuples t such that there exists a tuple s in relation
loan for which the values t and s for the loan-number attributes
are equal and the value of s for the amount attribute is greater
than $1200
Notice that a relation on schema [loan-number] is implicitly
defined by the query
{t |  s loan (t[loan-number] = s[loan-number]  s [amount]  1200)}
9/27/2020 41
Example Queries
 Find the names of all customers having a loan, an
account, or both at the bank
{t | s  borrower( t[customer-name] = s[customer-name])
 u  depositor( t[customer-name] = u[customer-name])
 Find the names of all customers who have a loan and
an account at the bank
{t | s  borrower( t[customer-name] = s[customer-name])
 u  depositor( t[customer-name] = u[customer-name])
9/27/2020 42
Safety of Expressions
 It is possible to write tuple calculus expressions that
generate infinite relations.
 For example, {t |  t r} results in an infinite relation if
the domain of any attribute of relation r is infinite
 To guard against the problem, we restrict the set of
allowable expressions to safe expressions.
 An expression {t | P(t)} in the tuple relational calculus is
safe if every component of t appears in one of the
relations, tuples, or constants that appear in P
 NOTE: this is more than just a syntax condition.
 E.g. { t | t[A]=5  true } is not safe --- it defines
an infinite set with attribute values that do not
appear in any relation or tuples or constants in P.
9/27/2020 43
Null Values
 It is possible for tuples to have a null value, denoted by null, for
some of their attributes
 null signifies an unknown value or that a value does not exist.
 The result of any arithmetic expression involving null is null.
 Aggregate functions simply ignore null values
 Is an arbitrary decision. Could have returned null as result
instead.
 We follow the semantics of SQL in its handling of null
values
 For duplicate elimination and grouping, null is treated like any
other value, and two nulls are assumed to be the same
 Alternative: assume each null is different from each other
 Both are arbitrary decisions, so we simply follow SQL
9/27/2020 44
Null Values
 Comparisons with null values return the special truth value
unknown
 If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
 Three-valued logic using the truth value unknown:
 OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
 AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
 NOT: (not unknown) = unknown
 In SQL “P is unknown” evaluates to true if predicate P
evaluates to unknown
 Result of select predicate is treated as false if it evaluates to
unknown

Contenu connexe

Tendances

Avl trees
Avl treesAvl trees
Avl trees
ppreeta
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize output
Syed Zaid Irshad
 

Tendances (20)

Unit 5 composite datatypes
Unit 5  composite datatypesUnit 5  composite datatypes
Unit 5 composite datatypes
 
ER Modeling and Introduction to RDBMS
ER Modeling and Introduction to RDBMSER Modeling and Introduction to RDBMS
ER Modeling and Introduction to RDBMS
 
Fd & Normalization - Database Management System
Fd & Normalization - Database Management SystemFd & Normalization - Database Management System
Fd & Normalization - Database Management System
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Avl trees
Avl treesAvl trees
Avl trees
 
Codd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management SystemsCodd's Rules for Relational Database Management Systems
Codd's Rules for Relational Database Management Systems
 
User defined Function in SQL
User defined Function in SQLUser defined Function in SQL
User defined Function in SQL
 
Using single row functions to customize output
Using single row functions to customize outputUsing single row functions to customize output
Using single row functions to customize output
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
 
Sql commands
Sql commandsSql commands
Sql commands
 
Join
JoinJoin
Join
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & Calculus
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
 
Advanced sql
Advanced sqlAdvanced sql
Advanced sql
 
Mysql
MysqlMysql
Mysql
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
MySQL JOINS
MySQL JOINSMySQL JOINS
MySQL JOINS
 

Similaire à relational algebra Tuple Relational Calculus - database management system

3. Relational Models in DBMS
3. Relational Models in DBMS3. Relational Models in DBMS
3. Relational Models in DBMS
koolkampus
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMS
koolkampus
 

Similaire à relational algebra Tuple Relational Calculus - database management system (20)

Relational algebra operations
Relational algebra operationsRelational algebra operations
Relational algebra operations
 
Lllll
LllllLllll
Lllll
 
3. Relational Models in DBMS
3. Relational Models in DBMS3. Relational Models in DBMS
3. Relational Models in DBMS
 
relational model in Database Management.ppt.ppt
relational model in Database Management.ppt.pptrelational model in Database Management.ppt.ppt
relational model in Database Management.ppt.ppt
 
Details of RDBMS.ppt
Details of RDBMS.pptDetails of RDBMS.ppt
Details of RDBMS.ppt
 
Database system by VISHAL PATIL
Database system by VISHAL PATILDatabase system by VISHAL PATIL
Database system by VISHAL PATIL
 
Relation model part 1
Relation model part 1Relation model part 1
Relation model part 1
 
3.ppt
3.ppt3.ppt
3.ppt
 
Dbms module ii
Dbms module iiDbms module ii
Dbms module ii
 
4. SQL in DBMS
4. SQL in DBMS4. SQL in DBMS
4. SQL in DBMS
 
Cs501 rel algebra
Cs501 rel algebraCs501 rel algebra
Cs501 rel algebra
 
SQL PPT.ppt
SQL PPT.pptSQL PPT.ppt
SQL PPT.ppt
 
1643 y є r relational calculus-1
1643 y є r  relational calculus-11643 y є r  relational calculus-1
1643 y є r relational calculus-1
 
ch2
ch2ch2
ch2
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
 
Lec02
Lec02Lec02
Lec02
 
Relation Algebra
Relation AlgebraRelation Algebra
Relation Algebra
 
Module 2 - part i
Module   2 - part iModule   2 - part i
Module 2 - part i
 
relational algebra
relational algebrarelational algebra
relational algebra
 

Plus de Surya Swaroop

Plus de Surya Swaroop (20)

Pie chart - qualitative aptitude
Pie chart - qualitative aptitude Pie chart - qualitative aptitude
Pie chart - qualitative aptitude
 
Simplification and data interpretation table
Simplification and data interpretation tableSimplification and data interpretation table
Simplification and data interpretation table
 
Quantitative aptitude table data interpretation
Quantitative aptitude  table data interpretationQuantitative aptitude  table data interpretation
Quantitative aptitude table data interpretation
 
Environmental engineering text book
Environmental engineering text bookEnvironmental engineering text book
Environmental engineering text book
 
Development of surfaces
Development of surfacesDevelopment of surfaces
Development of surfaces
 
Elasticity
ElasticityElasticity
Elasticity
 
Design and Drawing of Steel Structures
Design and Drawing of Steel StructuresDesign and Drawing of Steel Structures
Design and Drawing of Steel Structures
 
Design & Drawing of Steel Structures
 Design & Drawing of Steel Structures Design & Drawing of Steel Structures
Design & Drawing of Steel Structures
 
design drawing of steel structures
design drawing of steel structuresdesign drawing of steel structures
design drawing of steel structures
 
design-and-drawing Steel structures
design-and-drawing Steel structuresdesign-and-drawing Steel structures
design-and-drawing Steel structures
 
design drawing steel structures
design drawing steel structuresdesign drawing steel structures
design drawing steel structures
 
Integration material
Integration material Integration material
Integration material
 
Probability
ProbabilityProbability
Probability
 
Other transform dip
Other transform dipOther transform dip
Other transform dip
 
deadlock and locking - dbms
deadlock and locking -  dbmsdeadlock and locking -  dbms
deadlock and locking - dbms
 
transaction management, concept & State
transaction management, concept & Statetransaction management, concept & State
transaction management, concept & State
 
relational database
relational databaserelational database
relational database
 
er question dbms
er question dbmser question dbms
er question dbms
 
dbms er model
dbms er modeldbms er model
dbms er model
 
data base
data basedata base
data base
 

Dernier

Dernier (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

relational algebra Tuple Relational Calculus - database management system

  • 1. RELATIONAL ALGEBRA AND TUPLE RELATIONAL CALCULUS 9/27/2020 1
  • 2. Relational Algebra • Procedural Query Languages. • Operation to be performed on exciting relation to derive the result relation. • Unary operation. – Select – Project – Rename • Binary operation. – Cartesian product – Union – Set difference • Additional Operation – Intersection – Join – Division – Assignment
  • 22. 9/27/2020 22 Division Operation (Cont.)  Property  Let q – r  s  Then q is the largest relation satisfying q x s  r  Definition in terms of the basic algebra operation Let r(R) and s(S) be relations, and let S  R r  s = R-S (r) –R-S ( (R-S (r) x s) – R-S,S(r)) To see why  R-S,S(r) simply reorders attributes of r  R-S(R-S (r) x s) – R-S,S(r)) gives those tuples t in R-S (r) such that for some tuple u  s, tu  r.
  • 23. 9/27/2020 23 Division Operation – Example Relations r, s: B 1 2 A B            1 2 3 1 1 1 3 4 6 1 2 r  s: A   r s
  • 24. 9/27/2020 24 Another Division Example A B         a a a a a a a a C D         a a b a b a b b E 1 1 1 1 3 1 1 1 Relations r, s: r  s: D a b E 1 1 A B   a a C   r s
  • 25. 9/27/2020 25 Assignment Operation  The assignment operation () provides a convenient way to express complex queries.  Write query as a sequential program consisting of  a series of assignments  followed by an expression whose value is displayed as a result of the query.  Assignment must always be made to a temporary relation variable.  Example: Write r  s as temp1  R-S (r) temp2  R-S ((temp1 x s) – R-S,S (r)) result = temp1 – temp2  The result to the right of the  is assigned to the relation variable on the left of the .  May use variable in subsequent expressions.
  • 26. 9/27/2020 26 Extended Relational-Algebra- Operations  Generalized Projection  Outer Join  Aggregate Functions
  • 27. 9/27/2020 27 Generalized Projection  Extends the projection operation by allowing arithmetic functions to be used in the projection list.  F1, F2, …, Fn(E)  E is any relational-algebra expression  Each of F1, F2, …, Fn are are arithmetic expressions involving constants and attributes in the schema of E.  Given relation credit-info(customer-name, limit, credit-balance), find how much more each person can spend: customer-name, limit – credit-balance (credit-info)
  • 28. 9/27/2020 28 Aggregate Functions and Operations  Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values  Aggregate operation in relational algebra G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)  E is any relational-algebra expression  G1, G2 …, Gn is a list of attributes on which to group (can be empty)  Each Fi is an aggregate function  Each Ai is an attribute name
  • 29. 9/27/2020 29 Aggregate Operation – Example  Relation r: A B         C 7 7 3 10 g sum(c) (r) sum-C 27
  • 30. 9/27/2020 30 Aggregate Operation – Example  Relation account grouped by branch-name: branch-name g sum(balance) (account) branch-name account-number balance Perryridge Perryridge Brighton Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 750 700 branch-name balance Perryridge Brighton Redwood 1300 1500 700
  • 31. 9/27/2020 31 Aggregate Functions (Cont.)  Result of aggregation does not have a name  Can use rename operation to give it a name  For convenience, we permit renaming as part of aggregate operation branch-name g sum(balance) as sum-balance (account)
  • 32. 9/27/2020 32 Outer Join  An extension of the join operation that avoids loss of information.  Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join.  Uses null values:  null signifies that the value is unknown or does not exist  All comparisons involving null are (roughly speaking) false by definition.  Will study precise meaning of comparisons with nulls later
  • 33. 9/27/2020 33 Outer Join – Example  Relation loan  Relation borrower customer-name loan-number Jones Smith Hayes L-170 L-230 L-155 3000 4000 1700 loan-number amount L-170 L-230 L-260 branch-name Downtown Redwood Perryridge
  • 34. 9/27/2020 34 Outer Join – Example  Inner Join loan Borrower loan-number amount L-170 L-230 3000 4000 customer-name Jones Smith branch-name Downtown Redwood Jones Smith null loan-number amount L-170 L-230 L-260 3000 4000 1700 customer-namebranch-name Downtown Redwood Perryridge  Left Outer Join loan Borrower loan Borrower
  • 35. 9/27/2020 35 Outer Join – Example  Right Outer Join loan borrower loan borrower  Full Outer Join loan-number amount L-170 L-230 L-155 3000 4000 null customer-name Jones Smith Hayes branch-name Downtown Redwood null loan-number amount L-170 L-230 L-260 L-155 3000 4000 1700 null customer-name Jones Smith null Hayes branch-name Downtown Redwood Perryridge null loan borrower
  • 36. 9/27/2020 36 Tuple Relational Calculus  A nonprocedural query language, where each query is of the form {t | P (t) }  It is the set of all tuples t such that predicate P is true for t  t is a tuple variable, t[A] denotes the value of tuple t on attribute A  t  r denotes that tuple t is in relation r  P is a formula similar to that of the predicate calculus
  • 37. 9/27/2020 37 Predicate Calculus Formula 1. Set of attributes and constants 2. Set of comparison operators: (e.g., , , , , , ) 3. Set of connectives: and (), or (v)‚ not () 4. Implication (): x  y, if x is true, then y is true 5. Set of quantifiers:   t  r (Q(t))  ”there exists” a tuple in t in relation r such that predicate Q(t) is true  t r (Q(t)) Q is true “for all” tuples t in relation r s[x] Ѳ u[y] : s, u are tuple variables. x,y are attributes and Ѳ is comparison operator. s[x] Ѳ c , where c is a constant.
  • 38. 9/27/2020 38 Banking Example  branch (branch-name, branch-city, assets)  customer (customer-name, customer- street, customer-city)  account (account-number, branch-name, balance)  loan (loan-number, branch-name, amount)  depositor (customer-name, account- number)  borrower (customer-name, loan-number)
  • 39. Example Queries  Find all the tuples in loan relation  {t | t  loan}  Find those tuples for which loans of greater than 1200  {t | t  loan  t [amount]  1200} 9/27/2020 39
  • 40. 9/27/2020 40 Example Queries Find the loan number for each loan of an amount greater than $1200 The set of all tuples t such that there exists a tuple s in relation loan for which the values t and s for the loan-number attributes are equal and the value of s for the amount attribute is greater than $1200 Notice that a relation on schema [loan-number] is implicitly defined by the query {t |  s loan (t[loan-number] = s[loan-number]  s [amount]  1200)}
  • 41. 9/27/2020 41 Example Queries  Find the names of all customers having a loan, an account, or both at the bank {t | s  borrower( t[customer-name] = s[customer-name])  u  depositor( t[customer-name] = u[customer-name])  Find the names of all customers who have a loan and an account at the bank {t | s  borrower( t[customer-name] = s[customer-name])  u  depositor( t[customer-name] = u[customer-name])
  • 42. 9/27/2020 42 Safety of Expressions  It is possible to write tuple calculus expressions that generate infinite relations.  For example, {t |  t r} results in an infinite relation if the domain of any attribute of relation r is infinite  To guard against the problem, we restrict the set of allowable expressions to safe expressions.  An expression {t | P(t)} in the tuple relational calculus is safe if every component of t appears in one of the relations, tuples, or constants that appear in P  NOTE: this is more than just a syntax condition.  E.g. { t | t[A]=5  true } is not safe --- it defines an infinite set with attribute values that do not appear in any relation or tuples or constants in P.
  • 43. 9/27/2020 43 Null Values  It is possible for tuples to have a null value, denoted by null, for some of their attributes  null signifies an unknown value or that a value does not exist.  The result of any arithmetic expression involving null is null.  Aggregate functions simply ignore null values  Is an arbitrary decision. Could have returned null as result instead.  We follow the semantics of SQL in its handling of null values  For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same  Alternative: assume each null is different from each other  Both are arbitrary decisions, so we simply follow SQL
  • 44. 9/27/2020 44 Null Values  Comparisons with null values return the special truth value unknown  If false was used instead of unknown, then not (A < 5) would not be equivalent to A >= 5  Three-valued logic using the truth value unknown:  OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown  AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown  NOT: (not unknown) = unknown  In SQL “P is unknown” evaluates to true if predicate P evaluates to unknown  Result of select predicate is treated as false if it evaluates to unknown