Q1 RA Vs RC
Q1 RA Vs RC
Relational algebra
Relational algebra is a procedural query language, which takes instances of relations as input
and yields instances of relations as output. It uses operators to perform queries. An operator can
be either unary or binary. They accept relations as their input and yield relations as their output.
The relational algebra is a theoretical procedural query language which takes an instance of
relations and does operations that work on one or more relations to describe another relation
without altering the original relation(s). Thus, both the operands and the outputs are relations. So
the output from one operation can turn into the input to another operation, which allows
expressions to be nested in the relational algebra, just as you nest arithmetic operations. This
property is called closure: relations are closed under the algebra, just as numbers are closed under
arithmetic operations.
The relational algebra is a relation-at-a-time (or set) language where all tuples are controlled in
one statement without the use of a loop. There are several variations of syntax for relational algebra
commands, and you use a common symbolic notation for the commands and present it informally.
The primary operations of relational algebra are as follows:
Select
Project
Union
Set different
Cartesian product
Rename
Here σ stands for selection predicate, and r stands for relation, and p is a propositional logic
formula which may use connectors like and, or, and not.
σ predicate(R): This selection operation functions on a single relation R and describes a relation that
contains only those tuples of R that satisfy the specified condition (predicate).
Example:
σteacher = "database"(Names)
The Projection operation works on a single relation R and defines a relation that contains a
vertical subset of R, extracting the values of specified attributes and eliminating duplicates.
Produce a list of salaries for all staff, showing only the staffNo, fName, lName, and
salary details.
ΠstaffNo, fName, lName, salary(Staff)
In the below-mentioned example, the Projection operation defines a relation that contains only
the designated Staff attributes staffNo, fName, lName, and salary, in the specified order. The
result of this operation is shown in the figure below
Union Operation
For R ∪ S, The union of two relations, R and S, defines a relation that contains all the tuples of
R, or S, or both R and S, duplicate tuples being eliminated. R and S must be union-compatible.
For a union operation to be applied, the following rules must hold −
For R − S The Set difference operation defines a relation consisting of the tuples that are in
relation R, but not in S. R and S must be union-compatible.
Example:
∏ writer (Nobels) − ∏ writer (papers)
Cartesian product
For R × S, the Cartesian product operation defines a relation that is the concatenation of every
tuple of relation R with every tuple of relation S.
Example:
σwriter = 'gauravray'(Articles Χ Notes)
Join Operations
Typically, you want only combinations of the Cartesian product which satisfy certain situations,
and so you can normally use a Join operation instead of the Cartesian product operation. The
Join operation, which combines two relations to form a new relation, is one of the essential
operations in the relational algebra. There are various types of Join operation, each with subtle
differences, some more useful than others:
Theta join
Equijoin (a particular type of Theta join)
Natural join
Outer join
Semijoin
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename operation
provides database designers to rename the output relation. The rename-operation is denoted
using a small Greek letter rho (ρ).
It is written as:
ρ x (E)
Relational Calculus
Relational calculus is a non-procedural query language, and instead of algebra, it uses
mathematical predicate calculus. The relational calculus is not the same as that of differential and
integral calculus in mathematics but takes its name from a branch of symbolic logic termed as
predicate calculus. When applied to databases, it is found in two forms. These are
Tuple relational calculus which was originally proposed by Codd in the year 1972 and
Domain relational calculus which was proposed by Lacroix and Pirotte in the year 1977
In first-order logic or predicate calculus, a predicate is a truth-valued function with arguments.
When we replace with values for the arguments, the function yields an expression, called
a proposition, which will be either true or false.
Example:
For example, steps involved in listing all the employees who attend the 'Networking' Course
would be:
SELECT the tuples from COURSE relation with COURSENAME = 'NETWORKING'
To express the query 'Find the set of all tuples S such that F(S) is true,' we can write:
{S | F(S)}
Here, F is called a formula (well-formed formula, or wff in mathematical logic). For example, to
express the query 'Find the staffNo, fName, lName, position, sex, DOB, salary, and branchNo of
all staff earning more than £10,000', we can write:
{S | Staff(S) ∧ S.salary > 10000}
Example:
{t | TEACHER (t) and t.SALARY>20000}
- It implies that it selects the tuples from the TEACHER in such a way that the resulting teacher
tuples will have a salary higher than 20000. This is an example of selecting a range of values.
{t | TEACHER (t) AND t.DEPT_ID = 6}
- T select all the tuples of teachers' names who work under Department 8. Any tuple variable
with 'For All' (?) or 'there exists' (?) condition is termed as a bound variable. In the last example,
for any range of values of SALARY greater than 20000, the meaning of the condition does not
alter. Bound variables are those ranges of tuple variables whose meaning will not alter if another
tuple variable replaces the tuple variable.
In the second example, you have used DEPT_ID= 8, which means only for DEPT_ID = 8
display the teacher details. Such a variable is called a free variable. Any tuple variable without
any 'For All' or 'there exists' condition is called Free Variable.
Domain Relational Calculus
In the tuple relational calculus, you have use variables that have a series of tuples in a relation. In
the domain relational calculus, you will also use variables, but in this case, the variables take
their values from domains of attributes rather than tuples of relations. A domain relational
calculus expression has the following general format:
{d1, d2, . . . , dn | F(d1, d2, . . . , dm)} m ≥ n
where d1, d2, . . . , dn, . . . , dm stand for domain variables and F(d1, d2, . . . , dm) stands for a
formula composed of atoms.
Example:
select TCHR_ID and TCHR_NAME of teachers who work for department 8, (where suppose -
dept. 8 is Computer Application Department)
{<tchr_id, tchr_name=""> | <tchr_id, tchr_name=""> ? TEACHER Λ DEPT_ID = 10}
It is to be noted that these queries are safe. The use domain relational calculus is restricted to safe
expressions; moreover, it is equivalent to the tuple relational calculus, which in turn is similar to
the relational algebra
Summary
The basic difference between Relational Algebra and Relational Calculus is that Relational Algebra is a
Procedural language whereas, the Relational Calculus is a Non-Procedural, and instead it is a Declarative
language. ... Relational Algebra specifies the sequence in which operations have to be performed in
the query.