0% found this document useful (0 votes)
87 views15 pages

Relational Model: - Example: If

The document discusses the relational model for databases. It covers the basic structure of relations including attributes, domains, tuples, relation schemas, and relation instances. It also describes keys, query languages, and fundamental relational algebra operations including select, project, union, difference, Cartesian product, and rename. Relational algebra provides procedural operations to query and manipulate relations in a database.

Uploaded by

swet bhushan
Copyright
© Attribution Non-Commercial (BY-NC)
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)
87 views15 pages

Relational Model: - Example: If

The document discusses the relational model for databases. It covers the basic structure of relations including attributes, domains, tuples, relation schemas, and relation instances. It also describes keys, query languages, and fundamental relational algebra operations including select, project, union, difference, Cartesian product, and rename. Relational algebra provides procedural operations to query and manipulate relations in a database.

Uploaded by

swet bhushan
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 15

Relational Model

Chapter
• Structure of Relational Databases

• Fundamental Relational-Algebra-Operations
Relational Model
• Additional Relational-Algebra-Operations

• Extended Relational-Algebra-Operations

• Null Values

• Modification of the Database

Example of a Relation Basic Structure

• Formally, given sets D1, D2, …. Dn a relation r is a


subset of
D1 x D2 x … x Dn

Thus, a relation is a set of n-tuples (a1, a2, …, an)


where each ai ∈ Di

Basic Structure contd… Attribute Types


• Each attribute of a relation has a name
• Example: If
• The set of allowed values for each attribute is
customer_name = {Jones, Smith, Curry, Lindsay}
called the domain of the attribute
customer_street = {Main, North, Park}
customer_city = {Harrison, Rye, Pittsfield} • Attribute values are (normally) required to be
Then r = { (Jones, Main, Harrison), atomic; that is, indivisible
(Smith, North, Rye), – Note: multivalued attribute values are not atomic
(Curry, North, Rye),
– Note: composite attribute values are not atomic
(Lindsay, Park, Pittsfield) }
• The special value null is a member of every
is a relation over
customer_name x customer_street x domain
customer_city • The null value causes complications in the
definition of many operations

1
Relation Schema Relation Instance

• A1, A2, …, An are attributes • The current values (relation instance) of a


relation are specified by a table
• R = (A1, A2, …, An ) is a relation schema • An element t of r is a tuple, represented by
a row in a table attributes
Example:
(or columns)
Customer_schema = (customer_name, customer_name customer_street customer_city

customer_street, customer_city) Jones Main Harrison


Smith North Rye tuples
Curry North Rye (or rows)
Lindsay Park Pittsfield
• r(R) is a relation on the relation schema R
customer
Example:
customer (Customer_schema)

Relations are Unordered Database


„ Order of tuples is irrelevant (tuples may be stored in • A database consists of multiple relations
an arbitrary order) • Information about an enterprise is broken up into parts, with
each relation storing one part of the information
„ Example: account relation with unordered tuples
• account : stores information about accounts
• depositor : stores information about which customer owns
which account
• customer : stores information about customers
• Storing all information as a single relation such as
bank(account_number, balance, customer_name, ..)
results in
– repetition of information (e.g., two customers own an
account)
– the need for null values (e.g., represent a customer
without an account)
• Normalization theory deals with how to design relational
schemas

The customer Relation The depositor Relation

2
Keys Query Languages
• Let K ⊆ R
• Language in which user requests information from the
• K is a superkey of R if values for K are sufficient to identify a
unique tuple of each possible relation r(R) database.
– by “possible r ” we mean a relation r that could exist in the • Categories of languages
enterprise we are modeling.
– Procedural
– Example: {customer_name, customer_street} and
{customer_name} – Non-procedural, or declarative
are both superkeys of Customer, if no two customers can • “Pure” languages:
possibly have the same name.
– Relational algebra
• K is a candidate key if K is minimal
Example: {customer_name} is a candidate key for Customer, – Tuple relational calculus
since it is a superkey (assuming no two customers can – Domain relational calculus
possibly have the same name), and no subset of it is a
superkey. • Pure languages form underlying basis of query languages that
• Primary Key people use.

Relational Algebra Select Operation – Example


„ Relation r
• Procedural language A B C D

• Six basic operators α α 1 7


