0% found this document useful (0 votes)
8 views26 pages

Lec 12

Uploaded by

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

Lec 12

Uploaded by

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

DATABASE SYSTEMS

Dr. Noha Nagy

Lecture 12
Relational Query Languages
2

 Languages for describing queries on a relational


database
 Structured Query Language (SQL)
 Predominant application-level query language
 Declarative

 Relational Algebra
 Intermediate language used within DBMS
 Procedural
Relational Algebra Operations
3
 Unary Operations
 Selection  (sigma))
 Projection  (pi))
 Rename  (rho))
 Binary Operations
 Union 
 Intersection 
 Setdifference

 Cartesian product
x
Select Operator
4

 Produce table containing subset of rows of


argument table satisfying condition
condition relation
 Example:
Person Hobby=‘stamps’(Person)
Id Name Address Hobby Id Name Address Hobby
1123 John 123 Main stamps 1123 John 23 Main stamps
1123 John 123 Main coins 9876 Bart Pine St stamps
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps
Selection Condition - Examples
5

  Id>3000 Or Hobby=‘hiking’ (Person)


  Id>3000 AND Id <3999 (Person)

  NOT(Hobby=‘hiking’) (Person)
  Hobby‘hiking’ (Person)
STUDENT
ST-ID Name Address Major GPA
6
123 Ali Dokki EE 3.2
456 Maha Nasr City CE 1.9
789 Ahmad Haram Arch 2.7
341 Noha Dokki EE 1.0

 Address = “Dokki” and Major= “EE” (STUDENT)

ST-ID Name Address Major GPA


123 Ali Dokki EE 3.2
341 Noha Dokki EE 1.0
STUDENT
ST-ID Name Address Major GPA
7
123 Ali Dokki EE 3.2
456 Maha Nasr City CE 1.9
789 Ahmad Dokki Arch 2.7
341 Noha Dokki EE 1.0

 Address = “Dokki” or Major= “EE” (STUDENT)

ST-ID Name Address Major GPA


123 Ali Dokki EE 3.2
789 Ahmad Dokki Arch 2.7
341 Noha Dokki EE 1.0
Unary Relational Operations: SELECT (contd.)
8

 SELECT Operation Properties

 SELECT  is commutative:

◼  <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))

 Because of commutativity property, a cascade (sequence) of SELECT


operations may be applied in any order:

◼ <cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R)))

 A cascade of SELECT operations may be replaced by a single


selection with a conjunction of all the conditions:
<cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))
Unary Relational Operations: PROJECT
9

 PROJECT Operation is denoted by  (pi)


 This operation keeps certain columns (attributes) from a
relation and discards the other columns.
 Example: To list each employee’s first and last name and
salary, the following is used:
LNAME, FNAME,SALARY(EMPLOYEE)
Project Operator
10

 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
10
PROJECT Operation 

11

STUDENT
ST-ID Name Address Major GPA
123 Ali Dokki EE 3.2
456 Maha Nasr City CE 1.9
789 Ahmad Haram Arch 2.7
341 Noha Dokki EE 1.0

 ST-ID, Major (STUDENT)  Major (STUDENT)


ST-ID Major Major RA returns distinct
123 EE
EE tuples
CE
456 CE
Arch
789 Arch
341 EE
Unary Relational Operations: PROJECT (contd.)
12

 PROJECT Operation Properties


 The number of tuples in the result of projection <list>(R)
is always less or equal to the number of tuples in R
(WHEN EQUAL???)
◼ Ifthe list of attributes includes a key of R, then the number of
tuples in the result of PROJECT is equal to the number of
tuples in R
 PROJECT is NOT commutative
◼  <list1>( <list2> (R) ) =  <list1> (R) as long as <list2> contains
the attributes in <list1>
 Find names and GPAs of either EE or CE students where
the GPA is less than 2.0.
13
STUDENT
ST-ID Name Address Major GPA
123 Ali Dokki EE 1.2
456 Maha Nasr City CE 1.9
789 Ahmad Haram Arch 2.7
341 Noha Dokki EE 1.0

Name, GPA ( (Major = “EE” or Major = “CE”) and GPA < 2.0 (STUDENT))

Name GPA
Ali 1.2
Maha 1.9
Noha 1.0
Single expression versus sequence of relational
operations (Example)
14
 Retrieve the first name, last name, and salary of all employees
who work in department number 5.

 FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))

 OR We can explicitly show the sequence of operations, giving a


name to each intermediate relation:

 DEP5_EMPS   DNO=5(EMPLOYEE)

 RESULT   FNAME, LNAME, SALARY (DEP5_EMPS)


