SlideShare a Scribd company logo
1 of 46
Chapter Seven
Relational Algebra
1
Basic Terminologies
• Relational algebra
• Relational-algebra operations
– Select
– Project
– Union ∪
– Set difference −
– Cartesian product ×
– Rename
2
…
• Additional operations
– Set intersection ∩
– Natural join
– Assignment operation
– Outer join
• Left outer join
• Right outer join
• Full outer join
3
Relational Algebra
• The relational algebra defines a set of algebraic
operations that operate on tables, and output
tables as their results.
• These operations can be combined to get
expressions that express desired queries.
• The algebra defines the basic operations used
with in relational query languages.
4
…
• The operations in relational algebra can be
divided into:
– Basic operations
– Additional operations that can be expressed in
terms of the basic operations
– Extended operations, some of which add further
expressive power to relational algebra
5
Relational Query Languages
• Query languages: Allow manipulation and
retrieval of data from a database.
• Relational model supports simple, powerful QLs:
– Strong formal foundation based on logic.
– Allows for much optimization.
• Query Languages != programming languages!
– QLs not expected to be “Turing complete”.
– QLs not intended to be used for complex calculations.
– QLs support easy, efficient access to large data sets.
6
Formal Relational Query Languages
• Two mathematical Query Languages form
the basis for “real” languages (e.g. SQL),
and for implementation:
– Relational Algebra: More
operational(procedural), very useful for
representing execution plans.
– Relational Calculus: Lets users describe what
they want, rather than how to compute it.
(Non-operational, declarative.)
7
Preliminaries
• A query is applied to relation instances, and
the result of a query is also a relation instance.
– Schemas of input relations for a query are fixed
(but query will run regardless of instance!)
– The schema for the result of a given query is also
fixed! Determined by definition of query language
constructs.
8
Example Instances
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
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
sid bid day
22 101 10/10/96
58 103 11/12/96
R1
S1
S2
• “Sailors” and “Reserves”
relations for our examples.
“bid”= boats. “sid”: sailors
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! (Algebra is “closed”.)
σ
π
−
×

10
Selection
σrating
S
>8
2( )
sid sname rating age
28 yuppy 9 35.0
58 rusty 10 35.0
• Selects rows that satisfy
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.)
Projection sname rating
yuppy 9
lubber 8
guppy 5
rusty 10
πsname rating
S
,
( )2
age
35.0
55.5
πage S( )2
• 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?)
Union, Intersection, Set-
Difference
• All of these operations take
two input relations, which
must be union-compatible:
– Same number of fields.
– `Corresponding’ fields
have the same type.
• What is the schema of result?
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
44 guppy 5 35.0
28 yuppy 9 35.0
sid sname rating age
31 lubber 8 55.5
58 rusty 10 35.0
S S1 2∪
S S1 2∩
sid sname rating age
22 dustin 7 45.0
S S1 2−
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.
– Conflict: Both S1 and R1 have a field called sid.
(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
14
Division
• Not supported as a primitive operator, but useful
for expressing queries like:
Find sailors
who have reserved all boats.
• Precondition: in A/B, the attributes in B must be
included in the schema for A. Also, the result has
attributes A-B.
– SALES(supId, prodId);
– PRODUCTS(prodId);
– Relations SALES and PRODUCTS must be built using
projections.
– SALES/PRODUCTS: the ids of the suppliers supplying15
Examples of Division A/B
sno pno
s1 p1
s1 p2
s1 p3
s1 p4
s2 p1
s2 p2
s3 p2
s4 p2
s4 p4
pno
p2
pno
p2
p4
pno
p1
p2
p4
sno
s1
s2
s3
s4
sno
s1
s4
sno
s1
A
B1
B2
B3
A/B1 A/B2 A/B3
16
SET OPERATORS
SET operators are mainly used to combine the same
type of data from two or more tables. Although
more than one select statement will then be present,
only one result set is returned.
Four Set Operators:
Operator Returns 
UNION
Combine two or more result sets into a single set,
without duplicates.
UNION ALL
Combine two or more result sets into a single set,
including all duplicates.
INTERSECT
Takes the data from both result sets which are in
common.
EXCEPT
Takes the data from first result set,  but not the second
(i.e. no matching to each other)
17
Syntax:
SELECT [Column_Name, . . . ] FROM [table1]
[set operator]
SELECT [Column_Namse, . . .] FROM [table2]
Example
Create two tables with same column name and data
type.
CREATE TABLE Student1(
Name VARCHAR( 15),
TotalMark INT)
CREATE TABLE Stundent2(
Name VARCHAR(15),
TotalMark INT) 18
Let us insert a few values into the tables.
INSERT INTO Student1 VALUES('Robert',1063);
INSERT INTO Student1 VALUES('John',1070);
INSERT INTO Student1 VALUES('Rose',1032);
INSERT INTO Student1 VALUES('Abel',1002);
INSERT INTO Student2 VALUES('Robert',1063);
INSERT INTO Student2 VALUES('Rose',1032);
INSERT INTO Student2 VALUES('Boss',1086);
INSERT INTO Student2 VALUES('Marry',1034);
19
.Result Set for Students1 table
Result Set for Students2 table
20
UNION ALL
The SQL UNION ALL Operator is used to list all
records from two or more select statements.
All the records from both tables must be in
the same order.
SELECT * FROM student1 UNION ALL
SELECT * FROM student2
N.B you can use specific columns
21
Here Robert and Rose are stored in both tables. UNION ALL
retuns all records (including duplicate records).
22
Union
Union works like Distinct. Union all DOES NOT
do distinct.
SELECT * FROM student1 UNION
SELECT * FROM student2
The Robert and Rose records are duplicate
records. Thus, these are returned only once.
23
INTERSECT
INTERSECT returns any distinct values that are
returned by both the query on the left and
right sides of the INTERSECT operand.
SELECT * FROM student1INTERSECT
SELECT * FROM student2
Only the Robert and Rose records are returned,
because they are found in both tables. 24
.
EXCEPT
EXCEPT clause in SQL Server is working as like MINUS
operation in Oracle. EXCEPT query returns all rows
which are in the first query but those are not
returned in the second query.
Example 1
SELECT * FROM student1 EXCEPT
SELECT * FROM student2
25
.Example 2
EXCEPT returns any distinct values from the left select
query that are not also found on the right select
query.
SELECT * FROM student2 EXCEPT
SELECT * FROM student1
From the two results we understand that if any records
are found in both tables, they are removed from the
first table's record set.
26
Joins and join-like operators
• A JOIN puts the information from two tables together
into one result set. We can think of a result set as
being a “virtual” table. It has both columns and rows,
and the columns have data types.
• When data from more than one table in the database
is required, a join condition is used. Rows in one table
can be joined to rows on another table according to
common values existing in corresponding columns that
is usually primary and foreign keys.
27
Joins
• Condition Join:
Example
• Result schema same as that of cross-
product.
• Fewer tuples than cross-product. Filters
tuples not satisfying the join condition.
• Sometimes called a theta-join.
R c S c R S = ×σ ( )
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96
S R
S sid R sid
1 1
1 1

. .<
28
Cont..
Types of join.
1. Inner Join
– Equi-join
– Natural Join
2. Outer Join
– Left outer Join
– Right outer join
– Full outer join
3. Cross Join
4. Self Join
29
1. Inner join
A table join that involves selecting rows from the table
based on the equality of values in columns from each
table.
An inner join in SQL returns rows where there is at least
one match on both tables.
30
Cont…
• Equi-Join: A special case of condition join where
the condition c contains only equalities.
• Result schema similar to cross-product, but only
one copy of fields for which equality is specified.
• Natural Join: Equijoin on all common fields.
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
)11(,..,,..,
RS
sidbidagesid
π
31
.Example Let's assume that we have the following two
tables.
Create the following two tables and relate them
Create table student( Id int, Name char(20), Depid
varchar(20))
Create table dept( ddepid varchar(20) primary key,
Dname char(30))
The two tables are created then relate them
Alter table student add constraint foreign key (depid)
references dept (ddepid)
Now to create inner join
Select id,name,dname from student inner join dept where
depid=ddepid. id name dname
32
2. Outer join
Outer joins make use of the JOIN keyword between the
table names to let SQL Server know to mark a table
as “preserved”. There are three different types of
outer joins: LEFT, RIGHT, and FULL.
33
To work with joins, we need two tables. One table will
be called Department with columns of DepartmentId
and DepartmentName. The other table will be called
Employees with columns of LastName and
DepartmentId. The DepartmentId in the Department
table will be the primary key while the DepartmentId
in the Employee table will be the foreign key and
relate them.
Table Employee Department Table
34
.
2.1 LEFT outer JOIN : it displays all the rows from
first table and matched rows from second table .
(left table are persevered).
35
Cont….
Like other joins we must first use the SELECT statement
to declare which columns to select from the table, in
this case all of them. Next we declare what table we
want the columns selected from with the FROM
statement. We next use the LEFT JOIN keyword to
preserve the rows in the left table and then use the
ON keyword to output the same instances of
DepartmentId in both tables.
select * from employee left join dept on
employee.DeptId=dept.DeptId
36
.
Execute the query and make sure it completes
successfully. You should see a table with columns of
LastName, two DeptIds, and DeptName.
As you can see, all the rows in the Employee table are
displayed while we are missing the Marketing
department on the right side. This is because we Left
Joined the table, so all the rows on the left side
were preserved.
37
2.2 RIGHT outer Join :- Right outer join displays all the
rows of second table and matched rows from first table(right
table are persevered).
38
Cont…
In the next example we will RIGHT join the table and a
Marketing row will appear. The syntax is exactly the
same as the previous join, the only difference is we
change LEFT to RIGHT.
select * from employee right join dept on
employee.DeptId=dept.DeptId
Now you can see the marketing row
39
.2.3 FULL OUTER JOIN
A FULL join combines the LEFT and RIGHT joins
to get all rows from both tables. So all the
rows that appeared with the LEFT and RIGHT
join queries will appear together regardless of
null values.
40
Cont…
select * from employee left join dept on
employee.DeptId=dept.DeptId union
select * from employee right join dept on
employee.DeptId=dept.DeptId
41
3. Cross join
Cross join is the Cartesian product of tables and
its size is
Let m is the size of the record of the first table
n is the size of the record of the second table
Size=m*n
select * from employee cross join dept
42
Cont…
43
4) Self Join
• Joining the table itself called self join. Self join
is used to retrieve the records having some
relation or similarity with other records in the
same table. Here, we need to use aliases for
the same table to set a self join between
single table and retrieve records satisfying the
condition in where clause.
Example
SELECT e1.Username,e1.FirstName,e1.LastName
from Employee e1 _inner join Employee e2 on
e1.id=e2.DepartID
44
Assignment
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
bid bname color
101 Interlake Blue
102 Interlake Red
103 Clipper Green
104 Marine Red
sid bid day
22 101 10/10/96
58 103 11/12/96
Reserves
Sailors
Boats
45
Construct relational algebra and SQL queries for the
following statements based on the above tables .
1. Find names of sailors who’ve reserved boat #103
2. Find names of sailors who’ve reserved a red boat
3. Find sailors who’ve reserved a red boat or a green
boat
4. Find sailors who’ve reserved a red and a green boat
46

More Related Content

What's hot

Matlab ch1 (6)
Matlab ch1 (6)Matlab ch1 (6)
Matlab ch1 (6)mohsinggg
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra pptGirdharRatne
 
Relational algebra
Relational algebraRelational algebra
Relational algebraJoshi Vinay
 
3 2 representing functions
3 2 representing functions3 2 representing functions
3 2 representing functionslothomas
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and searchEstiak Khan
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Randa Elanwar
 
Selection sort
Selection sortSelection sort
Selection sortamna izzat
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureNurjahan Nipa
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Randa Elanwar
 
Relations and functions (Mariam)
Relations and functions (Mariam)Relations and functions (Mariam)
Relations and functions (Mariam)Mariam Bosraty
 
The fundamentals of regression
The fundamentals of regressionThe fundamentals of regression
The fundamentals of regressionStephanie Locke
 
Matlab practice
Matlab practiceMatlab practice
Matlab practiceZunAib Ali
 

What's hot (20)

Matlab ch1 (6)
Matlab ch1 (6)Matlab ch1 (6)
Matlab ch1 (6)
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Matlab
MatlabMatlab
Matlab
 
3 2 representing functions
3 2 representing functions3 2 representing functions
3 2 representing functions
 
single linked list
single linked listsingle linked list
single linked list
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
Dsu qb
Dsu qbDsu qb
Dsu qb
 
Selection sort
Selection sortSelection sort
Selection sort
 
Lecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structureLecture 02: Preliminaries of Data structure
Lecture 02: Preliminaries of Data structure
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and MatricesMATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4
 
Matlab tutorial 4
Matlab tutorial 4Matlab tutorial 4
Matlab tutorial 4
 
Relations and functions (Mariam)
Relations and functions (Mariam)Relations and functions (Mariam)
Relations and functions (Mariam)
 
The fundamentals of regression
The fundamentals of regressionThe fundamentals of regression
The fundamentals of regression
 
Matlab practice
Matlab practiceMatlab practice
Matlab practice
 
Basic concepts in_matlab
Basic concepts in_matlabBasic concepts in_matlab
Basic concepts in_matlab
 

Similar to Ch7

Chapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfChapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfTamiratDejene1
 
Relational Model
Relational ModelRelational Model
Relational ModelSiti Ismail
 
Distributed database
Distributed databaseDistributed database
Distributed databaseNasIr Irshad
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational AlgebraAmin Omi
 
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...RajuNestham1
 
Adbms 39 algorithms for project and set operations
Adbms 39 algorithms for project and set operationsAdbms 39 algorithms for project and set operations
Adbms 39 algorithms for project and set operationsVaibhav Khanna
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational modelATS SBGI MIRAJ
 
RelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdfRelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdf10GUPTASOUMYARAMPRAK
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica"FENG "GEORGE"" YU
 
Relational operation final
Relational operation finalRelational operation final
Relational operation finalStudent
 
Lesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfLesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfHasankaWijesinghe1
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptAnkush138
 
IBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinIBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinAjay Gupte
 
Chapter-6 Relational Algebra
Chapter-6 Relational AlgebraChapter-6 Relational Algebra
Chapter-6 Relational AlgebraKunal Anand
 

Similar to Ch7 (20)

Chapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfChapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdf
 
Relational Model
Relational ModelRelational Model
Relational Model
 
Distributed database
Distributed databaseDistributed database
Distributed database
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
lecture8Alg.ppt
lecture8Alg.pptlecture8Alg.ppt
lecture8Alg.ppt
 
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
lefg sdfg ssdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg sdfg d...
 
Adbms 39 algorithms for project and set operations
Adbms 39 algorithms for project and set operationsAdbms 39 algorithms for project and set operations
Adbms 39 algorithms for project and set operations
 
uniT 4 (1).pptx
uniT 4 (1).pptxuniT 4 (1).pptx
uniT 4 (1).pptx
 
Intro to relational model
Intro to relational modelIntro to relational model
Intro to relational model
 
354 ch6
354 ch6354 ch6
354 ch6
 
RelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdfRelationalAlgebra-RelationalCalculus-SQL.pdf
RelationalAlgebra-RelationalCalculus-SQL.pdf
 
ADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASADADVANCE ITT BY PRASAD
ADVANCE ITT BY PRASAD
 
Query Optimization - Brandon Latronica
Query Optimization - Brandon LatronicaQuery Optimization - Brandon Latronica
Query Optimization - Brandon Latronica
 
Relational operation final
Relational operation finalRelational operation final
Relational operation final
 
Lesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdfLesson 6 - Relational Algebra.pdf
Lesson 6 - Relational Algebra.pdf
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.ppt
 
IBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinIBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash Join
 
Chapter-6 Relational Algebra
Chapter-6 Relational AlgebraChapter-6 Relational Algebra
Chapter-6 Relational Algebra
 
Algebra
AlgebraAlgebra
Algebra
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 

Recently uploaded

Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Recently uploaded (20)

FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

Ch7

  • 2. Basic Terminologies • Relational algebra • Relational-algebra operations – Select – Project – Union ∪ – Set difference − – Cartesian product × – Rename 2
  • 3. … • Additional operations – Set intersection ∩ – Natural join – Assignment operation – Outer join • Left outer join • Right outer join • Full outer join 3
  • 4. Relational Algebra • The relational algebra defines a set of algebraic operations that operate on tables, and output tables as their results. • These operations can be combined to get expressions that express desired queries. • The algebra defines the basic operations used with in relational query languages. 4
  • 5. … • The operations in relational algebra can be divided into: – Basic operations – Additional operations that can be expressed in terms of the basic operations – Extended operations, some of which add further expressive power to relational algebra 5
  • 6. Relational Query Languages • Query languages: Allow manipulation and retrieval of data from a database. • Relational model supports simple, powerful QLs: – Strong formal foundation based on logic. – Allows for much optimization. • Query Languages != programming languages! – QLs not expected to be “Turing complete”. – QLs not intended to be used for complex calculations. – QLs support easy, efficient access to large data sets. 6
  • 7. Formal Relational Query Languages • Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: – Relational Algebra: More operational(procedural), very useful for representing execution plans. – Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non-operational, declarative.) 7
  • 8. Preliminaries • A query is applied to relation instances, and the result of a query is also a relation instance. – Schemas of input relations for a query are fixed (but query will run regardless of instance!) – The schema for the result of a given query is also fixed! Determined by definition of query language constructs. 8
  • 9. Example Instances 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 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 sid bid day 22 101 10/10/96 58 103 11/12/96 R1 S1 S2 • “Sailors” and “Reserves” relations for our examples. “bid”= boats. “sid”: sailors
  • 10. 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! (Algebra is “closed”.) σ π − ×  10
  • 11. Selection σrating S >8 2( ) sid sname rating age 28 yuppy 9 35.0 58 rusty 10 35.0 • Selects rows that satisfy 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.)
  • 12. Projection sname rating yuppy 9 lubber 8 guppy 5 rusty 10 πsname rating S , ( )2 age 35.0 55.5 πage S( )2 • 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?)
  • 13. Union, Intersection, Set- Difference • All of these operations take two input relations, which must be union-compatible: – Same number of fields. – `Corresponding’ fields have the same type. • What is the schema of result? sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 44 guppy 5 35.0 28 yuppy 9 35.0 sid sname rating age 31 lubber 8 55.5 58 rusty 10 35.0 S S1 2∪ S S1 2∩ sid sname rating age 22 dustin 7 45.0 S S1 2−
  • 14. 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. – Conflict: Both S1 and R1 have a field called sid. (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 14
  • 15. Division • Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats. • Precondition: in A/B, the attributes in B must be included in the schema for A. Also, the result has attributes A-B. – SALES(supId, prodId); – PRODUCTS(prodId); – Relations SALES and PRODUCTS must be built using projections. – SALES/PRODUCTS: the ids of the suppliers supplying15
  • 16. Examples of Division A/B sno pno s1 p1 s1 p2 s1 p3 s1 p4 s2 p1 s2 p2 s3 p2 s4 p2 s4 p4 pno p2 pno p2 p4 pno p1 p2 p4 sno s1 s2 s3 s4 sno s1 s4 sno s1 A B1 B2 B3 A/B1 A/B2 A/B3 16
  • 17. SET OPERATORS SET operators are mainly used to combine the same type of data from two or more tables. Although more than one select statement will then be present, only one result set is returned. Four Set Operators: Operator Returns  UNION Combine two or more result sets into a single set, without duplicates. UNION ALL Combine two or more result sets into a single set, including all duplicates. INTERSECT Takes the data from both result sets which are in common. EXCEPT Takes the data from first result set,  but not the second (i.e. no matching to each other) 17
  • 18. Syntax: SELECT [Column_Name, . . . ] FROM [table1] [set operator] SELECT [Column_Namse, . . .] FROM [table2] Example Create two tables with same column name and data type. CREATE TABLE Student1( Name VARCHAR( 15), TotalMark INT) CREATE TABLE Stundent2( Name VARCHAR(15), TotalMark INT) 18
  • 19. Let us insert a few values into the tables. INSERT INTO Student1 VALUES('Robert',1063); INSERT INTO Student1 VALUES('John',1070); INSERT INTO Student1 VALUES('Rose',1032); INSERT INTO Student1 VALUES('Abel',1002); INSERT INTO Student2 VALUES('Robert',1063); INSERT INTO Student2 VALUES('Rose',1032); INSERT INTO Student2 VALUES('Boss',1086); INSERT INTO Student2 VALUES('Marry',1034); 19
  • 20. .Result Set for Students1 table Result Set for Students2 table 20
  • 21. UNION ALL The SQL UNION ALL Operator is used to list all records from two or more select statements. All the records from both tables must be in the same order. SELECT * FROM student1 UNION ALL SELECT * FROM student2 N.B you can use specific columns 21
  • 22. Here Robert and Rose are stored in both tables. UNION ALL retuns all records (including duplicate records). 22
  • 23. Union Union works like Distinct. Union all DOES NOT do distinct. SELECT * FROM student1 UNION SELECT * FROM student2 The Robert and Rose records are duplicate records. Thus, these are returned only once. 23
  • 24. INTERSECT INTERSECT returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand. SELECT * FROM student1INTERSECT SELECT * FROM student2 Only the Robert and Rose records are returned, because they are found in both tables. 24
  • 25. . EXCEPT EXCEPT clause in SQL Server is working as like MINUS operation in Oracle. EXCEPT query returns all rows which are in the first query but those are not returned in the second query. Example 1 SELECT * FROM student1 EXCEPT SELECT * FROM student2 25
  • 26. .Example 2 EXCEPT returns any distinct values from the left select query that are not also found on the right select query. SELECT * FROM student2 EXCEPT SELECT * FROM student1 From the two results we understand that if any records are found in both tables, they are removed from the first table's record set. 26
  • 27. Joins and join-like operators • A JOIN puts the information from two tables together into one result set. We can think of a result set as being a “virtual” table. It has both columns and rows, and the columns have data types. • When data from more than one table in the database is required, a join condition is used. Rows in one table can be joined to rows on another table according to common values existing in corresponding columns that is usually primary and foreign keys. 27
  • 28. Joins • Condition Join: Example • Result schema same as that of cross- product. • Fewer tuples than cross-product. Filters tuples not satisfying the join condition. • Sometimes called a theta-join. R c S c R S = ×σ ( ) (sid) sname rating age (sid) bid day 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 58 103 11/12/96 S R S sid R sid 1 1 1 1  . .< 28
  • 29. Cont.. Types of join. 1. Inner Join – Equi-join – Natural Join 2. Outer Join – Left outer Join – Right outer join – Full outer join 3. Cross Join 4. Self Join 29
  • 30. 1. Inner join A table join that involves selecting rows from the table based on the equality of values in columns from each table. An inner join in SQL returns rows where there is at least one match on both tables. 30
  • 31. Cont… • Equi-Join: A special case of condition join where the condition c contains only equalities. • Result schema similar to cross-product, but only one copy of fields for which equality is specified. • Natural Join: Equijoin on all common fields. sid sname rating age bid day 22 dustin 7 45.0 101 10/10/96 58 rusty 10 35.0 103 11/12/96 )11(,..,,.., RS sidbidagesid π 31
  • 32. .Example Let's assume that we have the following two tables. Create the following two tables and relate them Create table student( Id int, Name char(20), Depid varchar(20)) Create table dept( ddepid varchar(20) primary key, Dname char(30)) The two tables are created then relate them Alter table student add constraint foreign key (depid) references dept (ddepid) Now to create inner join Select id,name,dname from student inner join dept where depid=ddepid. id name dname 32
  • 33. 2. Outer join Outer joins make use of the JOIN keyword between the table names to let SQL Server know to mark a table as “preserved”. There are three different types of outer joins: LEFT, RIGHT, and FULL. 33
  • 34. To work with joins, we need two tables. One table will be called Department with columns of DepartmentId and DepartmentName. The other table will be called Employees with columns of LastName and DepartmentId. The DepartmentId in the Department table will be the primary key while the DepartmentId in the Employee table will be the foreign key and relate them. Table Employee Department Table 34
  • 35. . 2.1 LEFT outer JOIN : it displays all the rows from first table and matched rows from second table . (left table are persevered). 35
  • 36. Cont…. Like other joins we must first use the SELECT statement to declare which columns to select from the table, in this case all of them. Next we declare what table we want the columns selected from with the FROM statement. We next use the LEFT JOIN keyword to preserve the rows in the left table and then use the ON keyword to output the same instances of DepartmentId in both tables. select * from employee left join dept on employee.DeptId=dept.DeptId 36
  • 37. . Execute the query and make sure it completes successfully. You should see a table with columns of LastName, two DeptIds, and DeptName. As you can see, all the rows in the Employee table are displayed while we are missing the Marketing department on the right side. This is because we Left Joined the table, so all the rows on the left side were preserved. 37
  • 38. 2.2 RIGHT outer Join :- Right outer join displays all the rows of second table and matched rows from first table(right table are persevered). 38
  • 39. Cont… In the next example we will RIGHT join the table and a Marketing row will appear. The syntax is exactly the same as the previous join, the only difference is we change LEFT to RIGHT. select * from employee right join dept on employee.DeptId=dept.DeptId Now you can see the marketing row 39
  • 40. .2.3 FULL OUTER JOIN A FULL join combines the LEFT and RIGHT joins to get all rows from both tables. So all the rows that appeared with the LEFT and RIGHT join queries will appear together regardless of null values. 40
  • 41. Cont… select * from employee left join dept on employee.DeptId=dept.DeptId union select * from employee right join dept on employee.DeptId=dept.DeptId 41
  • 42. 3. Cross join Cross join is the Cartesian product of tables and its size is Let m is the size of the record of the first table n is the size of the record of the second table Size=m*n select * from employee cross join dept 42
  • 44. 4) Self Join • Joining the table itself called self join. Self join is used to retrieve the records having some relation or similarity with other records in the same table. Here, we need to use aliases for the same table to set a self join between single table and retrieve records satisfying the condition in where clause. Example SELECT e1.Username,e1.FirstName,e1.LastName from Employee e1 _inner join Employee e2 on e1.id=e2.DepartID 44
  • 45. Assignment sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 bid bname color 101 Interlake Blue 102 Interlake Red 103 Clipper Green 104 Marine Red sid bid day 22 101 10/10/96 58 103 11/12/96 Reserves Sailors Boats 45
  • 46. Construct relational algebra and SQL queries for the following statements based on the above tables . 1. Find names of sailors who’ve reserved boat #103 2. Find names of sailors who’ve reserved a red boat 3. Find sailors who’ve reserved a red boat or a green boat 4. Find sailors who’ve reserved a red and a green boat 46

