©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure...

91
©Silberschatz, Korth and Sudarsha 2.1 Database System Concepts Chapter 2: Relational Model Chapter 2: Relational Model Structure of Relational Databases Relational Algebra

Transcript of ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure...

Page 1: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.1Database System Concepts

Chapter 2: Relational ModelChapter 2: Relational Model

Structure of Relational Databases

Relational Algebra

Page 2: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.2Database System Concepts

Basic StructureBasic 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 tuples (a1, a2, …, an) where each ai Di

Example:

cust-name = {Jones, Smith, Curry, Lindsay}cust-street = {Main, North, Park}cust-city = {Harrison, Rye, Pittsfield}

r = {(Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)}

r is a relation over cust-name x cust-street x cust-city

Page 3: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.3Database System Concepts

Relations are UnorderedRelations are Unordered

In this (mathematical) context, since a relation is a set, the order of tuples is irrelevant and may be thought of as arbitrary.

In a real DBMS, tuple order is typically very important and not arbitrary.

Page 4: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.4Database System Concepts

The Table:

The Relation:{ (A-101,Downtown,500),

(A-102,Perryridge,400), (A-201,Brighton,900),

:

(A-305,Round Hill,350) }

Table vs. RelationTable vs. Relation

Page 5: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.5Database System Concepts

Attribute TypesAttribute Types

Each attribute of a relation has a name.

The set of allowed values for each attribute is called the domain of the attribute.

Attribute values are required to be atomic, that is, indivisible (note how this differs from ER modeling). Multi-valued attribute values are not atomic.

Composite attribute values are not atomic.

Page 6: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.6Database System Concepts

The Evil Value “Null”The Evil Value “Null”

The special value null is an implicit member of every domain.

Tuples can have a null value for some of their attributes.

A null value can be interpreted in several ways: Value is unknown Value does not exist Value is known and exists, but just hasn’t been entered

yet

The null value causes complications in the definition of many operations.

We shall consider their effect later.

Page 7: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.7Database System Concepts

Relation SchemaRelation Schema

Let A1, A2, …, An be attributes. Then R = (A1, A2, …, An ) is a relation schema.

Customer-schema = (customer-name, customer-street, customer-city)

Sometimes referred to as a relational schema or relational scheme.

Let R be a relation schema. Then r(R) is a relation variable.

customer (Customer-schema)

Page 8: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.8Database System Concepts

DatabaseDatabase

A database consists of multiple relations.

account - account information depositor - depositor information, i.e., who deposits into which accounts customer - customer information

Storing all information as a single relation is possible, as in:

bank(account-number, balance, customer-name, ..)

This 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 (Chapter 7) deals with how to design relational schemas.

Page 9: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.9Database System Concepts

E-R DiagramE-R Diagramfor the Banking Enterprisefor the Banking Enterprise

The (modified) E-R diagram from the banking enterprise:

Page 10: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.10Database System Concepts

Relational SchemesRelational Schemesfor the Banking Enterprisefor the Banking Enterprise

The following relational schemes result:

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

customer (customer-name, customer-street, customer-city)

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

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

depositor (customer-name, account-number)

borrower (customer-name, loan-number)

Page 11: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.11Database System Concepts

Schema DiagramSchema Diagramfor the Banking Enterprisefor the Banking Enterprise

Page 12: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.12Database System Concepts

Query LanguagesQuery Languages

Language in which user requests information from the database.

Categories of languages procedural

non-procedural

“Pure” languages: Relational Algebra (primarily procedural)

Tuple Relational Calculus (non-procedural)

Domain Relational Calculus (non-procedural)

Pure languages form underlying basis of “real” query languages.

Page 13: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.13Database System Concepts

Relational AlgebraRelational Algebra

Procedural language (primarily)

Six basic operators: select

project

union

set difference

cartesian product

rename

Page 14: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.14Database System Concepts

Relational AlgebraRelational Algebra

