0% found this document useful (0 votes)
9 views20 pages

Reational Calculus

Uploaded by

kushal4493
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views20 pages

Reational Calculus

Uploaded by

kushal4493
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

RELATIONAL CALCULUS

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF CSE(AI&ML,DS), ANITS

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
RELATIONAL CALCULUS
•There is an alternate way of formulating queries known as Relational Calculus.
•Relational calculus is a non-procedural query language.
•In the non-procedural query language, the user is concerned with the details of how to obtain
the end results.
•The relational calculus tells what to do but never explains how to do.
•It is based on Predicate calculus, a name derived from branch of symbolic language.
•A predicate is a truth-valued function with arguments.
•On substituting values for the arguments, the function result in an expression called a
proposition. It can be either true or false.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
TYPES

REF :
https://www.google.com/search?sca_esv=f28b3b0bae9cc4f2&sca_upv=1&rlz=1C1YQLS_enIN865IN865&q=relational+calculus+in+dbms+types&udm=2&fbs=AEQNm0A633aO
WMcGwo5EkodWqZWQxPIwflRJ4Hu3ORx2YNN2hMyLXvg7YutBzzEkH5jrqRZVNqsK5Bw5ddbAfF-taybgSSQV7ogjWSUk63vkbvL-
4s0QFTv9AVIjLjAdl81dlvIDh7wjHADoJgyfun64VS1oZk63MZ4CuN969CmCBgbDdyPb3dIMBY8Q5HWwZYfbmqMC8CRwmCAAaqPcuJe2VhY9NIkmw&sa=X&ved=2ahUKEwiP97fAm
9iIAxVL3jgGHarrKwsQtKgLegQIERAB&biw=1366&bih=607&dpr=1#vhid=mzZ496PBuRL4EM&vssid=mosaic

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
QUANTIFIERS
Many of the calculus expressions involves the use of Quantifiers. There are two types of
quantifiers:
•Universal Quantifiers: The universal quantifier denoted by ∀ is read as for all which means that
in a given set of tuples exactly all tuples satisfy a given condition.
•Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all which means
that in a given set of tuples there is at least one occurrences whose value satisfy a given
condition.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
QUANTIFIERS
Existential Quantifier (∃)
The existential quantifier (∃) is used to find tuples where there exists at least one other tuple
satisfying a certain condition.
Example:
Consider the relation Employee(EID, Name, Salary, DeptID).
Query: Find the names of employees who work in the 'HR' department.
SQL: select * from employees where DeptID = ‘HR’
TRC: {t.Name | ∃t (Employee(t) ∧ t.DeptID = 'HR')}
• t represents a tuple from the Employee relation.
• The predicate t.DeptID = 'HR' ensures that the tuple is from the 'HR' department.
• The existential quantifier (∃) ensures that at least one such tuple (employee) exists.
PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF
CSE(AI&ML,DS), ANITS
QUANTIFIERS
Universal Quantifier (∀)
The universal quantifier (∀) is used when you want to ensure that a condition holds true for all tuples.
Example:
Consider the relation Student(SID, Name, Age).
Query: Find students whose age is greater than or equal to every other student's age.
SQL: SELECT Name FROM Student WHERE Age = (SELECT MAX(Age) FROM Student);
TRC: {t.Name | ∃t (Student(t) ∧ ∀s (Student(s) → t.Age ≥ s.Age))}
• t represents a tuple from the Student relation.
• s represents another tuple from the same Student relation.
• The universal quantifier (∀) ensures that for all tuples s, the age of t is greater than or equal to the age of s.
• The result retrieves the name of the student who is older or the same age as everyone else

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
TUPLE RELATIONAL CALCULUS (TRC)
•It is a non-procedural query language which is based on finding a number of tuple variables also
known as range variable for which predicate holds true.
•It describes the desired information without giving a specific procedure for obtaining that
information.
•The tuple relational calculus is specified to select the tuples in a relation.
•In TRC, filtering variable uses the tuples of a relation.
•The result of the relation can have one or more tuples.
•Syntax:
•{T | P (T)} or {T | Condition (T)}
◦ T is the resulting tuples
◦ P(T) is the condition used to fetch T.
◦ The curly braces {} are used to indicate that the expression is a set of tuples.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
TUPLE RELATIONAL CALCULUS (TRC)
Example: Student(SID, Name, Age, Dept)
Query: Find the names of students who are older than 20 years.
SQL: select name from students where Age>20
TRC: {t.Name | ∃t (Student(t) ∧ t.Age > 20)}
• t is a tuple variable.
• Student(t) refers to a tuple t in the Student relation.
• The condition t.Age > 20 filters tuples where the age is greater than 20.
• The result returns t.Name of those students.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
TUPLE RELATIONAL CALCULUS (TRC)
Example: Consider two relations:
Student(SID, Name, Dept)
Department(Dept, DeptName)
Query: Find the names of students along with their department names.
SQL: select name,dept from Student,Department where Student.Dept=Department.Dept
select name,dept from Student S,Department D where S.Dept=D.Dept
TRC: {(t1.Name, t2.DeptName) | ∃t1 ∃t2 (Student(t1) ∧ Department(t2) ∧ t1.Dept = t2.Dept)}
• t1 refers to tuples in the Student relation.
• t2 refers to tuples in the Department relation.
• The condition t1.Dept = t2.Dept ensures the join between the two relations.
• The result returns a tuple with t1.Name and t2.DeptName

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
TUPLE RELATIONAL CALCULUS (TRC)
Example: Consider a relation
Employee(EID, Name, Salary, DeptID)
Query: Find the employees who work in the IT department.
SQL: select name from Employee where DeptID = “IT”
TRC: {t.Name | ∃t (Employee(t) ∧ t.DeptID = 'IT')}
• Employee(t) filters the Employee relation.
• The condition t.DeptID = 'IT' selects employees from the IT department.
• The result returns t.Name of those employees.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
TUPLE RELATIONAL CALCULUS (TRC)
Example: Consider a relation
Course(CID, CName, Instructor)
Instructor(ID, Name)
Query: Find the names of instructors who teach a course called 'Database Systems’.
SQL: select I.name from instructor I where I.id IN ( select C.instructor from course C where C.cname
= 'Database Systems ' );
IN(1,2,3) or IN(“IT”,”CSE”)
TRC: {t2.Name | ∃t1 ∃t2 (Course(t1) ∧ Instructor(t2) ∧ t1.CName = 'Database Systems' ∧
t1.Instructor = t2.ID)}
• The outer query retrieves the Name of instructors (I.Name).
• The inner query selects the Instructor IDs from the Course table where the course name is 'Database Systems'.
• The outer query then checks if an instructor’s ID is in the result of the inner query (i.e., those who teach the
'Database Systems' course).

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
•Domain Relational Calculus is a non-procedural query language equivalent in power to Tuple
Relational Calculus.
•Domain Relational Calculus provides only the description of the query but it does not provide
the methods to solve it.
•In DRC, you specify what you want, not how to retrieve it.
•It focuses on the use of domain variables that take values from an attribute's domain (i.e., a
specific column in the database table).

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Basic Syntax of DRC:
DRC queries are expressed as { <domain variable list> | <condition> }
Here:
◦ <domain variable list> defines the attributes you want to retrieve.
◦ <condition> specifies the conditions that must be satisfied.