– select: σ α β 5 7

– project: ∏ β β 12 3
β β 23 10
– union: ∪
– set difference: –
ƒ σA=B ^ D > 5 (r)
– Cartesian product: x A B C D
– rename: ρ
α α 1 7
• The operators take one or two relations as inputs and β β 23 10
produce a new relation as a result.

Select Operation Project Operation – Example

• Notation: σ p(r) • Relation r: A B C


• p is called the selection predicate
α 10 1
• Defined as:
α 20 1
σp(r) = {t | t ∈ r and p(t)} β 30 1
β 40 2
Where p is a formula in propositional calculus consisting of
terms connected by : ∧ (and), ∨ (or), ¬ (not)
∏A,C (r) A C A C
Each term is one of:
<attribute>op <attribute> or <constant> α 1 α 1

where op is one of: =, ≠, >, ≥. <. ≤ α 1 = β 1


β 1 β 2
• Example of selection: β 2

σ branch_name=“Perryridge”(account)

3
Project Operation Union Operation – Example

• Relations A B A B
• Notation: ∏ A1 , A2 ,K, Ak
(r ) r, s:
α 1 α 2
α 2 β 3
where A1, A2 are attribute names and r is a relation name. β 1 s
r
• The result is defined as the relation of k columns obtained by
A B
erasing the columns that are not listed
„ r ∪ s: α 1
• Duplicate rows removed from result, since relations are sets
α 2

• Example: To eliminate the branch_name attribute of account β 1


β 3

∏account_number, balance (account)

Union Operation Set Difference Operation – Example

• Notation: r ∪ s • Relations r, s:A B A B

• Defined as: α 1 α 2
α 2 β 3
r ∪ s = {t | t ∈ r or t ∈ s}
β 1 s
• For r ∪ s to be valid. r

1. r, s must have the same arity (same number of attributes)


„ r – s:
A B
2. The attribute domains must be compatible (example: 2nd
α 1
column of r deals with the same type of values as does the 2nd
β 1
column of s)
• Example: to find all customers with either an account or a loan
∏customer_name (depositor) ∪ ∏customer_name (borrower)

Set Difference Operation Cartesian-Product Operation – Example


„ Relations r, s:
• Notation r – s A B C D E

α 1 α 10 a
• Defined as: β 10 a
β 2
β 20 b
r – s = {t | t ∈ r and t ∉ s} r γ 10 b
s
„ r x s:
A B C D E
• Set differences must be taken between compatible relations. α 1 α 10 a
α 1 β 10 a
– r and s must have the same arity α 1 β 20 b
α 1 γ 10 b
– attribute domains of r and s must be compatible β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
β 2 γ 10 b

4
Cartesian-Product Operation Composition of Operations
• Can build expressions using multiple
• Notation r x s operations
• Example: σA=C(r x s) A B C D E
• Defined as:
• rxs α 1 α 10 a
α 1 β 10 a
r x s = {t q | t ∈ r and q ∈ s} α 1 β 20 b
α 1 γ 10 b
β 2 α 10 a
β 2 β 10 a
β 2 β 20 b
• Assume that attributes of r(R) and s(S) are disjoint. (That is, R β 2 γ 10 b

∩ S = ∅).
• σA=C(r x s) A B C D E
• If attributes of r(R) and s(S) are not disjoint, then renaming α 1 α 10 a
β 2 β 10 a
must be used. β 2 β 20 b

Rename Operation Banking Example

• Allows us to name, and therefore to refer to, the results of branch (branch_name, branch_city, assets)
relational-algebra expressions.
• Allows us to refer to a relation by more than one name. customer (customer_name, customer_street,
customer_city)
• Example:
ρ x (E) account (account_number, branch_name, balance)

returns the expression E under the name X loan (loan_number, branch_name, amount)
• If a relational-algebra expression E has arity n, then
ρ x ( A ,A
1 2 ,..., A n ) (E ) depositor (customer_name, account_number)

returns the result of expression E under the name X, and with borrower (customer_name, loan_number)
the
attributes renamed to A1 , A2 , …., An .

Example Queries Example Queries

• Find all loans of over $1200 • Find the names of all customers who have a loan,
an account, or both, from the bank
σamount > 1200 (loan)
∏customer_name (borrower) ∪ ∏customer_name (depositor)