Each operator takes one or more relations as input and gives a new relation as a result.

Each operation defines: Requirements or constraints on its’ parameters.

Attributes in the resulting relation, including their types and names.

Which tuples will be included in the result.

Page 15: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.15Database System Concepts

Select Operation – ExampleSelect Operation – Example

Relation r A B C D

1

5

12

23

7

7

3

10

A=B ^ D > 5 (r)

A B C D

1

23

7

10

Page 16: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.16Database System Concepts

Select OperationSelect Operation

Notation:

p(r)

where p is a selection predicate and r is a relation (or more generally, a relational algebra expression).

Defined as:

p(r) = {t | t r and p(t)}

where p is a formula in propositional logic consisting of terms connected by: (and), (or), (not), and where each term can involve the comparison operators: =, , >, , <,

Page 17: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.17Database System Concepts

Select Operation, Cont.Select Operation, Cont.

Example of selection: branch-name=“Perryridge”(account)

Logically, one can think of selection as performing a table scan, but technically this may or may not be the case, i.e., an index may be used.

Page 18: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.18Database System Concepts

Project Operation – ExampleProject Operation – Example

Relation r:A B C

10

20

30

40

1

1

1

2

A C

1

1

1

2

=

A C

1

1

2

A,C (r)

Page 19: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.19Database System Concepts

Project OperationProject Operation

Notation:A1, A2, …, Ak (r)

where A1, A2 are attribute names and r is a relation.

The result is defined as the relation of k columns obtained by erasing the columns that are not listed.

Duplicate rows are removed from result, since relations are sets.

Example: To eliminate the branch-name attribute of account: account-number, balance (account)

Note, however, that account is not actually modified.

Page 20: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.20Database System Concepts

Union Operation – ExampleUnion Operation – Example

Relations r, s:

r s

A B

1

2

1

A B

2

3

rs

A B

1

2

1

3

Page 21: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.21Database System Concepts

Union OperationUnion Operation

Notation: r s

Defined as:

r s = {t | t r or t s}

For r s to be valid: r, s must have the same arity (same number of

attributes) The attribute domains must be compatible (e.g., 2nd

attribute of r deals with “the same type of values” as does the 2nd attribute of s)

Example: find all customers with either an account or a loan

customer-name (depositor) customer-name (borrower)

Page 22: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.22Database System Concepts

Set Difference OperationSet Difference Operation

Relations r, s:

r – s

A B

1

2

1

A B

2

3

rs

A B

1

1

Page 23: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.23Database System Concepts

Set Difference Operation, Cont.Set Difference Operation, Cont.

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

Note that there is no requirement that the attribute names be the same. So what about attributes names in the result?

Similarly for union.

Page 24: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.24Database System Concepts

Cartesian-Product OperationCartesian-Product Operation

Relations r, s:

r x s:

A B

1

2

A B

11112222

C D

1010201010102010

E

aabbaabb

C D

10102010

E

aabbr

s

Page 25: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.25Database System Concepts

Cartesian-Product Operation, Cont.Cartesian-Product Operation, Cont.

Notation r x s

Defined as:

r x s = {tq | t r and q s}

The attributes of r and s are assumed to be disjoint, i.e., that R S = .

If the attributes of r and s are not disjoint, then renaming must be used. Each attributes name has its originating relations name as

a prefix. If r and s are the same relation, then the rename

operation can be used.

Page 26: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.26Database System Concepts

Composition of OperationsComposition of Operations

Expressions can be built using multiple operations

r x s A=C(r x s)

A B

11112222

C D

1010201010102010

E

aabbaabb

A B C D E

122

102020

aab

Page 27: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.27Database System Concepts

Rename OperationRename Operation

The rename operator allows the results of an expression to be renamed.

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.

Page 28: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.28Database System Concepts

Formal DefinitionFormal Definition

A basic expression in relational algebra consists of one of the following: A relation in the database

A constant relation

