0% found this document useful (0 votes)
12 views

DBMS_lecture15

The document discusses relational query languages, categorizing them into imperative, functional, and declarative types, with SQL as a common example of a declarative language. It explains relational algebra, including operations like selection, projection, Cartesian product, and join, detailing how these operations are used to manipulate and retrieve data from databases. Examples illustrate the application of these operations in querying database relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DBMS_lecture15

The document discusses relational query languages, categorizing them into imperative, functional, and declarative types, with SQL as a common example of a declarative language. It explains relational algebra, including operations like selection, projection, Cartesian product, and join, detailing how these operations are used to manipulate and retrieve data from databases. Examples illustrate the application of these operations in querying database relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DBMS

Lecture 15
Relational Query Languages

A query language is a language in which a user requests information from the database.
Query languages can be categorized as: -
1. Imperative query language –
Imperative query language, the user instructs the system to perform a specific sequence of operations on the database
to compute the desired result; such languages usually have a notion of state variables, which are updated in the course
of the computation. Examples are C, Python etc.
2. Functional query language –
Functional query language, the computation is expressed as the evaluation of functions that may operate on data in the
database or on the results of other functions; functions are side-effect free, and they do not update the program state.
3. Declarative query language –
Declarative query language, the user describes the desired information without giving a specific sequence of steps or
function calls for obtaining that information; the desired information is typically described using some form of
mathematical logic.
Common declarative languages include those of database query languages (e.g., SQL)
There are a number of “pure” query languages: -
1. Relational algebra – It is a functional query language
2. Tuple relational – The tuple relational calculus and domain relational calculus are declarative.
Relational Query Languages
Relational algebra –
The relational algebra consists of a set of operations that take one or two relations as
input and produce a new relation as their result.
 Operations are:
 Unary operations
 Binary operations
 Unary operations such as the select, project, and rename operations, because they
operate on one relation.
 Binary operations such as union, Cartesian product, and set difference, because they
operate on pairs of relations.
Select Operation:
The select operation selects tuples that satisfy a given predicate. We use the
lowercase Greek letter sigma (σ) to denote selection. The predicate appears as a
subscript to σ. The argument relation is in parentheses after the σ.
Example –
1. To select those tuples of the instructor relation where the instructor is in the “Physics”
department, we write:

σdept name =“Physics” (instructor)


Relational Query Languages
2. We can find all instructors with salary greater than $90,000 by writing:

σsalary>90000 (instructor)
In general, it allow comparisons using =, ≠, <, ≤, >, and ≥ in the selection predicate.
We can combine several predicates into a larger predicate by using the connectives and (∧), or
(∨), and not (.). Example – To find the instructors in Physics with a salary greater than $90,000, we
write:

σdept name =“Physics” ∧ salary>90000 (instructor)


The selection predicate may include comparisons between two attributes.
To illustrate, consider the relation department. To find all departments whose name is the same as
their building name, we can write:

σdept name =building(department)


Project Operation:
The project operation is a unary operation that returns its argument relation, with
certain attributes left out.
Projection is denoted by the uppercase Greek letter pi (Π).
We list those attributes that we wish to appear in the result as a subscript to Π. The argument
relation follows in parentheses. We write the query to produce such a list as:

ΠID, name, salary(instructor)


Relational Query Languages
The basic version of the project operator ΠL(E) allows only attribute names to
be present in the list L. A generalized version of the operator allows
expressions involving attributes to appear in the list L. For example, we could use:

ΠID,name,salary∕12(instructor)

ΠID,name,salary∕12(instruct
or)
Relational Query Languages
Composition of Relational Operations:
Composing relational-algebra operations into relationalalgebra expressions is just like composing
arithmetic operations (such as +, −, ∗, and ÷) into arithmetic expressions.
Example - “Find the names of all instructors in the Physics department.” We write:

Πname (σdept name =“Physics” (instructor))

σdept name =“Physics” (instructor)

Πname (σdept name =“Physics” (instructor))

Notice that, instead of giving the name of a relation as the argument of the projection operation, we
give an expression that evaluates to a relation.
Relational Query Languages
Cartesian-Product Operation:
The Cartesian-product operation, denoted by a cross (×), allows us to combine information from
any two relations. We write the Cartesian product of relations r1 and r2 as r1 × r2.
r1 × r2 producing pairs (t1, t2) of tuples fromr1 and r2, the relational algebra concatenates t1 and
t2 into a single tuple.
Assume that we have n1 tuples in instructor and n2 tuples in teaches. Then, there are n1 ∗ n2
ways of choosing a pair of tuples—one tuple from each relation; so there are n1 ∗ n2 tuples in
r it means No. of rows in r1 X r2 is n1 * n2 and
No. of columns in r1 X r2 is n1 + n2.
X
(Notation of
Cartesian-Product
Operation )
Relational Query Languages
Join Operation:
The join operation allows us to combine a selection and a Cartesian product into a single operation.
Consider relations r(R) and s(S), and let θ be a predicate on attributes in the schema R ∪ S. The
join operation r ⋈θ s is defined as follows:

r ⋈θ s = σθ(r × s)
Thus, σinstructor.ID=teaches.ID(instructor × teaches) can equivalently be written as
instructor ⋈instructor.ID=teaches.ID teaches

You might also like