Download presentation
Presentation is loading. Please wait.
Published byAnissa Perkins Modified over 9 years ago
1
Relational Calculus
2
Non Procedural or Declarative Calculus has variables, constants, comparison ops, logical connectives and quantifiers. There are TWO VARIANTS of Relational Calculus TUPLE RELATIONAL CALCULUS (TRC) Variables range over (i.e., get bound to) tuples DOMAIN RELATIONAL CALCULUS (DRC) Variables range over domain elements (field/attribute values) Both TRC and DRC are simple subsets of first-order logic Expressions in the calculus are called formulas. An answer tuple is essentially an assignment of constants to variables that make the formula evaluate to true.
3
Syntax of TRC Queries Query: {T|P(T)} T is tuple variable P(T) is a formula that describes T Syntax Rel : Relation Name R, S : - Tuple Variables a : Attribute of R b : - Attribute of S op : Operator in the set {,=, ≤, ≥, ≠ } Atomic formula R ∈ Rel R.a op S.b, op is one of {,=, ≤, ≥, ≠ } R.a op constant Formula (Boolean condition) Made up of one or more atoms connected via logical operators ∧, ∨, and ¬
4
Quantifiers Existential quantifier ( ∃ ): x ∃ (f(x)) for formula f with free variable x Is true if there is some tuple which when substituted for x makes f true Universal quantifier ( ∀ ): ∀ x (f(x)) for formula f with free variable x Is true if any tuple substituted for x makes f true i.e. all tuples when substituted for x make f true
5
Semantics of TRC Queries Free variables in a formula take on the values of tuples A tuple is in the defined relation if and only if when substituted for a free variable, it satisfies (makes true) the formula Bound variable: ∀ x, ∃ x bind x – truth or falsehood no longer depends on a specific value of x Free variables: If x is not bound it is free. A variable is said to be FREE in a formula/subformula, if the (sub)formula does not contain an occurrence of a quantifier that binds it.
6
Example Relational Scheme Schemestudent (rollNo, name, degree, year, sex, deptNo, advisor) Department (deptId, name, hod, phone) professor (empId, name, sex, startYear, deptNo, phone) course (courseId, cname, credits, deptNo) enrollment (rollNo, courseId, sem, year, grade) teaching (empId, courseId, sem, year, classRoom) preRequisite(preReqCourse, courseID)
7
Example TRC queries Obtain the rollNo, name of all girl students in the Maths Dept {s.rollNo,s.name | student(s) ∧ s.sex=‘F’ ∧ ( ∃ d)(department(d) ∧ d.name=‘Maths’ ∧ d.deptId= s.deptNo)} s: free tuple variable d: existentially bound tuple variable Existentially or universally quantified tuple variables can be used on the RHS of the vertical bar to specify query conditions Attributes of free (or unbound ) tuple variables can be used on LHS of vertical bar to specify attributes required in the results
8
Example queries in TRC Determine the departments that do not have any girl students student (rollNo, name, degree, year, sex, deptNo, advisor) department (deptId, name, hod, phone) {d.name | department(d) ∧ ¬( ∃ s)(student(s) ∧ s.sex=‘F’ ∧ s.deptNo= d.deptId)
9
Example queries in TRC Obtain the names of courses enrolled by student named Mahesh {c.name | course(c) ∧ ( ∃ s) ( ∃ e) ( student(s) ∧ enrollment(e) ∧ s.name = “Mahesh” ∧ s.rollNo = e.rollNo ∧ c.courseId = e.courseId}
10
Example queries in TRC Get the names of students who have scored ‘S’ in all subjects they have enrolled. Assume that every student is enrolled in at least one course. {s.name | student(s) ∧ ( ∀ e)(( enrollment(e) ∧ e.rollNo = s.rollNo) → e.grade =‘S’)} person P with all S grades: for enrollment tuples not having her roll number, LHS is false for enrollment tuples having her roll number, LHS is true, RHS also true so the implication is true for all e tuples person Q with some non-S grades: for enrollment tuples not having her roll number, LHS is false for enrollment tuples having her roll number, LHS is true, but RHS is false for at least one tuple. So the implication is not true for at least one tuple.
11
Example queries in TRC Get the names of students who have taken at least one course taught by their advisor {s.name | student(s) ∧ ( ∃ e)( ∃ t)(enrollment(e) ∧ teaching(t) ∧ e.courseId= t.courseId ∧ e.rollNo= s.rollNo ∧ t.empId= s.advisor} Display the departments whose HODs are teaching at least one course in the current semester {d.name | department(d) ∧ ( ∃ t)(teaching(t) ∧ t.empid = d.hod ∧ t.sem= ‘odd’ ∧ t.year = ‘2008’)}
12
Domain Relational Calculus Domain Variable: A variable that ranges over the values in the domain of some attribute. DRC Query: xi - Domain variable or constant - a DRC formula The ONLY free variables in the formula are the variables among the xi, 1 ≤ i ≤ n. The result of the query is the set of all tuples a DRC formula for which the formula evaluates to true.
13
Example Sailors(sid:integer, sname:string, rating:integer, age:real) Boats(bid:integer, bname:string, color:string) Reserves(sid:integer, bid:integer, day:date) Find all sailors with a rating above 7 {(I, N, T, A) | (I, N, T, A) ∈ Sailors ∧ T > 7} Find the names of sailors who have reserved boat 103 {(N) | ∃ I, T, A((I, N, T, A) ∈ Sailors ∧ ∃ Ir, Br,D((Ir, Br,D) ∈ Reserves ∧ Ir = I ∧ Br = 103))}
14
Example Find the names of sailors who have reserved at least two boats {(N) | ∃ I, T, A((I, N, T, A) ∈ Sailors ∧ ∃ Br1, Br2,D1,D2 ((I, Br1,D1) ∈ Reserves ∧ (I, Br2,D2) ∈ Reserves ∧ Br1 ≠ Br2))} Find the names of sailors who have reserved all the boats {(N) | ∃ I, T, A((I, N, T, A) ∈ Sailors ∧ ∀( B, BN, C) ∈ Boats ( ∃( Ir, Br,D) ∈ Reserves (I = Ir ∧ Br = B)))} Find sailors who have reserved all red boats {(I, N, T, A) | (I, N, T, A) ∈ Sailors ∧ ∀( B, BN, C) ∈ Boats (C = ‘red’ → ∃ (Ir, Br, D) ∈ Reserves(I = Ir ∧ Br = B))}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.