Let E1 and E2 be relational-algebra expressions. Then the following are all also relational-algebra expressions: E1 E2

E1 - E2

E1 x E2

p (E1), P is a predicate on attributes in E1

s(E1), S is a list consisting of attributes in E1

x (E1), x is the new name for the result of E1

Page 29: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.29Database System Concepts

Banking ExampleBanking Example

Recall the relational schemes from the banking enterprise:

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

customer (customer-name, customer-street, customer-city)

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

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

depositor (customer-name, account-number)

borrower (customer-name, loan-number)

Page 30: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.30Database System Concepts

Example QueriesExample Queries

Find all loans of over $1200 (a bit ambiguous).

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

amount > 1200 (loan)

loan-number (amount > 1200 (loan))

Page 31: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.31Database System Concepts

Example QueriesExample Queries

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

customer-name (borrower) customer-name (depositor)

customer-name (borrower) customer-name (depositor)

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

Page 32: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.32Database System Concepts

Example QueriesExample Queries

Find the names of all customers who have a loan at the Perryridge branch.

customer-name (branch-name=“Perryridge”

(borrower.loan-number = loan.loan-number(borrower x loan)))

Notes: The two selections could have been combined into one. The selection on branch-name could have been applied to loan

first, as shown on the next slide…

Page 33: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.33Database System Concepts

Example QueriesExample Queries

Alternative - Find the names of all customers who have a loan at the Perryridge branch.

customer-name(loan.loan-number = borrower.loan-number( borrower x branch-name = “Perryridge”(loan)))

Notes: What are the implications of doing the selection first? How does a non-Perryridge borrower get eliminated? Couldn’t the amount and the branch-name attributes be

eliminated from loan after the selection? What would be the implications?

Page 34: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.34Database System Concepts

Example QueriesExample Queries

Find the names of all customers who have a loan at the Perryridge branch but no account at any branch of the bank.

customer-name (branch-name = “Perryridge”

(borrower.loan-number = loan.loan-number (borrower x loan))) –

customer-name(depositor)

Page 35: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.35Database System Concepts

Example QueriesExample Queries

Find the largest account balance. Requires a cartesian product between account and itself,

resulting in ambiguity of attribute names.

Resolved by renaming one instance of the account relation as d.

balance(account) – account.balance(account.balance < d.balance (account x d (account)))

Page 36: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.36Database System Concepts

Additional OperationsAdditional Operations

The following operations do not add any power to relational algebra, but simplify common queries.

Set intersection

Natural join

Theta join

Division

Assignment

All of the above can be defined in terms of the six basic operators.

Page 37: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.37Database System Concepts

Set-Intersection OperationSet-Intersection Operation

Notation: r s

Defined as:

r s = { t | t r and t s }

Assume: r, s have the same arity

attributes of r and s are compatible

Note: r s = r - (r - s)

Page 38: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.38Database System Concepts

Set-Intersection Operation, Cont.Set-Intersection Operation, Cont.

Relation r, s:

r s

A B

121

A B

23

r s

A B

2

Page 39: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.39Database System Concepts

Natural-Join OperationNatural-Join Operation

Notation: r s

Let r and s be relations on schemas R and S respectively.

r s is a relation that: Has all attributes in R S

For each pair of tuples tr and ts from r and s, respectively, if tr and ts have the same value on all attributes in R S, add a tuple t to the result, where:

• t has the same value as tr on attributes in R

• t has the same value as ts on attributes in S

Page 40: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.40Database System Concepts

Natural-Join ExampleNatural-Join Example

Relational schemes for relations r and s, respectively:

R = (A, B, C, D)

S = (E, B, D)

Resulting schema for r s :

(A, B, C, D, E)

r s is defined as:

r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))

Page 41: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.41Database System Concepts

Natural Join ExampleNatural Join Example

Relations r, s:

Contents of r s:

A B

12412

C D

aabab

B

13123

D

aaabb

E

r

A B

11112

C D

