0% found this document useful (0 votes)
15 views37 pages

Database Chapter 6

The document provides an overview of query languages, which enable users to interact with database management systems (DBMS) to retrieve and manipulate data. It discusses various types of query languages, including relational algebra and relational calculus, detailing operations such as selection, projection, and set operations. Additionally, it introduces Query By Example (QBE) and explains the differences between procedural and declarative languages in the context of database queries.

Uploaded by

tegenefikadu91
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)
15 views37 pages

Database Chapter 6

The document provides an overview of query languages, which enable users to interact with database management systems (DBMS) to retrieve and manipulate data. It discusses various types of query languages, including relational algebra and relational calculus, detailing operations such as selection, projection, and set operations. Additionally, it introduces Query By Example (QBE) and explains the differences between procedural and declarative languages in the context of database queries.

Uploaded by

tegenefikadu91
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/ 37

QUERY LANGUAGES

CHAPTER SIX
Introduction
2

 A query language is a computer programming language that


allows users to interact with a database management system
(DBMS) to retrieve and manipulate data.

 Query languages translate human questions into commands that


the database can understand.

 It serves as an interface between users and databases, enabling


users to manage data from a database management system
(DBMS)
what is a query?
3

 A query refers to a request for data or information from a


database or a data repository system.

 This request is typically made in the form of a specific question


or command, written in a query language that the database
understands.

 Queries can be used to find, summarize, filter, combine, adjust,


delete, insert, and update data.

 They can also answer data-related questions, analyze data from


multiple tables at once, and automate data management tasks.
Relational Algebra
4
Relational Algebra is a procedural query language that consists
of a set of operations that take one or two relations as input and
produce a new relation as a result.
Fundamental Operations of Relational Algebra
Unary Operators.
Selection :- 
Projection :- 
Rename :- 
 Binary Operators.
Product (Cartesian Product) :- 
Union :- 
Difference :- –
Unary Operations
5

Select Operation
 The select operation selects a subset of tuples from a relation
instance that satisfies a given predicate (condition).

C (R)

  represents the SELECT operator,

 C is a Boolean expression of the select condition, and

 R is the relation or relational algebra expression.


Example
6
Consider the student table given below:
Regno Branch Section
1 CSE A
2 ECE B
3 CIVIL B
4 IT A

1. Display all the records of CSE branch in student table, we


will use the following command

Regno Branch Section


1 CSE A
Cont’s
7

2. To display all the records in student tables whose regno>2

Regno Branch Section


3 CIVIL B
4 IT A

3. To display the record of ECE branch section B students


… Unary Operations
8
 Project Operation

 Projection operation forms a new relation by picking certain


columns in the relation.

A (R)

  represents the PROJECT operator,

 A is a set of attributes in the relation R , and

 R is the relation or relational algebra expression.


9 Example
Consider the student table given below:
Regno Branch Section
1 CSE A
2 ECE B
3 CIVIL B
4 IT A

1. To display regno column of student table, we can use the


following command

Regno

4
Cont’s
10
2. To display branch, section column of student table, use
the following command

3. To display regno, section of ECE students, use the following


command
… Unary Operations
11

 Rename Operation

 The renaming operator can be used to explicitly rename


resulting relations of an expression.

S(A1, A2, … An) (R)

  represents the RENAME operator,

 S is a name for the new relation , and

 A1, A2, … An are new names for the attributes in the


relation R.
Example
12

 The name, branch column of student table are renamed as


newname and newbranch respectively
Binary Operations
13

 Cartesian Product Operation

 The Cartesian product operation (also known as Cross Product or


Cross Join or Product) is binary set operation that generates a
new relation from two relation in a combinatorial fashion.

RS

  represents the PRODUCT operator, and

 R and S are relations to be joined..

 Degree of R X S = degree of R + degree of S

 {degree = total no of columns}


Example
14 Consider the following tables (R1 and R2)
respectively
Regno Branch Section
1 CSE A
2 ECE B
3 CIVIL B
4 IT A

Name RegNo
Bhanu 2
priya 4
15
… Binary Operations
16
 Set Operations

 Union Operation

 The union operation on R and S denoted by R  S results a relation


that includes all tuples either in R or in S or in both.

 Intersection Operation

 The intersection operation on R and S denoted by R  S results a


relation that includes all tuples in both R and S.

 Set Difference Operation

 The result of the set difference operation on R and S denoted by R −


S is the set of elements in R but not in S.
Example
17
 Union operation combines values in R1, R2 by
removing duplicate ones.
18
19
Example (Intersection)
20
 Find all the customers whose account is in the bank and
