Relational Databases: Query Languages and Relational Algebra, Study notes of Algebra

A query is simply a relational algebra expression ... (Our examples also generally have same attribute names, but not required! Arity and domains are what ...

Typology: Study notes

2021/2022

Uploaded on 09/07/2022

nabeel_kk
nabeel_kk šŸ‡øšŸ‡¦

4.6

(65)

1.3K documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
RELATIONAL ALGEBRA
CS121: Relational Databases
Fall 2018 – Lecture 2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

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