aaaab

E

s

Page 42: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.42Database System Concepts

Natural Join – Another ExampleNatural Join – Another Example

Find the names of all customers who have a loan at the Perryridge branch.

Original Expression:

Using the Natural Join Operator:

Specifying the join explicitly makes it look nicer, plus it helps the query optimizer.

customer-name (branch-name=“Perryridge”

(borrower.loan-number = loan.loan-number(borrower x loan)))

customer-name(branch-name = “Perryridge”(borrower loan))

Page 43: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.43Database System Concepts

Theta-Join OperationTheta-Join Operation

Notation: r θ s

Let r and s be relations on schemas R and S respectively.

Then, r θ s is a relation that:

Has all attributes in R S including duplicates.

For each pair of tuples tr and ts from r and s, respectively, if θ evaluates to true for tr and ts, then add a tuple t to the result, where:

• t has the same value as tr on attributes in R

• t has the same value as ts on attributes in S

r θ s is defined as: θ (r x s)

Page 44: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.44Database System Concepts

Theta-Join ExampleTheta-Join Example

Example:

R = (A, B, C, D)

S = (E, B, D)

Resulting schema:

(r.A, r.B, r.C, r.D, s.E, s.B, s.D)

Page 45: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.45Database System Concepts

Theta Join – Another ExampleTheta Join – Another Example

Consider the following relational schemes:

Exam = (Exam#, Average)

Score = (SS#, Exam#, Grade)

Find the SS#s for those students who scored less than average on some exam. Along with the SS# list the exam#, exam average, and the students’ grade.

Score.SS#, Exam.Exam#, Exam.Average, Score.Grade (

Exam Exam.Exam# = Score.Exam# Exam.Average > Score.Grade Score)

Page 46: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.46Database System Concepts

Division OperationDivision Operation

Notation:

Suited to queries that include the phrase “for all”

Let r and s be relations on schemas R and S respectively where S R, and let the attributes of R and S be:

R = (A1, …, Am, B1, …, Bn)

S = (B1, …, Bn)

The result of r s is a relation on schemaR – S = (A1, …, Am)

where:

r s = { t | t R-S(r) u s ( tu r ) }

r s

Page 47: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.47Database System Concepts

Division – Example #1Division – Example #1

Relations r, s: r s:

AB

1

2

A B

12311134612

r

s

Page 48: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.48Database System Concepts

Division – Example #2Division – Example #2

A B

aaaaaaaa

C D

aabababb

E

11113111

Relations r, s: r s:

D

ab

E

11 A B

aa

C

r

s

A B C

Page 49: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.49Database System Concepts

Division – Example #3Division – Example #3

A B

aaaaaaaa

C D

aabababb

E

11113111

Relations r, s: r s:

B

aa

D

ab

A C

E

11

r

s

Page 50: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.50Database System Concepts

Division Operation (Cont.)Division Operation (Cont.)

Let r(R) and s(S) be relations, and let S R. Then:

r s = R-S (r) – R-S ( (R-S (r) x s) – R-S,S(r))

To see why: R-S,S(r) simply reorders attributes of r

R-S(R-S (r) x s) – R-S,S(r)) gives those tuples t in R-S (r) such that for some tuple u s, tu r.

Property: Let q = r s Then q is the largest relation satisfying q x s r

Page 51: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.51Database System Concepts

Query 2

customer-name, branch-name (depositor

account)

temp(branch-name) ({(“Downtown”),

(“Uptown”)})

Example QueriesExample Queries

Find all customers who have an account from both the “Downtown” and the “Uptown” branches.

where CN denotes customer-name and BN denotes branch-name.

Query 1

CN(BN=“Downtown”(depositor account))

CN(BN=“Uptown”(depositor

account))

Page 52: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.52Database System Concepts

Find all customers who have an account at all branches located in the city of Brooklyn.

Example QueriesExample Queries

customer-name, branch-name (depositor account)

branch-name (branch-city = “Brooklyn” (branch))

Page 53: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.53Database System Concepts

Assignment OperationAssignment Operation

The assignment operation () provides a convenient way to express complex queries.

A query is written as a sequence of assignments.

Assignment is always made to a temporary relation variable.

Example: Write r s as

temp1 R-S (r)

temp2 R-S ((temp1 x s) – R-S,S (r))

result temp1 – temp2

Exercises => 2.1, 2.5 (5th edition)

=> 3.5 (3rd edition)

Page 54: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.54Database System Concepts

Extended RelationalExtended RelationalAlgebra OperationsAlgebra Operations

Generalized Projection

Outer Join

Aggregate Functions

Page 55: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.55Database System Concepts

Generalized ProjectionGeneralized Projection

Extends projection by allowing arithmetic functions to be used in the projection list.

F1, F2, …, Fn(E)

E is any relational-algebra expression

Each of F1, F2, …, Fn 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: Also determine what percentage of their credit line that they have already used.

customer-name, limit – credit-balance, (credit-balance/limit)*100 (credit-info)

Page 56: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.56Database System Concepts

Aggregate FunctionsAggregate Functions

An aggregation function takes a collection of values and returns a single value as a result.

avg - average valuemin - minimum valuemax - maximum valuesum - sum of valuescount - number of values

Other aggregate functions are provided by most DBMS vendors.

Page 57: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.57Database System Concepts

The Aggregate OperatorThe Aggregate Operator

Aggregation functions are specified in relational algebra using the aggregate operator:

G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)

