0% found this document useful (0 votes)
88 views21 pages

Chapter 6 RelationalQueryLanguage

The document discusses relational query languages and their components. It describes two basic mathematical query languages: relational algebra and relational calculus. Relational algebra uses operations like selection, projection, join, and set operations to manipulate relations and form queries. Relational calculus expresses queries declaratively using tuple and domain calculus with quantifiers. Both languages allow querying and retrieving data from relational databases.

Uploaded by

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

Chapter 6 RelationalQueryLanguage

The document discusses relational query languages and their components. It describes two basic mathematical query languages: relational algebra and relational calculus. Relational algebra uses operations like selection, projection, join, and set operations to manipulate relations and form queries. Relational calculus expresses queries declaratively using tuple and domain calculus with quantifiers. Both languages allow querying and retrieving data from relational databases.

Uploaded by

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

Fundamentals of Database Management Systems

Chapter 6
Relational Query Languages
https://sites.google.com/site/iotictcourses

12/16/20 Chapter 3: Database Design 1


Relational Query Language
• Relational Query languages:
– Allow manipulation and retrieval of data from a
database.
– Manipulation mechanism is also an important
component of any data model.
• The two basic mathematical Query Languages:
– Relational Algebra:
– Relational Calculus:

2
Relational Algebra
• Relational algebra is the basic set of operations that enables a
user to specify basic retrieval requests.
• The algebra operations produce new relations, which can be
further manipulated using operations of the same algebra.
• Sequence of relational algebra operations forms a relational
algebra expression  Represents a database query (retrieval
request).
• RA defines one or more new relations without changing the
original relation.
• The output from one operation can become the input to
another operation (nesting of RA operations)
3
• The basic type of Relational Algebra operations:
– Selection (  ) Selects a subset of rows from a relation.
– Projection (  ) Deletes unwanted columns from a relation.
– Renaming: assigning intermediate relation for a single
operation
– Cross-Product ( x ) Allows to concatenate(join or link) a
tuple from one relation with all the tuples from the other
relation.
– Set-Difference (-) Tuples in relation R1, but not in relation R2.
– Union ( ) Tuples in relation R1, or in relation R2.
– Intersection () Tuples in relation R1 and in relation R2
– Join Tuples joined from two relations based on a condition
• Join and intersection are derivable from the rest.
• Using these operations, we can build up sophisticated
database queries.
4
1. Selection operation:
– Selects subset of tuples/rows in a relation that satisfy
the selection condition.
– Schema of result identical to schema of (only) input
relation.
– Result relation can be the input for another relational
algebra operation! (Operator composition.) 5
• Notation:  <Selection Condition> <Relation Name>
• Example: Find all Employees with skill type of Database.
 < SkillType =”Database”> (Employee)

6
2. Projection
– Selects certain attributes while discarding the
other from the base relation.
– The PROJECT creates a vertical partitioning.
– Deletes attributes that are not in projection list.

7
8
3. Rename Operation
• Applies several relational algebra operations one
after the other.
• Two ways of expression:
1. A single algebraic expression:

2. Using an intermediate relation by the Rename


Operation:

9
4. Set Operations

10
I. Union Operation

II. Intersection Operation

11
5. Cartesian Product operation
– Combines tuples from two relations in a combinatorial
fashion.
– If R1 has n tuples & R2 has m tuples, then the resulting
relation will have n* m tuples.

12
• To extract employee information about Managers of
each department from the above result relation the
algebra query and the resulting relation will be:

13
6. Join Operation
• Select related tuples from two relations.
• In the JOIN operation:
– Cartesian & Selection Operations are used together.

14
Relational Calculus
• Relational calculus expressions creates a new relation
over:
– rows of the stored db relations (in tuple calculus) or
– columns of the stored relations (in domain calculus).
• In a RC, Unlike RA, there is no ordering of operations to
specify how to retrieve the query result.
• Relational calculus is nonprocedural or declarative
language. While Relational Algebra procedural or
sequential language.

15
• There are two types of Relational Calculus operations:
1. Tuple Relational Calculus
2. Domain Relational Calculus

Tuple Relational Calculus


• The tuple relational calculus is based on specifying a number of tuple
variables.
• A simple tuple relational calculus query is of the form
{t | 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).

16
• Example: To find the first and last names of all employees
whose skill level is greater than 5, we can write the following
tuple calculus expression:

{t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SKILLLEVEL>5}

• This is equivalent to:


(PROJECTION FNAME, LNAME) of each EMPLOYEE tuple t that
satisfies the condition
t.SKILLLEVEL>5 (SELECTION  SKILL_LEVEL>5) will be retrieved.

17
• Example2: Find all employees whose skill level is >= 8.
{E | Employee(E)  E.SkillLevel >= 8}

• To find only EmpId, FName, LName, Skill & School where


skill level >= 8, the resulting expression and relation will be:

18
Quantifiers in Relational Calculus
1. Existential quantifier  (‘there exists’)
– A rule should be true for at least one instance.
– Example: An employee with skill level >= 8 will be:
{E | Employee(E)  (E)(E.SkillLevel >= 8)}
– There exist at least one tuple of the relation employee where the value for the
SkillLevel >= 8.
2. Universal quantifier  (‘for all’)
– Implies every instance of a relation in the database:
– An employee with skill level >= 8 will be:
{E | Employee(E)  (E)(E.SkillLevel >= 8)}
– For all tuples in relation employee, the values for the SkillLevel are greater than or equal
to 8.

19
Domain Relational Calculus
• Domain calculus differs from tuple calculus in the type of
variables used in formulas:
– variables range over tuples in tuple calculus
– variables range over single values from domains of
attributes in Domain calculus
• Example:
– Query1: list Employee Name
{Fname, Lname| (Employee (EID,FName, LName)}

20
• Query2: Find the list of Employees who work in the
department of IS
– Domain relational Calculus expression for the query: {EID, Fname,
Lname|( DName, EDID, DID) (Employee(EID, FName, LName) 
Department(DID, DName, DMangID)  DID=EDID  DName=’IS’)},
Where DName, EDID, DID DName, EDID, DID
• Query3:List the names of employees that do not manage
any department.
– {Fname, Lname|(EID)(Employee(EID, Fname,Lname)
(~(DMangId)(Dept(DID, Dname, DMangId) (EID=DMangId))))}

21

You might also like