Editor's Notes

  1. The slides for this text are organized into chapters. This lecture covers relational algebra from Chapter 4. The relational calculus part can be found in Chapter 4, Part B. Chapter 1: Introduction to Database Systems Chapter 2: The Entity-Relationship Model Chapter 3: The Relational Model Chapter 4 (Part A): Relational Algebra Chapter 4 (Part B): Relational Calculus Chapter 5: SQL: Queries, Programming, Triggers Chapter 6: Query-by-Example (QBE) Chapter 7: Storing Data: Disks and Files Chapter 8: File Organizations and Indexing Chapter 9: Tree-Structured Indexing Chapter 10: Hash-Based Indexing Chapter 11: External Sorting Chapter 12 (Part A): Evaluation of Relational Operators Chapter 12 (Part B): Evaluation of Relational Operators: Other Techniques Chapter 13: Introduction to Query Optimization Chapter 14: A Typical Relational Optimizer Chapter 15: Schema Refinement and Normal Forms Chapter 16 (Part A): Physical Database Design Chapter 16 (Part B): Database Tuning Chapter 17: Security Chapter 18: Transaction Management Overview Chapter 19: Concurrency Control Chapter 20: Crash Recovery Chapter 21: Parallel and Distributed Databases Chapter 22: Internet Databases Chapter 23: Decision Support Chapter 24: Data Mining Chapter 25: Object-Database Systems Chapter 26: Spatial Data Management Chapter 27: Deductive Databases Chapter 28: Additional Topics