Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Week 10

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

Relational Algebra

Week 10
Recap
• In the previous lesson, we discussed
• the SELECT command
• Use various clauses with SELECT
Objectives
By the end of this lesson, students should be able to :
• Extract data from various relations, joins
• Describe and use basic operators
• Identify syntax for various operations
• Translate relational algebra expressions to SQL statements (vice versa)
Joins

• This is used to combine data from two different relations(tables) into one
and fetch data from the combined relation
• Theta join or inner join
• R ⋈<join condition>S
• Combines rows from two tables into a single record for records
meeting the join condition
Theta Join
Consider these two relations
JOINS
• Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining
fields from two tables by using values common to each.
• INNER JOIN is used to select data that have matching records in both tables
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
• LEFT JOIN selects data from the left table that have been matched to the right table
• SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Natural Join Operator
• Also known as a binary operator
• R ⋈S
• Enforces equality on the attributes that have the same name
• If we have two tables A and B. Student id in table A is identical to
student id in table B, then it retrieves details for both records.
• Removes one copy of duplicate attributes
• Since we are selecting the same information from table A and table B,
natural join just displays one information.

7
Natural Joins
Natural Join
Consider these two relations
JOINS
• RIGHT JOIN selects data from the right table that have been matched to the left table
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
• FULL OUTER JOIN selects data when there is a match in the left or right table.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
JOINS
• SELF JOIN is used when you want to join a table to itself
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
Union
• The operation take two input relations, which must be union-compatible:
• Same number of fields.
• `Corresponding’ fields have the same type.
• Duplicate tuples are automatically eliminated.
rUs
• Where r and s are either database relations or relation result set (temporary
relation).
Union U
sid sname rating age sid sname rating age
22 dustin 7 45.0 22 dustin 7 45.0
31 lubber 8 55.5
31 lubber 8 55.5 58 rusty 10 35.0
58 rusty 10 35.0 44 guppy 5 35.0
S1 28 yuppy 9 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
S2
Set Difference -
• The result of set difference operation is tuples, which are present in one
relation but are not in the second relation.
r − s
Set Difference -
sid sname rating age sid sname rating age
22 dustin 7 45.0 22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
S1
sid sname rating age sid sname rating age
28 yuppy 9 35.0 28 yuppy 9 35.0
31 lubber 8 55.5 44 guppy 5 35.0
44 guppy 5 35.0
58 rusty 10 35.0 S2 – S1
S2
Cartesian-Product X
• S1  R1: Each row of S1 paired with each row of R1.
• Result schema has one field per field of S1 and R1, with
field names `inherited’ if possible.
• May have a naming conflict: Both S1 and R1 have a field with the same name.
• In this case, can use the renaming operator:
Cartesian Product Example
sid bid day sid sname rating age
22 101 10/10/96 22 dustin 7 45.0
58 103 11/12/96 31 lubber 8 55.5
58 rusty 10 35.0
R1
S1
(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

S1 x R1 = 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
Intersection
• Intersection takes two input relations, which must be union-compatible.
Intersection
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0 sid sname rating age
31 lubber 8 55.5
S1
58 rusty 10 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
S2
Relational Algebra
•  is a procedural query language used to query the database tables to access
data in different ways.
• provides sets of operators to perform queries.​
• input is a relation and output is also a temporary relation also know as a
result set.
Relational Algebra
• 5 Basic Operations
1. Selection ( s ) Selects a subset of rows from relation (horizontal).
2. Projection ( p ) Retains only wanted columns from relation (vertical).
3. Cross-product (  ) Allows us to combine two relations.
4. Set-difference ( — ) Tuples in r1, but not in r2.
5. Union (  ) Tuples in r1 or in r2.
Selection σ
• The select operator returns results that meets a condition.
• Result is a relation.
σ
<select condition> (R)

• Where
• σ is the selection operator
• <select condition> - the specific condition
• (R) is the name of the relation
• The condition is built using operators like =, >, < , AND,OR
Select Operation

• σage > 17 (Student)


• σage > 17 (Student)

σCity =“Rundu” (Team) will be

Select * from Team where City = ‘Rundu’;


Selection
• Result set
Select Operator
• Example
• σ
Minister_Id IN (100, 200, 300) AND salary >= 100(Minister)
• This operation presented would read as
• σ – select
• <select condition> - select query which meets the two conditions i.e
Minister_Id as in the list with a salary greater or equal to 100.
• (R) – in this case is Minister thus the query will be retrieved from the
Minister table

25
Select Operator
• Exercises
What do the following Relational Algebra expressions mean?
1. σMinister_marital_status = ‘M’ OR salary > 5000(Minister)
2. σemployee_dob > ’01-MARCH-1999’(Employee)
Translate the following select statement to relational algebra
expressions
• SELECT first_name, last_name, date_of_birth, sex
FROM EMPLOYEE
WHERE graduation_school = ‘Harvard’ AND Qualification = ‘MSc’ AND
graduation_year = ‘2007’
26
Project Operation π
• used to project only a certain set of attributes of a relation, removing
duplicate data
• Retains only attributes that are in the “projection list”.
• Schema of result: exactly the fields in the projection list, with the same
names that they had in the input relation.
π
<attribute list> (R)
Projection π
• Result set
Project Operator
• Example
• π
first_name, last_name, date_of_birth, salary(Employee)
• This operation presented would read as
• π – select a distinct result
• <attribute list> - the list of columns to be retrieved
• (R) – in this case, is Employee
• The query will be retrieved from the Employee table

29
Activity-Projection
• What will be the result set given ?
• ∏TeamName, City(Team)
Activity Projection -Answer
Project Operator
• Exercise
• π first_name, last_name, date_of_birth, salary(Employee)
• Translate the following relational algebra expression to SQL query
• π Lecturer_surname, Lecturer_date_of_birth(Lecturer)
• π Student_id, Student_course, student_admission_year(Student)
• π Staff_name, Staff_id, staff_date_of_birth(Staff)

32
Rename Operation ρ
• This operation is used to rename the output relation for any query
operation which returns result like Select, Project

•ρ S
(R)

• ρ – a rename operator
• S- the new name of the relation
• (R) – the name of the relation to be changed
Rename Operation

• Renaming attribute
• ρ (Attr1…Attrn)(R)
• Renaming relation and its attribute
• ΡS(Attr1…Attrn)(R)
•ρ Minister_Accommodation
(Accommodation)

• Renames the Accomdation table to Minister_Accommodation


Rename
• Consider this relation
• 1. rename the attribute name to bar
Rename
Activity
• Write the relational Algebraic statement to:
1.Rename the following attribute Coach and Captain to TeamCoach and
TeamCap respectively
2.Rename the Team relation to PlayerDetails
Summary
• In this lesson we discussed
• Joins
• Relational Algebra
• Syntax of basic operators
Announcements
• Supplementary Test 12th May 2023
• Lab Marks (follow up with practical lecturers)
• Attend practical classes

You might also like