Sie sind auf Seite 1von 3

CHAPTER 4

Relational Algebra

4.3.2. Projection Operation


4.1 Definition of Terms
- works on a single relation and defines a
 RELATIONAL ALGEBRA - is a relation-at-a-
relation that contains a vertical subset,
time (or set) language in which all tuples are
extracting the values of specified attributes
manipulation in one statement without looping.
and eliminating duplicates.
 UNION COMPATIBLE – condition where Union
operation is possible if the schemas of the two
Consider the relation “Staff”
relations match, that is, if they have the same
Example1: Produce a list of salaries for all staff,
number of attributes with each pair of
showing only the StaffNo, Fname,
corresponding attributes having the same Lname & Salary details
domain.
4.3 Set Operations
Fundamental Operations
- Binary operation involving two (2) or more
 SELECT
relation.
 PROJECT
- Relations are sets, so we can apply set-theoretic
 UNION operators.
 SET DIFFERENCE 4.4.1. Union Operation
 CARTESIAN PRODUCT
 is used when we need some attributes
 RENAME that appear in either or both of the two
Notations relations.
 Must be union-compatible.
Operation Symb Operation Symbo Example: Given the relation Graduates & Managers,
ol l determine the union of Graduates &
Select  Intersection  Managers

Project  Difference - GRADUATES

Union  Assignment  Number


7274
Surname
Robinson
Age
37
7432 O’Malley 39
4.2 Unary Operations 9824 Darkes 38

- they operate on one relation MANAGERS


4.3.1. Selection Operation Number Surname Age
9297 O’Malley 56
- Works on a single relation and defines a 7432 O’Malley 39
relation that contains only those tuples that 9824 Darkes 38
satisfy the specified condition (predicate).
Examples:

Consider the relation "Book" List the number, surname and age where there are
either graduates or managers.
AccountNu Yr_pub Title
m
4.4.2. Difference Operation
734216 2002 Algorithm Design
237235 2005 Database Systems  Finds tuples in one relation but not in
631523 2002 Compiler Design
other. Meaning, tuples that are in R, but
543211 2011 Programming
376112 2008 Machine Language not in S.
 Must be union-compatible
Example1: Select from the relation “Book” all the
books whose year of publication is 2002.
Example: Given the relation Graduates & Managers,

Example2: Select from the relation “Book” all the books determine the difference of
whose Acc-no is greater than equal Graduates & Managers
to 56782.
R
GRADUATES X Y
Number Surname Age a 1
7274 Robinson 37 b 2
7432 O’Malley 39 a 2
9824 Darkes 38 R x S
R.X R.Y S.Y S.Z
MANAGERS
a 1 3 1a
Number Surname Age a 1 2 2b
9297 O’Malley 56 b 2 3 1a
7432 O’Malley 39 b 2 2 2b
9824 Darkes 38 a 2 3 1a
a 2 2 2b

List the number, surname and age where there


is/are graduates but were not managers.
Example 2: List the names and comments of all clients
4.4.3. Intersection Operation
who have viewed a property for rent.
 Finds tuples in both the relations.
 Defines a relation consisting of the set of 4.4 Join Operations
all tuples that are in both R and S. - To retrieve data using multiple tables, first join the
 Must be union-compatible. tables then formulate a query using commands for
a single table query.
Example: Given the relation Graduates & Managers, - To qualify column names use the format :
determine the intersection of table name.column name

Graduates & Managers To Join Tables


1. In the SELECT clause, list all columns you want
GRADUATES
to display
Number Surname Age
7274 Robinson 37 2. In the FROM clause, list all tables involved in
7432 O’Malley 39 the query
9824 Darkes 38
3. In the WHERE clause, list the condition that
restricts the data to be retrieved
MANAGERS
Special Operations
Number Surname Age
9297 O’Malley 56 a. INNER JOIN : A join that compares
7432 O’Malley 39 the tables in a FROM clause and lists
9824 Darkes 38
only those rows that satisfy the condition
in the WHERE clause.
b. OUTER JOIN : List all the rows from
List the number, surname and age where there one of the tables, regardless of whether
is/are both graduates and manager. they match any rows in a second table.

4.4.4. Cartesian Product 1. Left Outer Join : all rows from the
table on the left are included,
 Allows combining information from two
regardless of whether they match
relations.
rows from the table on the right.
 Defines relation that is the concatenation
of every tuple of relation R with every tuple
of relation S. 2. Right Outer Join : all rows from the

 Multiplies two relation to define another table on the right are included,

S of all possible pairs of


relation consisting regardless of whether they match

tuples from the two Y


relations.Z rows from the table on the right.
3 1a
Example 1: 2 2b 3. Full Join : all rows from both tables are
included regardless of whether they
match rows from the other table.

Das könnte Ihnen auch gefallen