STUDENT PROF
ST-ID Name Address Major GPA P-ID PName ADD WorksIn Rank
123 Ali Dokki EE 3.2 P71 Ali Shobra Comm Full
15 456 Maha Nasr City CE 1.9 P08 Karem Dokki NULL Full
789 Reda Dokki Arch 2.7 P11 Magda Agoza CE Assoc
341 Noha Dokki EE 1.0 P91 Nagy Dokki EE Full

STUDENT X PROF
ST-ID Name Address Major GPA P-ID PName ADD WorksIn Rank
123 Ali Dokki EE 3.2 P71 Ali Shobra Comm Full
123 Ali Dokki EE 3.2 P08 Karem Dokki NULL Full
123 Ali Dokki EE 3.2 P11 Magda Agoza CE Assoc
123 Ali Dokki EE 3.2 P91 Nagy Dokki EE Full
456 Maha Nasr City CE 1.9 P71 Ali Shobra Comm Full
456 Maha Nasr City CE 1.9 P08 Karem Dokki NULL Full
456 Maha Nasr City CE 1.9 P11 Magda Agoza CE Assoc
456 Maha Nasr City CE 1.9 P91 Nagy Dokki EE Full
789 Reda Dokki Arch 2.7 P71 Ali Shobra Comm Full
789 Reda Dokki Arch 2.7 P08 Karem Dokki NULL Full
789 Reda Dokki Arch 2.7 P11 Magda Agoza CE Assoc
789 Reda Dokki Arch 2.7 P91 Nagy Dokki EE Full
341 Noha Dokki EE 1.0 P71 Ali Shobra Comm Full
341 Noha Dokki EE 1.0 P08 Karem Dokki NULL Full
341 Noha Dokki EE 1.0 P11 Magda Agoza CE Assoc
341 Noha Dokki EE Slide
1.06- 15P91 Nagy Dokki EE Full
Relational Algebra Operations from Set Theory:
CARTESIAN PRODUCT (cont.)
16

 Generally, CARTESIAN PRODUCT is not a meaningful


operation

 Can become meaningful when followed by other operations


 Address = ADD (STUDENT X PROF) 

ST-ID Name Address Major GPA P-ID PName ADD WorksIn Rank
17 123 Ali Dokki EE 3.2 P71 Ali Shobra Comm Full
123 Ali Dokki EE 3.2 P08 Karem Dokki NULL Full
123 Ali Dokki EE 3.2 P11 Magda Agoza CE Assoc
123 Ali Dokki EE 3.2 P91 Nagy Dokki EE Full
456 Maha Nasr City CE 1.9 P71 Ali Shobra Comm Full
456 Maha Nasr City CE 1.9 P08 Karem Dokki NULL Full
456 Maha Nasr City CE 1.9 P11 Magda Agoza CE Assoc
456 Maha Nasr City CE 1.9 P91 Nagy Dokki EE Full
789 Reda Dokki Arch 2.7 P71 Ali Shobra Comm Full
789 Reda Dokki Arch 2.7 P08 Karem Dokki NULL Full
789 Reda Dokki Arch 2.7 P11 Magda Agoza CE Assoc
789 Reda Dokki Arch 2.7 P91 Nagy Dokki EE Full
341 Noha Dokki EE 1.0 P71 Ali Shobra Comm Full
341 Noha Dokki EE 1.0 P08 Karem Dokki NULL Full
341 Noha Dokki EE 1.0 P11 Magda Agoza CE Assoc
341 Noha Dokki EE 1.0 P91 Nagy Dokki EE Full
JOIN Operation
18

 X.a = Y.a (X x Y) 

Y X
X.a = Y.a

JOIN Operation

X.a = Y.a is called the JOIN Condition


Examples of Queries in Relational Algebra
19

◼ Q1: Retrieve the name and address of all employees who work for the
‘Research’ department.
◼ Employee(Eid,Fname,Lname,address,salary,Dno)
◼ Department(Dnumber,Dname)
RESEARCH_DEPT   DNAME=’Research’ (DEPARTMENT)
RESEARCH_EMPS  (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE)
RESULT   FNAME, LNAME, ADDRESS (RESEARCH_EMPS)
How to transform from SQL to RA
20

Select name, age
From student
Where depno=1 

 name, age  depno=1 (student)


How to transform from SQL to RA
21
Student(sid,name,age,dno)
Department (Did, depname)
Select depname 
From student, department
Where dno=did 
And age>30
 name  age>30 (student department)
Dno=did

 name  salary  age student


Set operation
22
Outer Join
23
Outer Join
24
Aggregate function
25
Divide Operation
26Slide

 Find suppliers that supply all parts

You might also like