have taken out a loan.
 The expression is as follows −
Depositor Borrower
Name Name
A B
B A
C D
21 Example
 Consider the below tables Employee and Student. Perform Set Difference
operation as (Employee-Student).
22 Result after performing Set difference (Employee-
Student)
… Binary Operations
23

 For the set operations (Union, Intersection, Set difference) the two
relational operands R and S must have same type of tuples, this
condition is known as Union Compatibility.

 Two relations R(A1, A2, … An) and S(B1, B2, … Bn) are said to
be union compatible if

 They have same degree n, and

 Domain(Ai) = Domain(Bi) for all i = 1, 2, … n

 For all set operations duplicates are eliminated from the result.
24 Relational calculus
 Relational calculus is a non-procedural query language in
database management systems, guides users on what data is
needed without specifying how to obtain it.

 Commonly utilized in commercial relational languages like


SQL-QBE and QUEL, relational calculus ensures a focus on
desired data without delving into procedural details, promoting a
more efficient and abstract approach to querying in relational
databases
25 Query By Example (QBE)
 It is a graphical query language where we get a user interface
and then we fill some required fields to get our proper result.

 In SQL we will get an error if the query is not correct but in the
case of QBE if the query is wrong either we get a wrong answer
or the query will not be going to execute but we will never get
any error.

 In QBE we don’t write complete queries like SQL or other


database languages it comes with some blank so we need to just
fill that blanks and we will get our required result.
26 What is Relational
 Calculus?
Before understanding Relational calculus in DBMS, we need to
understand Procedural Language and Declarative Language.

 Procedural Language - Those Languages which clearly define


how to get the required results from the Database are called
Procedural Language. Relational algebra is a Procedural
Language.

 Declarative Language - Those Language that only cares about


What to get from the database without getting into how to get the
results are called Declarative Language. Relational Calculus is
a Declarative Language.
27 Types of Relational Calculus in DBMS

Relational Calculus is Two Types:

 Tuple Relational Calculus (TRC)

 Domain Relational Calculus (DRC)


28 Tuple Relational Calculus (TRC)

 Tuple Relational Calculus in DBMS uses a tuple variable (t)


that goes to each row of the table and checks if the predicate
is true or false for the given row. Depending on the given
predicate condition, it returns the row or part of the row.

 The Tuple Relational Calculus expression Syntax

 { t\| p(t) }

 Where t is the tuple variable that runs over every Row, and
P(t) is the predicate logic expression or condition.
29 Example
Conts’….
30
 Example 1: Write a TRC query to get all the data of customers
whose zip code is 12345.
 TRC Query: {t \| t ∈ Customer ∧ t.Zipcode = 12345} or TRC
Query: {t \| Customer(t) ∧ t[Zipcode] = 12345 }
 Workflow of query - The tuple variable "t" will go through every
tuple of the Customer table. Each row will check whether the
Cust_Zipcode is 12345 or not and only return those rows that
satisfies the Predicate expression condition.
 Result of the TRC expression above:

Customer_id Name Zip code

1 Rohit 12345

4 Amit 12345
Conts’…….
31

 Example 2: Write a TRC query to get the customer id of all


the Customers.
 TRC query: { t \| ∃s (s ∈ Customer ∧ s.Customer_id =
t.customer_id) }
 Result of the TRC Query:

Customer_ID
1
2
3
4
5
32 EXERCISES
Consider student table

FN LN AGE
A E 30
B F 31
C G 27
D H 28

Display the last name of those students where age is


greater than 30

{ t.LN | students(t) and t.age >30)}


Domain Relational Calculus (DRC)
33
 Domain Relational Calculus uses domain Variables to get the
column values required from the database based on the
predicate expression or condition.

 The Domain relational calculus expression syntax:

 { < x1, x2, x3, x4…>\|p(x1, x2, x3, x4…)}

 where,

 <x1,x2,x3,x4...> are domain variables used to get the column


values required, and P(x1,x2,x3...) is predicate expression or
condition.
34 Example
Conts’…
35

 Example 1: Write a DRC query to get the data of all


customers with Zip code 12345.

 DRC query: {<x1,x2,x3> \| <x1,x2> ∈ Customer ∧ x3 =


12345 }

 Workflow of Query: In the above query x1,x2,x3 (ordered)


refers to the attribute or column which we need in the result,
and the predicate condition is that the first two domain
variables x1 and x2 should be present while matching the
condition for each row and the third domain variable x3
36 Conts’…

Result of the DRC query will be:


37

THANK YOU !

You might also like