Example:
• { < x1, x2, x3, ..., xn > | P (x1, x2, x3, ..., xn ) }
• where, < x1, x2, x3, …, xn> represents resulting domains variables and
• P (x1, x2, x3, …, xn ) represents the condition or formula equivalent to the Predicate calculus.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Example : Consider a relation
RollNo Name Age Dept
Students(Roll,No,Name,Age,Dept)
1 Alice 21 CSE
2 Bob 22 ECE
Query: Find the details of students in the 'CSE' department. 3 Carol 23 CSE
4 Dave 24 EEE
SQL: SELECT * FROM Students WHERE Dept = 'CSE';
DRC: { <R,N,A,D> | <R,N,A,D> ∈ Students ∧ D = 'CSE' }
• <R, N, A, D> represents a tuple of four domain variables: R (RollNo), N (Name), A (Age), and D (Dept).
• <R, N, A, D> ∈ Students means that the tuple exists in the Students relation.
• The condition D = 'CSE' ensures that only students in the 'CSE' department are retrieved.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Example : Consider a relation RollNo Name Age Dept
Students(Roll,No,Name,Age,Dept) 1 Alice 21 CSE
2 Bob 22 ECE
3 Carol 23 CSE
Query: Find the roll numbers of students who are older than 22.
4 Dave 24 EEE
SQL: SELECT RollNo FROM Students WHERE Age > 22;
DRC: { R | ∃ N, A, D ( <R, N, A, D> ∈ Students ∧ (A > 22)) }
• R, N, A, and D are domain variables representing RollNo, Name, Age, and Dept, respectively.
• <R, N, A, D> ∈ Students means that there exists a tuple in the Students relation.
• The condition (A > 22) filters tuples where the age (A) is greater than 22.

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Example : Consider a relation RollNo Name Age Dept
Students(Roll,No,Name,Age,Dept) 1 Alice 21 CSE
2 Bob 22 ECE
3 Carol 23 CSE
Query: Find the names of students who are older than 20 and
4 Dave 24 EEE
belong to the 'ECE' department.
SQL: SELECT Names FROM Students WHERE Age > 2 and Dept = ‘EEE’;
DRC: { <N> | ∃ R, A, D ( <R, N, A, D> ∈ Students ∧ A > 20 ∧ D = 'EEE' ) }

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Example : Consider a relation RollNo Name Age Dept
Students(RollNo,Name,Age,Dept) 1 Alice 21 CSE
2 Bob 22 ECE
3 Carol 23 CSE
Query: Find the roll numbers of students who are not in the
4 Dave 24 EEE
'EEE' department.
SQL: SELECT RollNo FROM Students WHERE Dept != ‘EEE’;
DRC: { <R> | ∃ N, A, D ( <R, N, A, D> ∈ Students ∧ D ≠ 'EEE' ) }

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Example : Consider a relation RollNo Name Age Dept
Students(RollNo,Name,Age,Dept) 1 Alice 21 CSE
2 Bob 22 ECE
3 Carol 23 CSE
Query: Find the roll numbers of students who are in the
4 Dave 24 EEE
‘CSE‘ or ‘ECE’ department.
SQL: SELECT RollNo FROM Students WHERE Dept = ‘CSE’ OR Dept = ‘ECE’ ;
DRC: { <R> | ∃ N,A,D (< R,N, A, D> ∈ Students ∧ (D = 'CSE' ∨ D = 'ECE') )}

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Example : Consider a relation RollNo Name Age Dept
Students(Roll,No,Name,Age,Dept) 1 Alice 21 CSE
2 Bob 22 ECE
3 Carol 23 CSE
Query: Find the roll numbers of students who are older than
4 Dave 24 EEE
the average age of all students.
SQL: SELECT RollNo FROM Students WHERE Age > (SELECT AVG(Age) FROM Students);
DRC: { <R> | ∃ N, A, D ( <R, N, A, D> ∈ Students ∧ A > (∃ AvgA (AvgA = AVG(A) FROM
Students))) }

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS
DOMAIN RELATIONAL CALCULUS (DRC)
Example : Consider a relation RollNo Name Age Dept
Students(Roll,No,Name,Age,Dept) 1 Alice 21 CSE
2 Bob 22 ECE
3 Carol 23 CSE
Query: Find the names of students who are in the same
4 Dave 24 EEE
department as the student with RollNo = 1.
SQL: SELECT Name FROM Students WHERE Dept = (SELECT Dept FROM Students WHERE
RollNo = 1);
DRC: { <N> | ∃ R, A, D ( <R, N, A, D> ∈ Students ∧ ∃ R1, N1, A1 ( <R1, N1, A1, D> ∈ Students ∧
R1 = 1 )) }

PREPARED BY; MRS. G.V.GAYATHRI, DEPT. OF


CSE(AI&ML,DS), ANITS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy