0% found this document useful (0 votes)
84 views17 pages

Database 04 Relational Algebra Part1

Uploaded by

Rasd sda
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)
84 views17 pages

Database 04 Relational Algebra Part1

Uploaded by

Rasd sda
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/ 17

Query Languages

Language in which user requests information from the database.


Categories of languages
procedural
non-procedural
“Pure” languages:
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Pure languages form underlying basis of query languages
that people use.

Database System Concepts 3.1 ©Silberschatz, Korth and Sudarshan


Relational Algebra
Procedural language
Six basic operators
select
project
union
set difference
Cartesian product
rename
The operators take one or more relations as inputs and give a
new relation as a result.

Database System Concepts 3.2 ©Silberschatz, Korth and Sudarshan


Select Operation – Example

• Relation r A B C D

  1 7
  5 7
  12 3
  23 10

• A=B ^ D > 5 (r)


A B C D

  1 7
  23 10

Database System Concepts 3.3 ©Silberschatz, Korth and Sudarshan


Select Operation

Notation:  p(r)
p is called the selection predicate
Defined as:
p(r) = {t | t  r and p(t)}
Where p is a formula in propositional calculus consisting
of terms connected by :  (and),  (or),  (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <. 
Example of selection:
 branch-name=“Perryridge”(account)

Database System Concepts 3.4 ©Silberschatz, Korth and Sudarshan


Project Operation – Example

Relation r: A B C

 10 1
 20 1
 30 1
 40 2

A,C (r) A C A C

 1  1
 1 =  1
 1  2
 2

Database System Concepts 3.5 ©Silberschatz, Korth and Sudarshan


Project Operation
Notation:

A1, A2, …, Ak (r)


where A1, A2 are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by
erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
E.g. To eliminate the branch-name attribute of account
account-number, balance (account)

Database System Concepts 3.6 ©Silberschatz, Korth and Sudarshan


Union Operation – Example

Relations r, s: A B A B

 1  2
 2  3
 1 s
r

r  s: A B

 1
 2
 1
 3

Database System Concepts 3.7 ©Silberschatz, Korth and Sudarshan


Union Operation
Notation: r  s
Defined as:
r  s = {t | t  r or t  s}

For r  s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (e.g., 2nd column
of r deals with the same type of values as does the 2nd
column of s)
E.g. to find all customers with either an account or a loan
customer-name (depositor)  customer-name (borrower)

Database System Concepts 3.8 ©Silberschatz, Korth and Sudarshan


Set Difference Operation – Example

Relations r, s: A B A B

 1  2
 2  3
 1 s
r

r – s: A B

 1
 1

Database System Concepts 3.9 ©Silberschatz, Korth and Sudarshan


Set Difference Operation
Notation r – s
Defined as:
r – s = {t | t  r and t  s}
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible

Database System Concepts 3.10 ©Silberschatz, Korth and Sudarshan


Cartesian-Product Operation-Example

Relations r, s: A B C D E

 1  10 a
 10 a
 2
 20 b
r  10 b
s
r x s:
A B C D E
 1  10 a
 1  10 a
 1  20 b
 1  10 b
 2  10 a
 2  10 a
 2  20 b
 2  10 b

Database System Concepts 3.11 ©Silberschatz, Korth and Sudarshan


Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t  r and q  s}
Assume that attributes of r(R) and s(S) are disjoint. (That is,
R  S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming must
be used.

Database System Concepts 3.12 ©Silberschatz, Korth and Sudarshan


Composition of Operations
Can build expressions using multiple operations
Example: A=C(r x s)
rxs A B C D E
 1  10 a
 1  10 a
 1  20 b
 1  10 b
 2  10 a
 2  10 a
 2  20 b
 2  10 b
A=C(r x s)
A B C D E

 1  10 a
 2  20 a
 2  20 b
Database System Concepts 3.13 ©Silberschatz, Korth and Sudarshan
Rename Operation
Allows us to name, and therefore to refer to, the results of
relational-algebra expressions.
Allows us to refer to a relation by more than one name.
Example:
 x (E)
returns the expression E under the name X
If a relational-algebra expression E has arity n, then
x (A1, A2, …, An) (E)
returns the result of expression E under the name X, and with
the attributes renamed to A1, A2, …., An.

Database System Concepts 3.14 ©Silberschatz, Korth and Sudarshan


Banking Example

branch (branch-name, branch-city, assets)

customer (customer-name, customer-street, customer-only)

account (account-number, branch-name, balance)

loan (loan-number, branch-name, amount)

depositor (customer-name, account-number)

borrower (customer-name, loan-number)

Database System Concepts 3.15 ©Silberschatz, Korth and Sudarshan


Example Queries

Find all loans of over $1200

amount > 1200 (loan)

Find the loan number for each loan of an amount greater than
$1200

loan-number (amount > 1200 (loan))

Database System Concepts 3.16 ©Silberschatz, Korth and Sudarshan


Example Queries

Find the names of all customers who have a loan, an account, or


both, from the bank

customer-name (borrower)  customer-name (depositor)

Find the names of all customers who have a loan and an


account at bank.

customer-name (borrower)  customer-name (depositor)

Database System Concepts 3.17 ©Silberschatz, Korth and Sudarshan

You might also like