You are on page 1of 39

Formal Query Languages

Mathematically defined Query Languages


Section 6 of the textbook

©Flemming Schmidt and Anne Haxthausen

10. Formal Query Languages 02170 Database Systems 1


Mathematical Foundations

 Formal Relational Query Languages


• Relational Algebra
• Tuple Calculus
• Domain Calculus

 Their Mathematical Foundations


• Set Theory
• Algebra
• Logic

10. Formal Query Languages 02170 Database Systems 2


Set Theory
Georg Cantor 1845-1918

 Set Theory
• In mathematics set theory is one of the most fundamental concepts,
and its formalization was a major event in the history of mathematics.
• Set theory is a foundation for most mathematical disciplines.
• A set is a collection of distinct elements considered as a whole, and
element membership of a set is the fundamental concept.
• Sets can be represented in different ways:
• As a list of individual elements: {e1, e2, e3}
Ordering and repetitions have no importance, e.g.
{e1, e2, e3} = {e2, e1, e3} = {e1, e2, e2, e3}
• By a description of element properties: {e ϵ Nat | e < 4 v e = 9} (= {0, 1, 2, 3, 9})
in such a way, that it can be determined whether a given element e is a member of
the set or not.
• Set operations: Union, Difference, Intersection, Cartesian Product …

10. Formal Query Languages 02170 Database Systems 3


Algebra
Diophantus of Alexandria 200-284 and Muhammad ibn Musa al-Khwarizmi 780-850

 Algebra
• A fundamental discipline in mathematics that arose from the idea
that one can perform arithmetic operations on non numerical objects
called variables. (Arithmetic: 3+4 = 4+3 ; Algebra: X+Y = Y+X).
• In its most general form, algebra is the study of mathematical
symbols and the rules for manipulating these symbols.

10. Formal Query Languages 02170 Database Systems 4


Calculus
Aristotle 384–322 BC , … Chrysippus of Soli 279-206 BC

 Logic
• Propositional Calculus is a formal system, where formulas are
propositions over some variables, like P(x), which for each possible
value of x has a truth value of either True or False. The formulas may
contain sub-formulas combined by logical operators like /\ (“and”), \/
(“or”), and => (“implies”).
Example:
P(x) ≜ x > 7 /\ x < 10; P(x) is True for x = 8 and for x = 9, and False for all other values
of x.
• Predicate Calculus extends propositional calculus by allowing formulas
to be quantified with the Existential Quantifier or the Universal
Quantifier:
∃x(P(x)) Reads: There exists some x for which P(x) is True
∀x(P(x)) Reads: For all x the proposition P(x) is True

10. Formal Query Languages 02170 Database Systems 5


Formal Relational Query Languages
 Given a simple Relation Schema and SQL Query
• Relation Schema R(A,B,C,D)
SELECT B, A FROM R WHERE D>100

 Formal Relational Query Languages?


Relation, Result Attributes
• Relational Algebra: use Relation Variables like R and Selection Predicate!
∏B, A(D>100(R))

• Tuple Calculus: use Tuple Variables like t and r


{t | rR(t[B]=r[B]  t[A]=r[A]  r[D]100) } i.e. t ≡ t(B, A)

• Domain Calculus: use Domain Variables like a,b,c,d


{ <b, a> | c,d(<a,b,c,d>R  d100) }

10. Formal Query Languages 02170 Database Systems 6


Relational Algebra
 Relational Algebra
• Was introduced by Edgar F. Codd while working for IBM in order to provide a
procedural query language for the relational model.
• Is a procedural language which provides a selection of operations to generate
an answer to a query. Queries are defined in terms of how to compute an
answer.
• Relational Algebra is the mathematical basis for the language SQL, and the
foundation of Query Processor Engines in some Database Systems.

 Basic Operations
• Is a minimal set of operations.
 Derived Operations
• Additional operations that can be derived from the Basic operations, not for
achieving more expressive power, but for expressive convenience.
 Extended Operations
• Give more expressive power - can’t be derived from the Basic operations.

10. Formal Query Languages 02170 Database Systems 7


Basic Operations in Relational Algebra
• Below R, S are relations, R has attributes A1,…, An, P(A1,…, An) is a selection
predicate (a proposition) over A1,…, An, and {Ai, …, Aj} ⊆ {A1,…, An}
Relational Meaning: a relation consisting of
• Algebra

