Download Relational Databases: Query Languages and Relational Algebra and more Study notes Algebra in PDF only on Docsity!
RELATIONAL ALGEBRA
CS121: Relational Databases
Fall 2018 ā Lecture 2
Query Languages
ĀØ A query language specifies how to access the data in
the database
ĀØ Different kinds of query languages:
¤ Declarative languages specify what data to retrieve, but not
how to retrieve it
¤ Procedural languages specify what to retrieve, as well as
the process for retrieving it
ĀØ Query languages often include updating and deleting
data as well
ĀØ Also called data manipulation language (DML)
2
āWhy is this useful?ā
ĀØ SQL is only loosely based on relational algebra
ĀØ SQL is much more on the ādeclarativeā end of the
spectrum
ĀØ Many relational databases use relational algebra
operations for representing execution plans
¤ Simple, clean, effective abstraction for representing
how results will be generated
¤ Relatively easy to manipulate for query optimization
4
Fundamental Relational Algebra Operations ĀØ Six fundamental operations: Ļ select operation Ī project operation āŖ set-union operation
- set-difference operation Ć Cartesian product operation Ļ rename operation ĀØ Each operation takes one or two relations as input ĀØ Produces another relation as output ĀØ Important details: ¤ What tuples are included in the result relation? ¤ Any constraints on input schemas? What is schema of result? 5
Select Examples
Using the account relation:
āRetrieve all tuples for accounts
in the Los Angeles branch.ā
Ļ branch_name =āLos Angelesā( account )
āRetrieve all tuples for accounts
in the Los Angeles branch,
with a balance under $300.ā
Ļ branch_name =āLos Angelesā ā§ balance <300( account )
acct_id branch_name balance A- 301 A- 307 A- 318 A- 319 A- 322 New York Seattle Los Angeles New York Los Angeles
account acct_id branch_name balance A- 318 A- 322 Los Angeles Los Angeles
acct_id branch_name balance A- 322 Los Angeles 275 7
Project Operation
ĀØ Written as: Ī
a,b,ā¦
(r)
ĀØ Result relation contains only specified attributes of r
¤ Specified attributes must actually be in schema of r
¤ Resultās schema only contains the specified attributes
¤ Domains are same as source attributesā domains
ĀØ Important note:
¤ Result relation may have fewer rows than input relation!
¤ Why?
n Relations are sets of tuples, not multisets
8
Composing Operations
ĀØ Input can also be an expression that evaluates to a
relation, instead of just a relation
acct_id
balance ā„ 300
( account ))
¤ Selects the account IDs of all accounts with a balance of
$300 or more
¤ Input relationās schema is:
Account_schema = ( acct_id , branch_name , balance )
¤ Final result relationās schema?
n Just one attribute: ( acct_id )
ĀØ Distinguish between base and derived relations
¤ account is a base relation
¤ Ļ balance ā„ 300 ( account ) is a derived relation
10
Set-Union Operation
ĀØ Written as: r āŖ s
ĀØ Result contains all tuples from r and s
¤ Each tuple is unique, even if itās in both r and s
ĀØ Constraints on schemas for r and s?
ĀØ r and s must have compatible schemas:
¤ r and s must have same arity
n (same number of attributes)
¤ For each attribute i in r and s , r [i] must have the same
domain as s [i]
¤ (Our examples also generally have same attribute names,
but not required! Arity and domains are what matter.)
11
Set-Union Example (2)
ĀØ Find names of all customers that have either a
bank account or a loan at the bank
acct_id branch_name balance A- 301 A- 307 A- 318 A- 319 A- 322 New York Seattle Los Angeles New York Los Angeles
account loan_id branch_name amount L- 421 L- 445 L- 437 L- 419 San Francisco Los Angeles Las Vegas Seattle
loan cust_name acct_id Johnson Smith Reynolds Lewis Reynolds
A- 318
A- 322
A- 319
A- 307
A- 301
depositor cust_name loan_id Anderson Jackson Lewis Smith
L- 437
L- 419
L- 421
L- 445
borrower 13
Set-Union Example (3)
ĀØ Find names of all customers that have either a bank
account or a loan at the bank
¤ Easy to find the customers
with an account:
Ī cust_name ( depositor )
¤ Also easy to find customers
with a loan:
Ī cust_name ( borrower )
ĀØ Result is set-union of these expressions:
Ī cust_name ( depositor ) āŖ Ī cust_name ( borrower )
¤ Note that inputs have 8 tuples,
but result has 6 tuples.
cust_name Johnson Smith Reynolds Lewis cust_name Anderson Jackson Lewis Smith cust_name Johnson Smith Reynolds Lewis Anderson Jackson Ī cust_name ( depositor ) Ī cust_name ( borrower ) 14
Set-Difference Example
āFind all customers that have an account but not a loan.ā
acct_id branch_name balance A- 301 A- 307 A- 318 A- 319 A- 322 New York Seattle Los Angeles New York Los Angeles
account loan_id branch_name amount L- 421 L- 445 L- 437 L- 419 San Francisco Los Angeles Las Vegas Seattle
loan cust_name acct_id Johnson Smith Reynolds Lewis Reynolds
A- 318
A- 322
A- 319
A- 307
A- 301
depositor cust_name loan_id Anderson Jackson Lewis Smith
L- 437
L- 419
L- 421
L- 445
borrower 16
Set-Difference Example (2)
ĀØ Again, each component is easy
¤ All customers that have an account:
cust_name
( depositor )
¤ All customers that have a loan:
Ī cust_name ( borrower )
ĀØ Result is set-difference of these expressions
cust_name
( depositor ) ā Ī
cust_name
( borrower )
cust_name Johnson Smith Reynolds Lewis cust_name Anderson Jackson Lewis Smith cust_name Johnson Reynolds Ī cust_name ( depositor ) Ī cust_name ( borrower ) 17
Cartesian Product Operation (2)
ĀØ Result of r Ć s
¤ Contains every tuple in r , combined with every tuple in s
¤ If r contains N
r
tuples, and s contains N
s
tuples, result
contains Nr Ć Ns tuples
ĀØ Allows two relations to be compared and/or
combined
¤ If we want to correlate tuples in relation r with tuples in
relation s ā¦
¤ Compute r às , then select out desired results with an
appropriate predicate
19
Cartesian Product Example
ĀØ Compute result of borrower Ć loan
ĀØ Result will contain 4 Ć 4 = 16 tuples
loan_id branch_name amount L- 421 L- 445 L- 437 L- 419 San Francisco Los Angeles Las Vegas Seattle
loan cust_name loan_id Anderson Jackson Lewis Smith
L- 437
L- 419
L- 421
L- 445
borrower 20