E is any relational-algebra expression.

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.

Page 58: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.58Database System Concepts

Aggregate Operation – ExampleAggregate Operation – Example

Relation r: g sum(c) (r)

Could also add min, max, and other aggregates to the above expression.

g sum(c), max(c), min(c) (r)

A B

C

7

7

3

10

min-C

3

sum-C

27

sum-C

27

max-C

10

Page 59: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.59Database System Concepts

Aggregate Operation – ExampleAggregate Operation – Example

Grouping is somewhat like sorting, although not identical.

Relation account grouped by branch-name:

A list of branch names and the sum of all their account balances.

branch-name g sum(balance) (account)

branch-name account-number balancePerryridgePerryridgeBrightonBrightonRedwood

A-102A-201A-217A-215A-222

400900750750700

branch-name balance

PerryridgeBrightonRedwood

13001500700

Page 60: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.60Database System Concepts

Aggregate Functions (Cont.)Aggregate Functions (Cont.)

Consider the following relational scheme:History = (Student-Name, Department, Course-Number, Grade)

Give a list of student names and, for each name, list the average course grade for each department in which the student has taken classes.

Smith CSE 87Smith MTH 93Jones CHM 88Jones CSE 75Brown PSY 97

:

student-name, department g avg(grade) (History)

Adding count(Course-Number) would tell how many courses the student had in each department. Similarly min and max could be added.

student-name, department g avg(grade), count(Course-Number), min(Grade), max(Grade)(History)

Page 61: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.61Database System Concepts

Aggregate Functions (Cont.)Aggregate Functions (Cont.)

Neither the resulting relation nor the aggregated attributes have names.

The rename operation can be used to give both names.

Aggregated attributes can be renamed in the aggregate operator:

branch-name g sum(balance) as sum-balance (account)

Page 62: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.62Database System Concepts

Aggregate FunctionsAggregate Functionsand Null Valuesand Null Values

Null values are controversial.

Various proposals exist in the research literature on whether null values should be allowed and, if so, how they should affect operations.

See www.dbazine.com/ofinterest/oi-articles/pascal27 for example.

Null values can frequently be eliminated through normalization and decomposition.

Page 63: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.63Database System Concepts

Aggregate FunctionsAggregate Functionsand Null Valuesand Null Values

How nulls are treated by relational operators: For duplicate elimination and grouping, null is treated