Selection P(A1,…, An)(R) Those tuples t of R, for which P(t) is True: { t | t  R  P(t) }
Projection Ai, …, Aj (R) Delete columns not listed in Ai, …, Aj, then delete duplicate tuples:
{ (Ai, …, Aj) | (A1,…, An)  R}
Set Union RS The union of tuples of R and S: { t | t  R v t  S }
Only defined when R and S are compatible.
Set Difference R–S Those tuples of R that are not in S: { t | t  R  t  S}
Only defined when R and S are compatible.
Cartesian Product RxS All combined tuples: { (r1,…, rn, s1,…, sm) | (r1,…, rn)  R  (s1,…, sm)  S }
Rename S(R) Rename of R to S.
S(L2)(R(L1)) Rename R to S and rename attributes in L1 to attributes in L2
• Compatible means that R and S must have the same number of attributes with
matching, or at least compatible, domains.
• Note: Selection corresponds to SQL’s where clause (and not SQL’s select
clause).

10. Formal Query Languages 02170 Database Systems 8


Relational Algebra Basic Operations in SQL
• Below R, S are relations, R has attributes A1,…, An, P(A1,…, An) is a selection
predicate (a proposition) over A1,…, An, and {Ai, …, Aj} ⊆ {A1,…, An}

Basic Relational Algebra Equivalent SQL Statement


Operations
Selection P(A1,…, An)(R) SELECT * FROM R WHERE P(A1,…, An)
Projection Ai, …, Aj (R) SELECT Ai, …, Aj FROM R
Set Union RS (SELECT * FROM R) UNION (SELECT * FROM S)
Set Difference R–S (SELECT * FROM R) EXCEPT (SELECT * FROM S)
Cartesian RxS SELECT * FROM R JOIN S
Product
Rename S(R) SELECT * FROM R AS S
S(Bi, …, Bj)(R(Ai, …, Aj)) SELECT Ai AS Bi , … , Aj AS Bj FROM R AS S

10. Formal Query Languages 02170 Database Systems 9


Basic Operations in Relational Algebra
Using the University Schema

10.1.1 Selection Operation


Find Instructor rows for instructors in the DeptName=‘Physics’  Salary>90000(Instructor)
physics department earning more than 90000.
10.1.2 Projection Operation
InstName, InstID(Instructor)
Find name and IDs of all instructors
10.1.3 Set Union
InstName, InstID(Instructor)  StudName, StudID(Student)
Find name and IDs of all instructors and
students.
10.1.4 Set Difference InstID(Instructor) – InstID(Teaches)
Find instructor IDs for instructors that have
not taught any courses yet.
10.1.5 Cartesian Product Instructor x Student
Combine each instructor with each student
10.1.6 Rename Salary(Instructor) –
Find salary of the instructor with the highest Instructor.Salary
salary. (Use T as a temporary relation). (Instructor.Salary < T.Salary (Instructor x T(Instructor)))

10. Formal Query Languages 02170 Database Systems 10


Derived Operations in Relational Algebra
• Below R and S are relations, and {A1,…, An} is the intersection of their attributes.
• Θ is a predicate over attributes in the union of attributes of R and S.
Derived Relational Meaning: a relation consisting of
Operations Algebra

Natural Join R⋈S Select those tuples from R x S, which have same values for A1,…, An, then
delete duplicate attributes (A1,…, An) from S.
Left Outer Join R⟕S Adds to R ⋈ S: tuples of R that do not match any tuples of S,
concatenated with null values for non common attributes in the S
relation.
Right Outer Join R⟖S Adds to R ⋈ S: tuples of S that do not match any tuples of R,
concatenated with null values for non common attributes in the R
relation.

Full Outer Join R ⟗S (R ⟕ S) ∪ (R ⟖ S)

Theta join R ⋈θ S σθ(R × S)


Set Intersection RS Tuples that are members of both R and S: { t | t  R  t  S }
Only defined when R and S are compatible.
Assignment SR Relation variable S is assigned the value of relation R.

10. Formal Query Languages 02170 Database Systems 11


Deriving Operations from Basic Operations
 Natural Join can be derived from Cartesian Product, Selection and