„ Find the loan number for each loan of an amount „ Find the names of all customers who have a loan
greater than $1200 and an account at bank.
∏loan_number (σamount > 1200 (loan))
∏customer_name (borrower) ∩ ∏customer_name (depositor)

5
Example Queries Example Queries

• Find the names of all customers who have a loan at the • Find the names of all customers who have a loan at the
Perryridge branch. Perryridge branch.
∏customer_name (σbranch_name=“Perryridge” z Query 1

(σborrower.loan_number = loan.loan_number(borrower x ∏customer_name (σbranch_name = “Perryridge” (


loan))) σborrower.loan_number = loan.loan_number (borrower x loan)))
„ Find the names of all customers who have a loan at
the Perryridge branch but do not have an account at any
branch of the bank. z Query 2

∏customer_name(σloan.loan_number = borrower.loan_number (
∏customer_name (σbranch_name = “Perryridge”
(σbranch_name = “Perryridge” (loan)) x borrower))
(σborrower.loan_number = loan.loan_number(borrower x loan))) –
∏customer_name(depositor)

Example Queries Formal Definition

• Find the largest account balance


• A basic expression in the relational algebra
– Strategy:
consists of either one of the following:
• Find those balances that are not the largest
– Rename account relation as d so that we – A relation in the database
can compare each account balance with all – A constant relation
others
• Use set difference to find those account
• Let E1 and E2 be relational-algebra
balances that were not found in the earlier expressions; the following are all relational-
step. algebra expressions:
– The query is:
– E1 ∪ E2
∏balance(account) - ∏account.balance – E1 – E2
(σaccount.balance < d.balance (account x ρd (account)))
– E1 x E2

Additional Operations Set-Intersection Operation

We define additional operations that • Notation: r ∩ s


do not add any power to the
relational algebra, but that simplify • Defined as:
common queries. • r ∩ s = { t | t ∈ r and t ∈ s }
• Set intersection • Assume:
• Natural join – r, s have the same arity
• Division – attributes of r and s are compatible
• Assignment
• Note: r ∩ s = r – (r – s)

6
Set-Intersection Operation – Example Natural-Join Operation
„ Notation: r s

• Relation r,As: B A B • Let r and s be relations on schemas R and S respectively.


α 1 α 2
α 2 β 3 Then, r s is a relation on schema R ∪ S obtained as
β 1

r s
follows:

– Consider each pair of tuples tr from r and ts from s.


A B
– If tr and ts have the same value on each of the attributes in
• r∩s α 2

R ∩ S, add a tuple t to the result, where

• t has the same value as tr on r

• t has the same value as ts on s

Example Natural Join Operation – Example

• Relations r, s:
A B C D B D E
R = (A, B, C, D)
α 1 α a 1 a α
β 2 γ a 3 a β
S = (E, B, D) γ 4 β b 1 a γ
α 1 γ a 2 b δ
Result schema = (A, B, C, D, E) δ 2 β b 3 b ∈
r s
r s is defined as: „ r s
A B C D E
∏r.A, r.B, r.C, r.D, s.E (σr.B = s.B ∧ r.D = s.D (r x s)) α 1 α a α
α 1 α a γ
α 1 γ a α
α 1 γ a γ
δ 2 β b δ

Division Operation Division Operation – Example


• Notation: r ÷ s „ Relations r, s:
A B
• Suited to queries that include the phrase “for all”. B
α 1 1
• Let r and s be relations on schemas R and S respectively α 2
where α 3 2
β 1 s
– R = (A1, …, Am , B1, …, Bn ) γ 1
δ 1
– S = (B1, …, Bn) δ 3
δ 4
The result of r ÷ s is a relation on schema ∈ 6
∈ 1
R – S = (A1, …, Am) β 2
„ r ÷ s:
r ÷ s = { t | t ∈ ∏ R-S (r) ∧ ∀ u ∈ s ( tu ∈ r ) } A r

Where tu means the concatenation of tuples t and u to α

produce a single tuple β

