0% found this document useful (0 votes)
11 views27 pages

Chap 6

Chapter six discusses query languages, focusing on relational query languages (RQL) and relational algebra, which involves operations like selection, projection, union, and set difference. It contrasts relational algebra's procedural nature with relational calculus's non-procedural approach and introduces SQL as a practical language for database manipulation. The chapter highlights the importance of these languages in querying and managing relational databases.

Uploaded by

hirpaadugna1
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)
11 views27 pages

Chap 6

Chapter six discusses query languages, focusing on relational query languages (RQL) and relational algebra, which involves operations like selection, projection, union, and set difference. It contrasts relational algebra's procedural nature with relational calculus's non-procedural approach and introduces SQL as a practical language for database manipulation. The chapter highlights the importance of these languages in querying and managing relational databases.

Uploaded by

hirpaadugna1
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/ 27

Chapter six

Query Language and Relational Query Languages


• A query language is a language in which user
requests information from the database.
• RQL:-Languages for describing queries on a
relational database
• Relational Algebra
– We begin with the notations used in RA and that
are unique to RA and are procedural, requiring a
sequence of operations to express a query
transaction

1
What is an Algebra?
• A language based on operators and a domain of values
• Operators map values taken from the domain into other
domain values
• Hence, an expression involving operators and arguments
produces a value in the domain
• When the domain is a set of all relations (and the operators are
as described later), we get the relational algebra
• We refer to the expression as a query and the value produced
as the query result

2
Relational Algebra
• Domain: set of relations
Fundamental Operations
SELECT
 PROJECT
 UNION
 SET DIFFERENCE
 CARTESIAN PRODUCT
• Select and project operations are unary operation as they
operate on a single relation. Union, set difference, Cartesian
product and operations are binary operations as they operate on
pairs of relations.

3
Relational Algebra
• Basic operations:
– Selection (  ) Selects a subset of rows from relation.
– Projection ( ) Deletes unwanted columns from relation.
– 
Cross-product ( ) Allows us to combine two relations.
– 
Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
– Union (  ) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!)
useful.
• Since each operation returns a relation, operations can be
composed!
Selection Condition
• Selects rows that satisfy • Operators: <, , , >,
selection condition.
• Schema of result identical to =, 
schema of (only) input relation. • Simple selection
• Result relation can be the input
for another relational algebra
condition:
operation. • <condition> AND
<condition>
• <condition> OR
<condition>
• NOT <condition>

5
Selection Condition - Examples
•  salary>3000 Or name=‘Tom’ (Employee)
•  nr>5 AND nr <8 (Employee)

•  NOT(name=‘John’) (Employee)
Table1: Employee •  nr 7 (Employee)

Note that the select operation in relational algebra has nothing to do with the SQL keyword select.
Selection in relational algebra returns those tuples in a relation that fulfil a condition, while the SQL
keyword select means "here comes an SQL statement".
Projection
• Deletes attributes that are not in projection list.
• Schema of result contains exactly the fields in the projection list, with the same names that they had
in the (only) input relation.
• Projection operator has to eliminate duplicates! (Why??, what are the consequences?)
– Note: real systems typically don’t do duplicate elimination unless the user
explicitly asks for it. (Why not?)
– where'Π. (pi) is the symbol used to represent the PROJECT
operation,and<attributelist>
– Is the desired list of attributes from the attributes of relation R
• Produces table containing subset of columns of argument table
attribute list(relation)
Example:
Person Name,Hobby(Person)
Id Name Address Hobby Name Hobby
1123 John 123 Main stamps John stamps
1123 John 123 Main coins John coins
5556 Mary 7 Lake Dr hiking Mary hiking
9876 Bart 5 Pine St stamps Bart stamps
Project Operator
• Example:
Person Name,Address(Person)
Id Name Address Hobby Name Address

1123 John 123 Main stamps John 123 Main


1123 John 123 Main coins Mary 7 Lake Dr
5556 Mary 7 Lake Dr hiking Bart 5 Pine St
9876 Bart 5 Pine St stamps

Result is a table (no duplicates)

8
sid bid day
R1
22 101 10/10/96
58 103 11/12/96

S1
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0 sid sname rating age
28 yuppy 9 35.0
S2 31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Union, Intersection, Set-Difference
• union: The result of this operation, denoted by R U
S, is a relation that includes all tuples that are
either in R or in S or in both R and S. Duplicate
tuples are eliminated.
• intersection: The result of this operation, denoted
by R n S, is a relation that includes all tuples that
are in both R and S.
• set difference(or MINUS):The result of this
operation, denoted by R - S, is a relation that
includes all tuples that are in R but not in S.
Union, Intersection, Set-Difference
sid sname rating age
• All of these operations take two 22 dustin 7 45.0
input relations, which must be 31 lubber 8 55.5
union-compatible: 58 rusty 10 35.0
– Same number of fields. 44 guppy 5 35.0
28 yuppy 9 35.0
– `Corresponding’ fields
S1 S2
have the same type.
• What is the schema of result? sid sname rating age
sid sname rating age 31 lubber 8 55.5
22 dustin 7 45.0 58 rusty 10 35.0
S1 S2 S1 S2
Cross-Product
• Each row of S1 is paired with each row of R1.
• Result schema has one field per field of S1 and
R1, with field names `inherited’ if possible.
– C=AXB
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96
Cont..
• Notice that both UNION and INTERSECTION
are commutative operations; that is,R U S=SU
R, and RnS=SnR
• Both UNION and INTERSECTION can be
treated as n-ary operations applicable to any
number of relations because both are
associative operations;that is,RU (S U T)=(RU
S) U T, and (RnS)nT=Rn(S nT)
• The MINUS operation is not commutative;that
is, in general,R-S#S-R
Relational calculus