Projection
• Schemas R(A,B,C,D), S(B,D,E) and Natural Join Schema T(A,B,C,D,E)
• T ≡ R ⋈ S ≡ ∏R.A,R.B,R.C,R.D,S.E (R.B=S.B  R.D=S.D (R x S))

 Left Outer Join can be derived from


Cartesian Product, Selection, Projection, Set Difference and Set Union
• Schemas R(A,B,C), S(A,C,D,E) and Left Outer Join Schema T(A,B,C,D,E)
• T ≡ R ⟕ S ≡ (R ⋈ S)  ((R – ∏R.A,R.B,R.C (R ⋈ S)) x {(Null, Null)} )
(Here R – ∏R.A,R.B,R.C (R ⋈ S) contains those tuples of R that did not match any tuple in S.)
R ⋈ S can be further decomposed using Cartesian Product, Selection & Projection

 Set Intersection can be derived from Set Difference


• R  S ≡ R – (R – S)

10. Formal Query Languages 02170 Database Systems 12


Relational Algebra Derived Operations in SQL
• Below R and S are relations.

Derived Relational Equivalent SQL Statement


Operations Algebra
Natural Join R⋈S SELECT * FROM R NATURAL JOIN S

Left Outer Join R⟕S SELECT * FROM R NATURAL LEFT OUTER JOIN S

Right Outer Join R⟖S SELECT * FROM R NATURAL RIGHT OUTER JOIN S

Full Outer Join R⟗S SELECT * FROM R NATURAL FULL OUTER JOIN S
Set Intersection RS (SELECT * FROM R) INTERSECT (SELECT * FROM S)
Assignment SR CREATE TABLE S SELECT * FROM R

10. Formal Query Languages 02170 Database Systems 13


Derived Operations in Relational Algebra
10.1.7 Natural Join
Find instructor names and course IDs they InstName, CourseID(Instructor ⋈ Teaches)
have taught. (Join on attribute InstID).
10.1.8 Natural Join
Find the names of instructors in the Comp. Sci. InstName, Title (DeptName=‘Comp. Sci.’ (Instructor ⋈ Teaches
⋈ Course))
department together with the course titles of
the courses that the instructors teach.
10.1.9 Left Outer Join StudName, InstID (Student ⟕ Advisor)
Find student names and their advisor IDs.
10.1.10 Set Intersection CourseID (Semester=‘Fall’ Λ StudyYear=2009 (Section)) 
Find courses that were taught both in Fall CourseID (Semester=‘Spring’ Λ StudyYear=2010 (Section))
2009 and in Spring 2010.
10.1.11 Assignment
Make a relation called Teacher from Instructor. Teacher  Instructor

10. Formal Query Languages 02170 Database Systems 14


Extended Relational Equivalent SQL
Operations Algebra
Generalized ∏E1, E2, … , En(R) SELECT E1, E2, … , En FROM R
Projection
Aggregate AVG, MIN, MAX, SUM, COUNT AVG, MIN, MAX, SUM, COUNT
Functions
Aggregate G F1(A1), … , Fn(An) (R) SELECT F1(A1), … , Fn(An) FROM R
Operation AXG F1(A1), … , Fn(An) (R) SELECT AX, F1(A1), … , Fn(An) FROM R GROUP BY AX

 E1, E2, …, En are arithmetic expressions involving constants and attribute names of R.
 AVG, MIN, MAX, SUM and COUNT are aggregate functions taking a multiset of values as input
and returning a single value.
 Fi is an aggregate function, and Ai is an attribute name of R.
 AX is a list of attributes on which to group; Can be empty.

10. Formal Query Languages 02170 Database Systems 15


Extended Operations in Relational Algebra
10.1.12 Generalized Projection
For all instructors increase salary with 3% and InstID, InstName, DeptName, Salary*1.03/12 (Instructor)
show monthly instead of yearly salary.
10.1.13 Aggregate Operation
GMAX(Salary), MIN(Salary), AVG(Salary), SUM(Salary) (Instructor)
Find the maximum, minimum, average and
sum of all instructor salaries.
10.1.14 Aggregate Operation with Grouping
DeptName GMAX(Salary), AVG(Salary) (Instructor)
Find the maximum and average salary in each
department.

10. Formal Query Languages 02170 Database Systems 16


Using Relational Algebra for Language Semantics

 Relational Algebra can be used to specify the meaning of relational


languages
 Example:
• The meaning of
SELECT A1,…, An FROM R1,…, Rn WHERE P
is
A1, …, An(P(R1 × … × Rn ))

10. Formal Query Languages 02170 Database Systems 17


Tuple Calculus
 Tuple Calculus
• Introduced by Edgar F. Codd in order to provide a declarative query language
based on mathematical logic for the relational model.
• Tuple Calculus, in contrast to Relational Algebra, is a non procedural or
declarative language that describes the answer, without giving specific
procedures for how to obtain the answer.
• Tuple Calculus is the mathematical basis for the query language QUEL .

 Tuple Calculus versus Relational Algebra


• Tuple Calculus restricted to Safe Expressions (i.e. expressions with a finite
number of tuples in the result), is relationally complete: it is equivalent to
Relational Algebra in expressive power with regard to the basic operations and
derived operations.
• Tuple Calculus can be extended to handle arithmetic expressions as well as
aggregation operations. However, that is outside the scope of this lecture.

10. Formal Query Languages 02170 Database Systems 18


Tuple Calculus
 Each query is of the form: { t | P(t) } (some books allow a derived form {t[Ai], …, t[Aj]| P(t) } )
• t is a tuple variable
• P(t) is a Predicate Calculus formula in attributes t[A] of the tuple t
• t[A] (t.A in some other books) denotes the value of tuple t in attribute A
• { t | P(t) } denotes the set of those tuples t for which P(t) is true, t is a tuple
variable
 A Predicate Calculus formula P(t):
• Membership test: t R (written R(t) in some other books)
• Comparisons:
• t [A] op r[B] or t[A] op c,
where op is a comparison operator (, , , , , ), and c is a constant, t and r are tuple variables for
relations having attributes A and B, respectively.
• f1  f2 , f1 v f2 , f1  f2 ,  f1 , where f1 and f2 are formula. Note: x  y (x) v y
• Quantified expressions:
rR(Q(r)) There exists some tuples r in relation R, for which the predicate Q(r) is True
rR(Q(r)) For all tuples r in relation R, the predicate Q(r) is True
 Free and bound variables in a Predicate Calculus formula
• A tuple variable r is said to be a bound variable, if it is bound via  or  to a relation R.
• A tuple variable t is said to be a free variable, if it is NOT bound.
• For { t | P(t) } only t is allowed to be free in P(t).

10. Formal Query Languages 02170 Database Systems 19


Basic Operations of Tuple Calculus
 Equivalent Operations in Relational Algebra and Tuple Calculus, examples
Base Relational Tuple Calculus
Operations Algebra Basic Operations
Selection B>3, C=B(R) {t | tR  t[B]>3  t[C]=t[B]} Given R(A,B,C)
Projection B,A(R) {t | rR(t[B]=r[B]  t[A]=r[A])} Given R(A,B,C) then t is t(B,A)
Set Union RS {t | rR(t[A]=r[A]) v sS(t[A]=s[A]) } Given R(A) and S(A)
Set Difference R–S {t | rR(t[A]=r[A])  sS(t[A]=s[A]) } Given R(A) and S(A)
Cartesian Product RxS {t | rR(t[A]=r[A]  t[B]=r[B])  sS(t[C]=s[C]  t[D]=s[D]) }
Given R(A, B) and S(C, D)
Rename R(C,D)(R(A,B)) {t | rR(t[C]=r[A]  t[D]=r[B]) }

• Note for a query of the form {t | … tR … } all attributes of R will be kept in t.
Otherwise a projection to those attributes A for which t[A] are defined will be
made.

10. Formal Query Languages 02170 Database Systems 20


Basic Operations in Tuple Calculus
Using the University Schema

10.2.1 Selection Operation


Find Instructor rows for instructors in the {t | tInstructor  t[DeptName]=‘Physics’
 t[Salary]>90000}