like any other value, i.e., two nulls are assumed to be the same.

Aggregate functions (except for count) simply ignore null values.

The above rules are arbitrary, but consistent with SQL.

Note how the second rule can be misleading: Is avg(grade) actually a class average?

Page 64: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.64Database System Concepts

Null Values andNull Values andExpression EvaluationExpression Evaluation

Null values also affect how selection predicates are evaluated: The result of any arithmetic expression involving null is null.

Comparisons with null returns the special truth value unknown.

Value of a predicate is treated as false if it evaluates to unknown.

balance*100 > 500 (account)

For more complex predicates, the following three-valued logic is used: OR: (unknown or true) = true

(unknown or false) = unknown (unknown or unknown) = unknown

AND: (true and unknown) = unknown (false and unknown) = false (unknown and unknown) = unknown

NOT: (not unknown) = unknown

(balance*100 > 500) and (branch-name = “Perryridge”)(account)

Page 65: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.65Database System Concepts

Null ValuesNull Valuesand Expression Evaluation, Cont.and Expression Evaluation, Cont.

Why doesn’t a comparison with null simply result in false? If false was used instead of unknown, then:

not (A < 5) would not be equivalent to:

A >= 5

Why would this be a problem?

How does a comparison with null resulting in unknown help?

Page 66: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.66Database System Concepts

Outer JoinOuter Join

An extension of the join operation that avoids loss of information.

Computes the join and then adds tuples from one relation that do not match tuples in the other relation.

Typically introduces null values.

Page 67: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.67Database System Concepts

Outer Join – ExampleOuter Join – Example

Relation loan:

Relation borrower:

customer-nameloan-number

JonesSmithHayes

L-170L-230L-155

300040001700

loan-number amount

L-170L-230L-260

branch-name

DowntownRedwoodPerryridge

Page 68: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.68Database System Concepts

Outer Join – ExampleOuter Join – Example

Inner Join

loan Borrower

loan-number amount

L-170L-230

30004000

customer-name

JonesSmith

branch-name

DowntownRedwood

Left Outer Join

loan Borrower

JonesSmithnull

loan-number amount

L-170L-230L-260

300040001700

customer-namebranch-name

DowntownRedwoodPerryridge

Page 69: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.69Database System Concepts

Outer Join – ExampleOuter Join – Example

Right Outer Join

loan borrower

loan borrower

Full Outer Join

loan-number amount

L-170L-230L-155

30004000null

customer-name

JonesSmithHayes

branch-name

DowntownRedwoodnull

loan-number amount

L-170L-230L-260L-155

300040001700null

customer-name

JonesSmithnullHayes

branch-name

DowntownRedwoodPerryridgenull

Page 70: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.70Database System Concepts

Example Left-Outer JoinExample Left-Outer Join

Consider the following relational schemes:

Student = (SS#, Address, Date-of-Birth)

Grade-Point-Average = (SS#, GPA)

Consider the following query:

“Create a list of all student SS#’s and their GPAs. Be sure to include all students, including first semester freshman, who do not have a GPA.”

Solution:

Student Grade-Point-Average

Page 71: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.71Database System Concepts

Modification of the DatabaseModification of the Database

The database contents can be modified with operations:

Deletion

Insertion

Updating

These operations can all be expressed using the assignment operator. Some can be expressed other ways too.

Page 72: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.72Database System Concepts

DeletionDeletion

A deletion is expressed in relational algebra by:

r r – E

where r is a relation and E is a relational algebra query.

The deletion of a single tuple is expressed by letting E be a constant relation containing one tuple.

Only whole tuples can be deleted, not specific attribute values.

Page 73: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.73Database System Concepts

r1 branch-city = “Needham” (account branch)

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

r3 customer-name, account-number (depositor r2)

account account – r2

depositor depositor – r3

Deletion ExamplesDeletion Examples

Delete all account records with a branch name equal to Perryridge.

Delete all accounts at branches located in Needham.

Delete all loan records with amount in the range of 0 to 50

loan loan – amount 0and amount 50 (loan)

account account – branch-name = “Perryridge” (account)

Page 74: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.74Database System Concepts

Alternative VersionsAlternative Versions

Delete all accounts at branches located in Needham.

Delete all accounts at branches located in Needham.

Which version is preferable?

r1 branch-name (branch-city = “Needham” (branch))

r2 account-number (account-number, branch-name(account) r1)

account account – (account r2)

depositor depositor – (depositor r2)

r1 (branch-city <> “Needham” (depositor account branch))

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

depositor customer-name, account-number(r1)

Page 75: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.75Database System Concepts

InsertionInsertion

In relational algebra, an insertion is expressed by:

r r E

where r is a relation and E is a relational algebra expression.

The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.

Page 76: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.76Database System Concepts

r1 (branch-name = “Perryridge” (borrower loan))

account account loan-number, branch-name,200 (r1)

depositor depositor customer-name, loan-number(r1)

Insertion ExamplesInsertion Examples

Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch.

Provide as a gift for all loan customers at the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account.

Is r1 overly complex?

account account {(A-973, “Perryridge”, 1200)}

depositor depositor {(“Smith”, A-973)}

Page 77: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.77Database System Concepts

UpdatingUpdating

Generalized projection can be used to change a value in a tuple without changing all values in the tuple.

r F1, F2, …, FI, (r)

Each Fi is either:

The ith attribute of r, if the ith attribute is not updated, or,

An expression, involving only constants and attributes of r, which gives a new value for an attribute, when that attribute is to be updated.

Page 78: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.78Database System Concepts

Update ExamplesUpdate Examples

Make interest payments by increasing all balances by 5 percent.

Pay 6 percent interest to all accounts with balances over $10,000 and pay 5 percent interest to all others.

account AN, BN, BAL * 1.06 ( BAL 10000 (account)) AN, BN, BAL * 1.05 (BAL 10000

(account))

account AN, BN, BAL * 1.05 (account)

where AN, BN and BAL stand for account-number, branch-name and balance, respectively.

Page 79: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.79Database System Concepts

ViewsViews

Views are very important, but we will not consider them until chapter 3.

Page 80: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.80Database System Concepts

The The employeeemployee and and ft-works ft-works Relations Relations

Page 81: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.81Database System Concepts

The Result of The Result of employee ft-worksemployee ft-works

Page 82: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.82Database System Concepts

The Result of The Result of employeeemployee ft-worksft-works

Page 83: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.83Database System Concepts

Result of Result of employee ft-works employee ft-works

Page 84: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.84Database System Concepts

Result of Result of employee ft-worksemployee ft-works

Page 85: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.85Database System Concepts

Tuples Inserted Into Tuples Inserted Into loan loan and and borrowerborrower

Page 86: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.86Database System Concepts

Names of All Customers Who Have a Loan Names of All Customers Who Have a Loan at the Perryridge Branchat the Perryridge Branch

Page 87: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.87Database System Concepts

E-R DiagramE-R Diagram

Page 88: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.88Database System Concepts

The The branchbranch Relation Relation

Page 89: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.89Database System Concepts

The The loan loan RelationRelation

Page 90: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.90Database System Concepts

The The borrowerborrower Relation Relation

Page 91: ©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.

©Silberschatz, Korth and Sudarshan2.91Database System Concepts

Determining Keys from E-R SetsDetermining Keys from E-R Sets

Strong entity set. The primary key of the entity set becomes the primary key of the relation.

Weak entity set. The primary key of the relation consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set.

Relationship set. The union of the primary keys of the related entity sets becomes a super key of the relation.

For binary many-to-one relationship sets, the primary key of the “many” entity set becomes the relation’s primary key.

For one-to-one relationship sets, the relation’s primary key can be that of either entity set.

For many-to-many relationship sets, the union of the primary keys becomes the relation’s primary key