7
Another Division Example Division Operation (Cont.)
„ Relations r, s: • Property
A B C D E D E – Let q = r ÷ s
α a α a 1 a 1 – Then q is the largest relation satisfying q x s ⊆ r
α a γ a 1 b 1
• Definition in terms of the basic algebra operation
α a γ b 1 s
β a γ a 1 Let r(R) and s(S) be relations, and let S ⊆ R
β a γ b 3
γ a γ a 1 r ÷ s = ∏R-S (r ) – ∏R-S ( ( ∏R-S (r ) x s ) – ∏R-S,S(r ))
γ a γ b 1
γ a β b 1
r To see why
„ r ÷ s:
– ∏R-S,S (r) simply reorders attributes of r
A B C

α a γ
– ∏R-S (∏R-S (r ) x s ) – ∏R-S,S(r) ) gives those tuples t in
γ a γ
∏R-S (r ) such that for some tuple u ∈ s, tu ∉ r.

Assignment Operation Bank Example Queries


• The assignment operation (←) provides a convenient way to
express complex queries. „ Find the names of all customers who have a loan
– Write query as a sequential program consisting of and an account at bank.
• a series of assignments ∏customer_name (borrower) ∩ ∏customer_name (depositor)
• followed by an expression whose value is displayed as a
result of the query.
– Assignment must always be made to a temporary relation
variable. • Find the name of all customers who have a
• Example: Write r ÷ s as loan at the bank and the loan amount
temp1 ← ∏R-S (r )
temp2 ← ∏R-S ((temp1 x s ) – ∏R-S,S (r ))
result = temp1 – temp2 ∏ customer-name, loan-number, am ount (borrower loan )
– The result to the right of the ← is assigned to the relation
variable on the left of the ←.
– May use variable in subsequent expressions.

Bank Example Queries Example Queries

• Find all customers who have an account from at • Find all customers who have an account at all
least the “Downtown” and the Uptown” branches. branches located in Brooklyn city.
z Query 1

∏customer_name (σbranch_name = “Downtown” (depositor account )) ∩

∏customer_name (σbranch_name = “Uptown” (depositor account))


∏customer_name, branch_name (depositor account)
÷ ∏branch_name (σbranch_city = “Brooklyn” (branch))

z Query 2
∏customer_name, branch_name (depositor account)
÷ ρtemp(branch_name) ({(“Downtown” ), (“Uptown” )})

Note that Query 2 uses a constant relation.

8
Extended Relational-Algebra-Operations Generalized Projection
• Extends the projection operation by allowing arithmetic
• Generalized Projection
functions to be used in the projection list.
• Aggregate Functions
• Outer Join ∏F ,F ,...,F (E)
1 2 n

E is any relational-algebra expression


• Each of F1, F2, …, Fn are are arithmetic expressions involving
constants and attributes in the schema of E.
• Given relation credit_info(customer_name, limit,
credit_balance), find how much more each person can spend:
∏customer_name, limit – credit_balance (credit_info)

Aggregate Functions and Operations Aggregate Operation – Example