physics department earning more than 90000.
10.2.2 Projection Operation
{t | rInstructor(t[InstName]=r[InstName]
Find name and IDs of all instructors  t[InstID]=r[InstID]) }
10.2.3 Set Union {t | rInstructor(
Find name and IDs of all instructors and t[Name]=r[InstName]  t[ID]=r[InstID]) v
students. sStudent(
t[Name]=s[StudName]  t[ID]=s[StudID]) }
10.2.4 Set Difference
Find instructor IDs for instructors that have {t | rInstructor(t[InstID]=r[InstID]) 
sTeaches (t[InstID]=s[InstID]) }
not taught any courses yet.
10.2.5 Cartesian Product
{t | rInstructor sStudent(
Combine each instructor with each student t[InstID]=r[InstID]  t[InstName]=r[InstName] 
t[DeptName]=r[DeptName]  t[Salary]=r[Salary] 
t[StudID]=s[StudID]  t[StudName]=s[StudName] 
t[Birth]=s[Birth]  t[DeptName]=s[DeptName] 
t[TotCredits]=s[TotCredits] ) }

10. Formal Query Languages 02170 Database Systems 21


Derived Operations of Tuple Calculus
 Equivalent Operations in Relational Algebra and Tuple Calculus
Derived Relational Tuple Calculus
Operations Algebra Derived Operations
Natural Join R⋈S {t | rR sS( r[B]=s[B]  t[A]=r[A]  t[B]=r[B]  t[C]=s[C]) }
Given R(A, B) and S(B,C)
Left Outer Join R⟕S {t | rR sS( r[B]=s[B]  t[A]=r[A]  t[B]=r[B]  t[C]=s[C] ) v
rR sS((r[B]=s[B])  t[A]=r[A]  t[B]=r[B]  t[C]=Null) }
Given R(A, B) and S(B,C)
Right Outer Join R⟖S Self study
Full Outer Join R⟗S Self study
Set Intersection RS {t | tR  tS } Given R and S are compatible
Assignment SR NA

10. Formal Query Languages 02170 Database Systems 22


Derived Operations in Tuple Calculus
10.2.7 Natural Join {t | rInstructor sTeaches(r[InstID]=s[InstID] 
Find instructor names and course IDs they t[InstName]=r[InstName]t[CourseID]=s[CourseID])}
have taught. (Join on attribute InstID).
10.2.8 Natural Join {t | rInstructor sTeaches qCourse(
Find the names of instructors in the Comp. Sci. (r[InstID]=s[InstID]  r[DeptName]=‘Comp. Sci.’ 
t[InstName]=r[InstName]  s[CourseID]=q[CourseID]
department together with the course titles of
 t[Title]=q[Title] ) }
the courses that the instructors teach.
10.2.9 Left Outer Join (skipped) Self Study
Find student names and their advisor IDs
10.2.10 Set Intersection {t |  rSection (
Find courses that were taught both in Fall t[CourseID]=r[CourseID] 
2009 and in Spring 2010. r[Semester]=‘Fall’  r[StudyYear]=2009)
)
 uSection (
t[CourseID]=u[CourseID] 
u[Semester]=‘Spring’  u[StudyYear]=2010)
)

10. Formal Query Languages 02170 Database Systems 23


Domain Calculus
 Domain Calculus
• Introduced by Michel Lacroix and Alain Pirotte in order to provide a
declarative query language based on mathematical logic for the relational
model.
• Domain Calculus, like Tuple Calculus, but in contrast to Relational Algebra, is a
non procedural or declarative language that describes the answer, without
giving specific procedures for how to obtain the answer.
• Domain Calculus is the mathematical basis for the language QBE.

 Domain Calculus versus Relational Algebra


• Domain Calculus restricted to Safe Expressions (i.e. expressions with a finite
number of tuples in the result), is equivalent to Relational Algebra in
expressive power with regard to the Basic operations and derived operations.
• Domain Calculus can be extended to handle arithmetic expressions as well as
aggregation operations. However, that is outside the scope of this lecture.

10. Formal Query Languages 02170 Database Systems 24


Domain Calculus
 Each query is of the form: {  x1, x2, … , xn  | P(x1, x2, … , xn)}
• x1, x2, … , xn represent domain variables (for attributes in the relation)
•  x1, x2, … , xn  represents a tuple
• P(x1, x2, … , xn) is a Predicate Calculus formula in the domain variables
 A Predicate Calculus formula
• Membership test:  v1, v2, … , vn  R, where R is a relation having n attributes and
each vi is either a domain variable xi or a constant c.
• Comparisons:
• xi op xj or xi op c,
where op is an operator (like , , , , , ), xi and xj are domain variables and c is a constant.
• f1  f2 , f1 v f2 , f1  f2 ,  f1 , where f1 and f2 are formula
• Quantified expressions:
 x1, … , xn (P(x1, x2, …, xn)) There exists values for x1, … , xn , for which P(x1, x2, …, xn) is true.
 x1, … , xn (P(x1, x2, …, xn)) For all values of x1, … , xn , P(x1, x2, …, xn) is true.
• Free and bound variables xi:
• Are defined in a similar (usual) way as for Tuple Calculus.
• For {  x1, x2, … , xn  | P(x1, x2, … , xn)} only x1, x2, … , xn are allowed to be free in
P(x1, x2, … , xn)

10. Formal Query Languages 02170 Database Systems 25


Basic Operations of Domain Calculus
 Equivalent Operations in Relational Algebra and Domain Calculus
Basic Relational Domain Calculus
Operations Algebra Basic Operations
Selection B>3, C=B(R) {<a,b,c> | <a,b,c>R  b>3  c=b} Given R(A,B,C)
Projection B,A(R) {<b,a> | c(<a,b,c>R) } Given R(A,B,C)
Set Union RS {<a,b,c> | <a,b,c>R v <a,b,c>S } Given R and S are compatible
Set Difference R–S {<a,b,c> | <a,b,c>R  <a,b,c>S } Given R and S are compatible
Cartesian Product RxS {<a,b,c,d> | <a,b>R  <c,d>S } Given R(A,B) and S(C,D)

10. Formal Query Languages 02170 Database Systems 26


Basic Operations in Domain Calculus
Using the University Schema

10.3.1 Selection Operation


Find Instructor rows for instructors in the {<i,n,d,s> | <i,n,d,s>Instructor  d=‘Physics’ 
s>90000}
physics department earning more than 90000.
10.3.2 Projection Operation
{<n,i> | d,s(<i,n,d,s>Instructor) }
Find name and IDs of all instructors
10.3.3 Set Union
Find name and IDs of all instructors and {<n,i> | d,s(<i,n,d,s>Instructor) v
students. b,d,t(<i,n,b,d,t>Student) }

10.3.4 Set Difference {<i> | n,d,s (<i,n,d,s>Instructor) 


Find instructor IDs for instructors that have c,sec,sem,sy (<i,c,sec,sem,sy>Teaches) }
not taught any courses yet.
10.3.5 Cartesian Product {<i,n,d,s,si,sn,b,sd,t> | (<i,n,d,s>Instructor) 
Combine each instructor with each student (<si,sn,b,sd,t>Student) }

10. Formal Query Languages 02170 Database Systems 27


Derived Operations of Domain Calculus
 Equivalent Operations in Relational Algebra and Domain Calculus
Derived Relational Domain Calculus
Operations Algebra Derived Operations
Natural Join R⋈S {<a,b,c> | <a,b>R  <b,c>S } Given R(A,B) and S(B,C)
Left Outer Join R⟕S Self Study
Right Outer Join R⟖S Self Study
Full Outer Join R⟗S Self Study
Set Intersection RS {<a,b,c> | <a,b,c>R  <a,b,c>S } Given R and S are compatible
Assignment SR NA

10. Formal Query Languages 02170 Database Systems 28


Derived Operations in Domain Calculus
10.3.7 Natural Join Find instructor names and {<n,c> | i,d,s(<i,n,d,s>Instructor 
course IDs they have taught. (Join on attribute sec,sem,y(<i,c,sec,sem,y>Teaches)) }
InstID).
10.3.8 Natural Join
{<n,t> | i,d,s(<i,n,d,s>Instructor  d=‘Comp. Sci.’ 
Find the names of instructors in the Comp. Sci.
c,sec,sem,y(<i,c,sec,sem,y>Teaches 
department together with the course titles of cr(<c,t,d,cr>Course))) }
the courses that the instructors teach.
10.3.9 Left Outer Join (skipped) Self Study
Find student names and their advisor IDs
10.3.10 Set Intersection {<ci> |
Find courses that were taught both in Fall (sec,sem,y,b,r,tsi(<ci,sec,sem,y,b,r,tsi>Section 
2009 and in Spring 2010. sem=‘Fall’  y=2009))

(sec,sem,y,b,r,tsi(<ci,sec,sem,y,b,r,tsi>Section 
sem=‘Spring’  y=2010))
Course(CourseID, Title, DeptName, Credits) }
Instructor(InstID, InstName, DeptName, Salary)
Section(CourseID, Section, Semester, StudyYear, Building,
Room, TimeSlotID)
Teaches(InstID, CourseID, SectionID, Semester, StudyYear)

10. Formal Query Languages 02170 Database Systems 29


Summary
• Mathematical Foundation
• Relational Algebra
• Tuple Calculus
• Domain Calculus

Readings
• In Database Systems Concepts please read Chapter 6.
(Remember to check the book typo list – there are several typos.)
• Pay special attention to the Summary

10. Formal Query Languages 02170 Database Systems 30


Demo Exercises

Demo Exercises clarify ideas


and concepts from the Lecture
to provide you with good
Database Skills.

Make the
Demo Exercises.

10. Formal Query Languages 02170 Database Systems 31


Formal Query Languages
10.4.1 Selection and Projection Query
Find name and IDs of all instructors in the
physics department earning more than 90000.

a) Make the query in SQL

b) Make the query in Relational Algebra

c) Make the query in Tuple Calculus

d) Make the query in Domain Calculus

10. Formal Query Languages 02170 Database Systems 32


Formal Query Languages
10.4.2 Query using aggregation
Find the number of courses provided by the
Biology department (in the university
database).

a) Make the query in SQL

b) Make the query in Relational Algebra

10. Formal Query Languages 02170 Database Systems 33


Solutions to Demo Exercises

10. Formal Query Languages 02170 Database Systems 34


Formal Query Languages
10.4.1 Selection and Projection Query a) SQL:
Find name and IDs of all instructors in the SELECT InstName, InstID FROM Instructor
physics department earning more than 90000. WHERE DeptName = ‘Physics' AND Salary >
90000;
a) Make the query in SQL
b) Relational Algebra:
b) Make the query in Relational Algebra InstName, InstID(
DeptName=‘Physics’  Salary>90000(Instructor) )
c) Make the query in Tuple Calculus
c) Tuple Calculus:
{t | rInstructor (t[InstName]=r[InstName]
d) Make the query in Domain Calculus  t[InstID]=r[InstID])  r[DeptName]=‘Physics’
 r[Salary]>90000)}

d) Domain Calculus:
{<n,i> | d,s(<i,n,d,s>Instructor 
d=‘Physics’  s>90000)}

10. Formal Query Languages 02170 Database Systems 35


Formal Query Languages
10.4.2 Query using aggregation
Find the number of courses provided by the
Biology department (in the university
database).
a) SQL:
a) Make the query in SQL SELECT COUNT(CourseID) FROM Course
WHERE DeptName = 'Biology';
b) Make the query in Relational Algebra
b) Relational Algebra:
GCOUNT(CourseID) (DeptName = ‘Biology’ (Course))

10. Formal Query Languages 02170 Database Systems 36


Exercises

Please answer all exercises


to demonstrate your
Database Skills.

Pencil, Paper & MySQL


Exercises
Solutions are available at 11:45

10. Formal Query Languages 02170 Database Systems 37


Formal Query Languages
10.5.1 Selection and Projection Query 10.5.2 Join Query
Consider the relation schema: As continuation of 10.5.1, consider the
Employee(Eid, Ename, Profession, Rate) Relation Schemas:
and consider the query: Find the employees Employee(Eid, Ename, Profession, Rate)
Eid and Ename for employees with Profession Projects(Pid, Pname)
‘Carpenter’, and whose Rate is less than 100 Staffing(Pid, Eid, Hours)
$/Hour. and consider the query:
a) Make the query in SQL. Find for each staffed project and each of the
Create the relation, populate with some employees in its staff: Pid, Pname, Eid, Ename,
example data, and make the query. Hours and Rate.
a) Make the query in SQL.
b) Make the query in Relational Algebra Create the relations, populate with some
example data, and make the query.
c) Make the query in Tuple Calculus
b) Make the query in Relational Algebra
d) Make the query in Domain Calculus
c) Make the query in Tuple Calculus

d) Make the query in Domain Calculus

10. Formal Query Languages 02170 Database Systems 38


Formal Query Languages
10.5.3 Aggregation and Grouping
Consider the University database. Find
for each course offered in autumn 2009
the number of students who have taken
that course.

a) Make the query in Relational Algebra.


b) Make the query in SQL.

10. Formal Query Languages 02170 Database Systems 39

You might also like