• Expression to specify a retrieval request, and hence there is no


description of how to evaluate a query.
• A calculus expression specifies what is to be retrieved rather than
how to retrieve it.
• Therefore , the relational calculus is considered to be a non
procedural language. This differs from relational algebra, where
we must write sequence of operations to specify a retrieval
request;
• hence, it can be considered as a procedural way of stating a query.
• It is possible to nest algebra operations to form a single
expression;
• How ever, a certain order among the operations is always
explicitly specified in a relational algebra expression.
Cont..
A simple tuple relational calculus query is of the form {t I COND(t)}
Where t is a tuple variable and COND(t) is a conditional expression
involving t. The result of such a query is the set of all tuples t that
satisfy COND(t).
• The AND, OR, and NOT logical operators are also used in the
expressions. An example of a RC expression is:
• {t | STUDENT(t) AND t. GPA > 3.0}
• This query would return all students whose GPA is greater than 3.0.
• Expressions and Formulas in RC as noted by Elmasri et al are in the
form:
• {t1.Aj, t2.Ak,…, tn.Am | COND(t1, t2, …, tn, tn+1, tn+2, …, tn+m)}
• Where COND is the condition or formula.
Cont..
• For example2, to find all employees whose salary is
above $50,000, we can write the following tuple
calculus expression:[r I EMPLOYEE(t) and
t.SALARy>50000}
• The condition EMPLOYEE(t) specifies that the range
relation of tuple variable t is EMPLOYEE. Each
EMPLOYEE tuple t r that satisfies the condition
t.SALARy>50000 will be retrieved.
• Notice that t.SALARY references attribute SALARY
of tuple variable t; this notation resembles how
attribute names are qualified with relation names or
aliases in SQL,
Cont..
• as we shall see in the above query retrieves all attribute values
for each selected EMPLOYEE tuple r . To Retrieve only some
of the attributes say , the first and last names-we write
{t.FNAME, t.LANME I EMPLOYEE(t) AND
t.SALARy>50000}
• Informally, we need to specify the following information in a
tuple calculus expression:
For each tuple variable t, the range relation R of t. This value is
specified by a condition of the form R(t).
cont.
QUERY1
• Retrieve the birth date and address of the employee(or
employees)whose name is'John Bekel Smith'.
• Q1:{t.BDATE, t.ADDRESS I EMPLOYEE(t) AND
t.FNAME='John' AND t.MIDLNA='B' AND
t.LNAME='Smith'}
• ln tuple relational calculus, we first specify the requested
attributes t.BDATE and t.ADDRESS for each selected
tuple r. Then we specify the condition or selecting a tuple
following the bar (I)-namely , that t be a tuple of the
EMPLOYEE relation whose FNAME, t.MIDLNA And
LNAME attribute values are 'John', 'B', and 'Smith',
respectively.
Universal and extensile quantifier

• Universal quantifier (∀): Is true if all tuples make the formula


true.
• Existential quantifier (∃): Is true if there exists some tuple that
makes the formula true.
• Read example of them more?
Sql language
Sql is
stands for structured query language. standard
language for accessing and manipulating
database Sql language(
 DDL(create , drop, alter ),
 DML(select, update, Insert , delete ))
Sql is not case sensitive
After sql statement semicolon(,)
Primary key can’t be null
Cont..
• The SQL language may be considered one of
the major reasons for the success of relational
databases in the commercial world.
• Because it be came a standard for relational
databases, users were less concerned about
migrating their database applications from
Other types of database systems for example ,
network or hierarchical systems-to relational
systems
Cont..
• SQL is a comprehensive database language: It
has statements for data definition, query, and
update. Hence, it is both a DML. In addition, it
has facilities for Defining views on the
database, for specifying security and
authorization, for defining integrity
constraints, and for specifying transaction
controls.
Query Sublanguage of SQL
• Select,where,from,create,delete,drop,update,
alter ……are sql keywords read more from lab
practicing
Duplicates

• Duplicate rows not allowed in a relation


• However, duplicate elimination from query
result is costly and not automatically done; it
must be explicitly requested:

SELECT DISTINCT …..


FROM …..

24
The differences b/n them
• Relational Algebra (RA) and Relational Calculus (RC) are formal
languages for the database relational model while
• SQL is the practical language in the database relational model.
• In these formal languages a conceptual database model is expressed in
mathematical terms and notations while in the practical language –
SQL, the mathematical expressions of the functionality and transaction
of the database operations are implemented physically.
• Each language, that RA, RC, and SQL have their own notations to
express their notations.
• RA Notations – Procedural Expression Language
• RC Notation – Non Procedural Expression Language
• SQL is what is called a declarative language or non procedural , meaning
that you only need to specify what needs to be accomplished (such as a
query or insert) and not how to do it; the DBMS determines and
performs internally the step by step operation needed to obtain the
result.
Cont..
• For more about SQL
Read your lab class handout
d !
En

You might also like