Docsity

SQL and Relational Calculi: Understanding Tuples and Domains - Prof. David Kuijt, Study notes of Principles of Database Management

Partial preview of the text

Download SQL and Relational Calculi: Understanding Tuples and Domains - Prof. David Kuijt and more Study notes Principles of Database Management in PDF only on Docsity!

Recap of Feb 11: SQL

  • “*” is an abbreviation for all attributes in the from list
  • implicit tuple variables; table names as tuple variables
  • Set operators: union, intersect, minus, contains, exists, in, not
  • Insert command and semantics
  • Delete command and semantics
  • Update command and semantics
  • Aggregate functions: avg, min, max, sum, count
  • group by clause
  • having clause

SQL: Multiple Group Bys

Example: using relation emp(ss#, ename, dept, cat, sal)

Count the employees and average monthly salary for each

employee category in each department

select dept, cat, count(*), avg(sal)/ from emp group by dept, cat

SQL: Multiple Group By

Select … from emp

group by dname, cat

note that some dname/cat groups are empty.

SQL: Examples on Having

Find the average salary of employees under 30 for each

department with more than 10 such employees

select dname, avg(sal) from emp where age<30 (employee age under 30) group by dname (group by department) having 10<count(*) (group size > 10)

SQL: Examples on Having

Find categories of employees whose average salary exceeds

that of programmers

select cat, avg(sal) from emp group by cat having avg(sal)> (select avg(sal) from emp where cat=“programmer”)

SQL: Examples on Having

Find all departments with at least two clerks

select dname from emp where job=“clerk” group by dname having count(*) >= 2

SQL: Examples

For each boat, find the number of sailors of rating >7 who

have reserved this boat

select bid, bname, count(s.sid) from sailors s, boats b, reserve r where s.sid=r.sid and r.bid=b.bid and rating> group by b.bid

SQL: Examples

For each red boat, find the number of sailors who have

reserved this boat

select bid, bname, count(s.sid) from sailors s, boats b, reserve r where s.sid=r.sid and r.bid=b.bid group by b.bid having colour=“red”

And Now, For Something

Completely Different...

  • The recent SQL material largely covers chapter 4, at least

sections 4.1 through 4.6 and some of 4.9.

  • Earlier we examined Relational Algebra, covering sections

3.1 through 3.

  • Now we leave chapter 4 and head back to examine sections

3.6 and 3.7, covering Relational Calculi

  • based upon predicate calculus
  • non-procedural query languages (descriptive rather than prescriptive)
  • we will examine two relational calculi: tuple calculus and domain calculus

Tuple Calculus

Query: {t | P(t)} P: is a predicate associated with some relation R

t: is a tuple variable ranging over the relation R t[A]: is the value of attribute A in tuple t

  • students in CMSC 424
    • {t | t? enroll? t[course#] = CMSC424}
  • students in CMSC 424 conforming with the CMSC- prerequisite - {t | t? enroll?? s? enroll? t[course#] = CMSC424? s[course#] = CMSC420? t[ss#] = s[ss#]}

Tuple Calculus

  • Formulas
    • an Atom is a Formula
    • If P and Q are Formulas, then so are (P),? P, P? Q, P? Q, and P? Q
    • If P(t) is a Formula, then so are? t P(t) and? t P(t)
  • Equivalences -? (P? Q)?? P?? Q -? (P? Q)?? P?? Q -? t P(t)?? (? t (? P(t))) -? t P(t)?? (? t (? P(t)))

Tuple Calculus

  • Safety
    • Math is too powerful; we can easily phrase expressions that describe infinite sets {t | t? enroll}
    • These expressions are called unsafe
    • When we are dealing with finite sets, unsafe expressions happen in expressions that involve negation (? )
    • We can avoid this problem by using an entirely positive (non- negated) scope as the first operand of any conjunction where we use negation. The first operand establishes the scope and the second one filters the established scope. {t | t? enroll? t[course#]? CMSC-420}

Domain Calculus

  • Atoms
    • < x 1 , x 2 , x 3 , ..., xn>? R
    • x? y where x,y are domain variables and ?? {? ,? ,? ,? ,? ,? }
    • x? c where c is a constant
  • Formulas
    • an atom is a formula
    • If P and Q are formulas, then so are (P),? P, P? Q, P? Q, and P? Q
    • If P(x) is a formula and x is a domain variable, then? x P(x) and ? x P(x) are also formulas

Domain Calculus

  • Queries are of the form:

{< x 1 , x 2 , x 3 , ..., xn> | P( x 1 , x 2 , x 3 , ..., xn) }

  • Examples

{<ss#, course#, semester> | Enroll(ss#, course#, semester)}

{<x, y, z> | Enroll(x, y, z)? y = CMSC-424}