• Aggregation function takes a collection of values and returns
a single value as a result. • Relation r:
A B C
avg: average value
min: minimum value α α 7
max: maximum value α β 7
sum: sum of values β β 3
count: number of values β β 10
• Aggregate operation in relational algebra
G1 ,G2 ,K,Gn
ϑF ( A ),F ( A ,K,F ( A ) (E )
1 1 2 2 n n

„ g sum(c) (r) sum(c )


E is any relational-algebra expression 27
– G1, G2 …, Gn is a list of attributes on which to group (can be
empty)
– Each Fi is an aggregate function
– Each Ai is an attribute name

Aggregate Operation – Example Aggregate Functions (Cont.)

• Relation account grouped by branch-name: • Result of aggregation does not have a name
branch_name account_number balance
– Can use rename operation to give it a name
Perryridge A-102 400
Perryridge A-201 900
– For convenience, we permit renaming as part of
Brighton A-217 750
Brighton A-215 750
aggregate operation
Redwood A-222 700

•branch_name g (account) g
sum(balance)
branch_name sum(balance) as sum_balance (account)

branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700

9
Outer Join Outer Join – Example

• An extension of the join operation that avoids loss of • Relation loan

information. loan_number branch_name amount


L-170 Downtown 3000
• Computes the join and then adds tuples form one L-230 Redwood 4000
L-260 Perryridge 1700
relation that does not match tuples in the other relation
„ Relation borrower
to the result of the join.
customer_name loan_number
• Uses null values: Jones L-170
Smith L-230
Hayes L-155
– null signifies that the value is unknown or does not
exist

Outer Join – Example Outer Join – Example

• Inner Join „ Right Outer Join


loan borrower
loan Borrower
loan_number branch_name amount customer_name
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-170 Downtown 3000 Jones L-230 Redwood 4000 Smith
L-230 Redwood 4000 Smith L-155 null null Hayes

„ Left Outer Join „ Full Outer Join


loan Borrower loan borrower
loan_number branch_name amount customer_name loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null L-260 Perryridge 1700 null
L-155 null null Hayes

Null Values Null Values


• Comparisons with null values return the special truth value:
• It is possible for tuples to have a null value, denoted unknown
by null, for some of their attributes – If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
• null signifies an unknown value or that a value does • Three-valued logic using the truth value unknown:
not exist. – OR: (unknown or true) = true,
(unknown or false) = unknown
• The result of any arithmetic expression involving null
(unknown or unknown) = unknown
is null. – AND: (true and unknown) = unknown,
• Aggregate functions simply ignore null values (as in (false and unknown) = false,
(unknown and unknown) = unknown
SQL)
– NOT: (not unknown) = unknown
• For duplicate elimination and grouping, null is treated – In SQL “P is unknown” evaluates to true if predicate P
like any other value, and two nulls are assumed to evaluates to unknown
• Result of select predicate is treated as false if it evaluates to
be the same (as in SQL) unknown

10
Modification of the Database Deletion

• The content of the database may be • A delete request is expressed similarly to a query,
modified using the following operations:
except instead of displaying tuples to the user, the
– Deletion
– Insertion selected tuples are removed from the database.
– Updating • Can delete only whole tuples; cannot delete values on
• All these operations are expressed using only particular attributes
the assignment operator.
• A deletion is expressed in relational algebra by:
r←r–E
where r is a relation and E is a relational algebra
query.

Deletion Examples Insertion

• Delete all account records in the Perryridge branch. • To insert data into a relation, we either:
account ← account – σ branch_name = “Perryridge” (account ) – specify a tuple to be inserted
„ Delete all loan records with amount in the range of 0 to 50 – write a query whose result is a set of tuples to be
loan ← loan – σ amount ≥ 0 and amount ≤ 50 (loan)
inserted
„ Delete all accounts at branches located in Needham. • in relational algebra, an insertion is expressed by:
r1 ← σ branch_city = “Needham” (account branch )
r← r ∪ E
r2 ← ∏branch_name, account_number, balance (r1)
r3 ← ∏ customer_name, account_number (r2 depositor) where r is a relation and E is a relational algebra
account ← account – r2 expression.
depositor ← depositor – r3
• The insertion of a single tuple is expressed by letting
E be a constant relation containing one tuple.

Insertion Examples Updating

• Insert information in the database specifying • A mechanism to change a value in a tuple without
that Smith has $1200 in account A-973 at charging all values in the tuple
the Perryridge branch. • Use the generalized projection operator to do this task
account ← account ∪ {(“Perryridge”, A-973, 1200)}
depositor ← depositor ∪ {(“Smith”, A-973)}
r ← ∏ F ,F ,K,F , ( r )
„ Provide as a gift for all loan customers in the 1 2 l

Perryridge branch, a $200 savings account. Let • Each Fi is either


the loan number serve as the account number for – the I th attribute of r, if the I th attribute is not
the new savings account. updated, or,
r1 ← (σbranch_name = “Perryridge” (borrower loan)) – if the attribute is to be updated Fi is an expression,
account ← account ∪ ∏branch_name, loan_number,200 (r1) involving only constants and the attributes of r,
depositor ← depositor ∪ ∏customer_name, loan_number (r1)
which gives the new value for the attribute

11
Update Examples

• Make interest payments by increasing all


balances by 5 percent.
account ← ∏ account_number, branch_name, balance * 1.05 (account)

„ Pay all accounts with balances over $10,000 6


percent interest and pay all others 5 percent

account ← ∏ account_number, branch_name, balance * 1.06 (σ BAL > 10000 (account


)) ∪ ∏ account_number, branch_name, balance * 1.05 (σBAL ≤ 10000 (account))

12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy