Top Banner
Chapter 3: Relational Chapter 3: Relational Model Model Structure of Relational Databases Structure of Relational Databases Normal forms (chap. 7) Normal forms (chap. 7) Reduction of an E-R Schema to Reduction of an E-R Schema to Relational (Sect. 2.9) Relational (Sect. 2.9) Relational Algebra Relational Algebra Tuple Relational Calculus Tuple Relational Calculus Domain Relational Calculus Domain Relational Calculus Extended Relational-Algebra- Extended Relational-Algebra- Operations Operations Modification of the Database Modification of the Database Views Views
62

Chapter 3: Relational Model Structure of Relational Databases Normal forms (chap. 7) Reduction of an E-R Schema to Relational (Sect. 2.9) Relational.

Apr 01, 2015

Download

Documents

Julia Lavers
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Chapter 3: Relational ModelChapter 3: Relational Model Structure of Relational DatabasesStructure of Relational Databases Normal forms (chap. 7)Normal forms (chap. 7) Reduction of an E-R Schema to Relational Reduction of an E-R Schema to Relational

(Sect. 2.9)(Sect. 2.9) Relational AlgebraRelational Algebra Tuple Relational CalculusTuple Relational Calculus Domain Relational CalculusDomain Relational Calculus Extended Relational-Algebra-OperationsExtended Relational-Algebra-Operations Modification of the DatabaseModification of the Database ViewsViews

Page 2: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Query LanguagesQuery Languages Language in which user requests Language in which user requests

information from the database.information from the database. Categories of languages: Categories of languages:

proceduralprocedural non-procedural non-procedural

““Pure” languages:Pure” languages: Relational Algebra (procedural)Relational Algebra (procedural) Tuple Relational Calculus (non-procedural)Tuple Relational Calculus (non-procedural) Domain Relational Calculus (non-Domain Relational Calculus (non-

procedural)procedural) Pure languages form underlying basis of Pure languages form underlying basis of

query languages that people use.query languages that people use.

Page 3: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Relational AlgebraRelational Algebra Procedural language consisting of:Procedural language consisting of:

Set of Set of operatorsoperators that take one or more relations that take one or more relations as inputs and give a new relation as a result.as inputs and give a new relation as a result.

Six basic operators:Six basic operators: selectselect projectproject unionunion set differenceset difference cartesian productcartesian product renamerename

And derived operators:And derived operators: Set intersection, natural join, division, assignmentSet intersection, natural join, division, assignment

Page 4: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Formal DefinitionFormal Definition A A basic expressionbasic expression in the relational algebra consists of in the relational algebra consists of

either one of the following:either one of the following: A relation in the databaseA relation in the database A constant relationA constant relation

Let Let EE11 and and EE22 be relational-algebra expressions; the be relational-algebra expressions; the following are all relational-algebra expressions:following are all relational-algebra expressions: EE11 EE22

EE11 - - EE22

EE11 x x EE22

pp ( (EE11), ), PP is a predicate on attributes in is a predicate on attributes in EE11

ss((EE11), ), SS is a list of attributes in is a list of attributes in EE11

xx ((EE11), x is the new name for the result of ), x is the new name for the result of EE11

Page 5: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

SelectSelect Selects tuples that satisfy a given predicateSelects tuples that satisfy a given predicate

Notation: Notation: pp((rr)) pp is called the is called the selection predicateselection predicate Defined as:Defined as: pp((rr) = {) = {tt | | tt rr and and p(t)p(t)}}

WhereWhere p p is a formula in propositional calculus is a formula in propositional calculus consisting of consisting of termsterms connected by : connected by : ( (andand), ), ( (oror), ), ((notnot))Each Each termterm is one of: is one of:

<attribute> <attribute> opop <attribute>|<constant> <attribute>|<constant> where where opop is one of: =, is one of: =, , >, , >, . <. . <.

Another example:Another example: branch-name=“Perryridge”branch-name=“Perryridge”((accountaccount))

Page 6: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Select – ExampleSelect – Example

Relation r A B C D

1

5

12

23

7

7

3

10

A B C D

1

23

7

10

A=B ^ D > 5 (r)

Page 7: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

ProjectProject Returns the input relation with certain attributes left outReturns the input relation with certain attributes left out

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

where where AA11, A, A22 are attribute names and are attribute names and rr is a relation is a relation

name.name. The result is defined as the relation of The result is defined as the relation of kk columns columns

obtained by erasing the columns that are not listedobtained by erasing the columns that are not listed Duplicate rowsDuplicate rows removed from result, since relations removed from result, since relations

are setsare sets Another example: To eliminate the Another example: To eliminate the branch-namebranch-name

attribute of attribute of accountaccount account-number, balanceaccount-number, balance ( (accountaccount) )

Page 8: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Project – ExampleProject – Example

RelationRelation r 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 9: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

UnionUnion Analogous to union of two setsAnalogous to union of two sets

Notation: Notation: rr ss Defined as: Defined as:

rr ss = { = {tt | | tt rr oror t t ss}} For For rr ss to be valid: to be valid:

1. 1. r,r, ss must have the must have the same same arityarity (same number (same number of attributes)of attributes)2. The attribute domains must be 2. The attribute domains must be compatiblecompatible

Another example: to find all customers with Another example: to find all customers with either an account or a loaneither an account or a loan customer-namecustomer-name ( (depositordepositor) ) customer-namecustomer-name ( (borrower)borrower)

Page 10: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Union – ExampleUnion – Example

Relations Relations r, s:r, s:

r s:

A B

1

2

1

A B

2

3

rs

A B

1

2

1

3

Page 11: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Set DifferenceSet Difference

Returns tuples that are in one relation Returns tuples that are in one relation and not in the otherand not in the other Notation Notation r – sr – s

Defined as:Defined as:

r – sr – s = { = {tt | | tt rr andand t t ss}} Set differences must be taken between Set differences must be taken between

compatiblecompatible relations. relations. rr and and ss must have the must have the same aritysame arity attribute domains of attribute domains of r r and and s s must be must be

compatiblecompatible

Page 12: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Set Difference – ExampleSet Difference – Example

Relations Relations r, s:r, s:

r – s:

A B

1

2

1

A B

2

3

rs

A B

1

1

Page 13: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Cartesian-ProductCartesian-Product

Combines information from any two relationsCombines information from any two relations Notation:Notation: r r xx s s

Defined as:Defined as:

rr x x ss = { = {t q t q || t t r r and and q q ss}} Assume that attributes of r(R) and s(S) are Assume that attributes of r(R) and s(S) are

disjoint. (That is, disjoint. (That is, RR S S = = ).). If attributes of If attributes of r(R)r(R) and and s(Ss(S) are not disjoint, ) are not disjoint,

then renaming must be used.then renaming must be used.

Page 14: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Cartesian-Product - ExampleCartesian-Product - Example

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 15: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Composition of OperationsComposition of Operations

Can build expressions using multiple Can build expressions using multiple operationsoperations

Example: Example: A=CA=C((r x sr x s))

r x sr x s

A B

11112222

C D

1010201010102010

Eaabbaabb

A B C D E

122

102020

aab

Page 16: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Rename OperationRename Operation

Allows us to name, and therefore to refer to, the Allows us to name, and therefore to refer to, the results of relational-algebra expressions.results of relational-algebra expressions.

Allows us to refer to a relation by more than one Allows us to refer to a relation by more than one name.name. Notation: Notation: xx((EE))

returns the expression returns the expression EE under the name under the name XX

If a relational-algebra expression If a relational-algebra expression EE has arity has arity nn, , then then xx ((A1, A2, …, AnA1, A2, …, An) ) ((EE)) returns the result of returns the result of

expression expression EE under the name under the name XX, and with the , and with the attributes renamed to attributes renamed to AA11, A2, …., An, A2, …., An..

Page 17: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Banking ExampleBanking Examplebranch (branch (branch-namebranch-name, branch-city, assets), branch-city, assets)

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

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

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

depositor (depositor (customer-name, account-numbercustomer-name, account-number))

borrower (borrower (customer-name, loan-numbercustomer-name, loan-number))

Page 18: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Example QueriesExample Queries

Find all loans of over $1200Find all loans of over $1200

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

amount > 1200 (loan)

loan-number (amount > 1200 (loan))

Page 19: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Example QueriesExample Queries

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

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

Page 20: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Example QueriesExample Queries

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

Query 2

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

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

Page 21: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

The The borrowerborrower Relation Relation

Page 22: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

The The loan loan RelationRelation

Page 23: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Result of Result of borrower borrower loanloan

Page 24: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Result of Result of branch-name = branch-name = “Perryridge” “Perryridge” ((borrower borrower loan) loan)

Page 25: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Example QueriesExample Queries

Find the largest account balanceFind the largest account balance Rename Rename account account relation as relation as dd The query is:The query is:

balance(account) - account.balance

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

Page 26: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Additional OperationsAdditional Operations

We define additional operations that do We define additional operations that do not add any power to the relational not add any power to the relational algebra, but that simplify common algebra, but that simplify common queries.queries.

Set intersectionSet intersection Natural joinNatural join DivisionDivision AssignmentAssignment

Page 27: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Set-Intersection OperationSet-Intersection Operation

Notation: Notation: rr ss Defined as:Defined as:

rr ss ={ ={ t t | | tt rr andand tt ss } } Assume: Assume:

rr, , ss have the have the same aritysame arity attributes of r and s are compatibleattributes of r and s are compatible

Note: Note: rr ss = = rr - ( - (rr - - ss))

Find the names of all customers who have a loan and an account at bank.customer-name (borrower) customer-name (depositor)

Page 28: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Set-Intersection Operation - Set-Intersection Operation - ExampleExample

Relation r, s:Relation r, s:

r r s s

A B

121

A B

23

rs

A B

2

Page 29: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Notation: r s

Natural-Join OperationNatural-Join Operation

Let Let rr and and ss be relations on schemas be relations on schemas RR and and SS respectively. Then, r s is respectively. Then, r s is a a relation relation on schema on schema R R SS obtained as follows:obtained as follows: Consider each pair of tuples Consider each pair of tuples ttrr from from rr and and ttss

from from ss. . If If ttrr and and ttss have the same value on each of the have the same value on each of the

attributes in attributes in RR SS, add a tuple , add a tuple tt to the result, to the result, wherewhere

• tt has the same value as has the same value as ttrr on on rr

• tt has the same value as has the same value as ttss on on ss

Page 30: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Natural Join Operation – Natural Join Operation – ExampleExample

Relations r, s:Relations r, s:A B

12412

C D

aabab

B

13123

D

aaabb

E

r

A B

11112

C D

aaaab

E

s

r s

Page 31: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Division OperationDivision Operation

Notation: Notation: r r ss Suited to queries that include the phrase Suited to queries that include the phrase “for all”“for all”.. Let Let rr and and ss be relations on schemas R and S be relations on schemas R and S

respectively whererespectively where RR = ( = (AA11, …, , …, AAmm, , BB11, …, , …, BBnn))

SS = ( = (BB11, …, , …, BBnn))

The result of r The result of r s is a relation on schema s is a relation on schema

RR – – S S = (= (AA11, …, , …, AAmm))

r r ss = { = { tt | | t t R-SR-S((rr) ) u u s s ( ( tutu r r ) } ) }

Page 32: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Division Operation – ExampleDivision Operation – Example

Relations r, s:

r s: A

B

1

2

A B

12311134612

r

s

Page 33: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Another Division ExampleAnother Division Example

A B

aaaaaaaa

C D

aabababb

E

11113111

Relations r, s:

r s:

D

ab

E

11

A B

aa

C

r

s

Page 34: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Assignment OperationAssignment Operation The The assignment operationassignment operation provides a convenient way provides a convenient way

to express complex queries. to express complex queries. Write query as a sequential program consisting of a Write query as a sequential program consisting of a

series of assignments followed by an expression whose series of assignments followed by an expression whose value is displayed as a result of the query.value is displayed as a result of the query.

Assignment must always be made to a temporary Assignment must always be made to a temporary relation variable.relation variable.

Notation: Notation: Example: Write Example: Write rr ss as as

temptemp11 R-SR-S ( (rr) )

temptemp2 2 R-SR-S (( ((temptemp1 x 1 x ss) – ) – R-S,S R-S,S (r(r))))

resultresult = = temptemp1 –1 – temp temp22

Page 35: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Example Queries (1)Example Queries (1) Find all customers who have an account from Find all customers who have an account from

the “Downtown” and the Uptown” branches.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))

Query 2

customer-name, branch-name (depositor account)

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

Page 36: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

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

Example Queries (2)Example Queries (2)

customer-name, branch-name (depositor account) branch-name (branch-city = “Brooklyn” (branch))

Page 37: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Extended Relational-AlgebraExtended Relational-AlgebraOperationsOperations

Generalized ProjectionGeneralized Projection Aggregate FunctionsAggregate Functions Outer JoinOuter Join

Page 38: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Generalized ProjectionGeneralized Projection Extends the projection operation by allowing Extends the projection operation by allowing

arithmetic functions to be used in the projection list.arithmetic functions to be used in the projection list. F1, F2, …, FnF1, F2, …, Fn((EE))

EE is any relational-algebra expression is any relational-algebra expression Each of Each of FF11, , FF22, …, , …, FFn n are are are are arithmetic expressionsarithmetic expressions

involving constants and attributes in the schema of involving constants and attributes in the schema of EE..

Example: Given relation Example: Given relation credit-info(customer-name, credit-info(customer-name, limit, credit-balance),limit, credit-balance), find how much more each find how much more each person can spend: person can spend:

customer-name, limit – credit-balancecustomer-name, limit – credit-balance (credit-info) (credit-info)

Page 39: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Aggregate Functions and Aggregate Functions and OperationsOperations

Aggregation functionAggregation function takes a collection of takes a collection of values and returns a single value as a result.values and returns a single value as a result. avg, min, max, sumavg, min, max, sum

Aggregate operationAggregate operation in relational algebra in relational algebra

G1, G2, …, GnG1, G2, …, Gn gg F1( A1), F2( A2),…, Fn( An)F1( A1), F2( A2),…, Fn( An) ( (EE)) EE is any relational-algebra expression is any relational-algebra expression GG11, , GG22 …, …, GGnn is a list of attributes on which to group is a list of attributes on which to group

(can be empty)(can be empty) Each Each FFii is an aggregate functionis an aggregate function Each Each AAii is an attribute nameis an attribute name

Page 40: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Aggregate Operation – Aggregate Operation – ExampleExample

Relation Relation rr::A B

C

7

7

3

10

g sum(c) (r)sum-C

27

Page 41: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Aggregate Operation – Another Aggregate Operation – Another exampleexample

Relation Relation accountaccount grouped by grouped by branch-namebranch-name::

branch-name g sum(balance) (account)

branch-name account-number

balance

PerryridgePerryridgeBrightonBrightonRedwood

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

400900750750700

branch-name balance

PerryridgeBrightonRedwood

13001500700

Page 42: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

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

Result of aggregation does not have a Result of aggregation does not have a namename Can use rename operation to give it a nameCan use rename operation to give it a name

For convenience, we permit renaming as For convenience, we permit renaming as part of aggregate operationpart of aggregate operation

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

Page 43: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Null ValuesNull Values

It is possible for tuples to have a null value, It is possible for tuples to have a null value, denoted by denoted by nullnull, for some of their attributes, for some of their attributes nullnull signifies an unknown value or that a value does signifies an unknown value or that a value does

not exist.not exist. The result of any The result of any arithmeticarithmetic expression involving expression involving

nullnull is is null.null. Aggregate functionsAggregate functions simply ignore null values simply ignore null values For For duplicate elimination and groupingduplicate elimination and grouping, null is , null is

treated like any other value, and two nulls are treated like any other value, and two nulls are assumed to be the sameassumed to be the same• Alternative: assume each null is different from each otherAlternative: assume each null is different from each other

Arbitrary decisions, so we simply follow SQLArbitrary decisions, so we simply follow SQL

Page 44: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Null ValuesNull Values ComparisonsComparisons with null values return the special with null values return the special

truth value truth value unknownunknown Three-valued logicThree-valued logic using the truth value using the truth value

unknownunknown:: OROR: (: (unknownunknown oror truetrue) = ) = truetrue, ,

( (unknownunknown oror falsefalse) = ) = unknownunknown ( (unknown unknown oror unknown) = unknown unknown) = unknown

ANDAND:: (true (true and and unknown) = unknown, unknown) = unknown, (false (false and and unknown) = false,unknown) = false, (unknown (unknown andand unknown) = unknown unknown) = unknown

NOTNOT: (: (notnot unknown) = unknown unknown) = unknown Result of selectResult of select predicate is treated as predicate is treated as false false if it if it

evaluates to evaluates to unknownunknown

Page 45: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Outer JoinOuter Join

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

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

Uses Uses nullnull values values

Page 46: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Outer Join – ExampleOuter Join – Example

Relation Relation loanloan

Relation borrowercustomer-

name loan-numberJonesSmithHayes

L-170L-230L-155

300040001700

loan-number amount

L-170L-230L-260

branch-name

DowntownRedwoodPerryridge

Page 47: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Outer Join – ExampleOuter Join – Example Inner JoinInner Join

loan Borrowerloan Borrower

loan-number amount

L-170L-230

30004000

customer-name

JonesSmith

branch-name

DowntownRedwood

JonesSmithnull

loan-number amountL-170L-230L-260

300040001700

customer-namebranch-name

DowntownRedwoodPerryridge

Left Outer Join

loan Borrower

Page 48: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Outer Join – ExampleOuter Join – Example

Right Outer JoinRight Outer Join loanloan borrowerborrower

loan borrower

Full Outer Join

loan-number amountL-170L-230L-155

30004000null

customer-name

JonesSmithHayes

branch-nameDowntownRedwoodnull

loan-number amountL-170L-230L-260L-155

300040001700null

customer-name

JonesSmithnullHayes

branch-nameDowntownRedwoodPerryridgenull

Page 49: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Modification of the DatabaseModification of the Database

The content of the database may be The content of the database may be modified using the following operations:modified using the following operations: DeletionDeletion InsertionInsertion UpdatingUpdating

All these operations are expressed All these operations are expressed using the assignment operator.using the assignment operator.

Page 50: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

DeletionDeletion A A deletedelete request is expressed similarly to a request is expressed similarly to a

query, except instead of displaying tuples to the query, except instead of displaying tuples to the user, the selected tuples are removed from the user, the selected tuples are removed from the database.database.

Can delete only whole tuples; cannot delete Can delete only whole tuples; cannot delete values on only particular attributesvalues on only particular attributes

A deletion is expressed in relational algebra by:A deletion is expressed in relational algebra by:

rr rr – – EE

where where rr is a relation and is a relation and EE is a relational is a relational algebra query.algebra query.

Page 51: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Deletion ExamplesDeletion Examples Delete all account records in the Delete all account records in the

Perryridge branch.Perryridge branch.

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 52: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

InsertionInsertion To To insertinsert data into a relation, we either:data into a relation, we either:

specify a tuple to be insertedspecify a tuple to be inserted write a query whose result is a set of tuples to be write a query whose result is a set of tuples to be

insertedinserted in relational algebra, an insertion is expressed by:in relational algebra, an insertion is expressed by:

r r r r EE

where where rr is a relation and is a relation and EE is a relational algebra is a relational algebra expression.expression.

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

Page 53: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Insertion ExamplesInsertion Examples

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

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

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

Page 54: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

UpdatingUpdating

A mechanism to A mechanism to change a valuechange a value in a tuple in a tuple without charging without charging allall values in the tuple values in the tuple

Use the generalized projection operator to do Use the generalized projection operator to do this taskthis task

rr FF1, 1, FF2, …, 2, …, FFI,I, ( (rr)) Each Each FFii is either is either

the the iith attribute of th attribute of rr, if the , if the iith attribute is not th attribute is not updated, or,updated, or,

if the attribute is to be updated if the attribute is to be updated FFii is an expression, is an expression,

involving only constants and the attributes of involving only constants and the attributes of rr, , which gives the new value for the attributewhich gives the new value for the attribute

Page 55: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Update ExamplesUpdate Examples Make interest payments by increasing all Make interest payments by increasing all

balances by 5 percent.balances by 5 percent.

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

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 56: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

ViewsViews In some cases, In some cases, it is not desirable for all users to see it is not desirable for all users to see

the entire logical modelthe entire logical model (i.e., all the actual relations (i.e., all the actual relations stored in the database.)stored in the database.)

Consider a person who needs to know a customer’s Consider a person who needs to know a customer’s loan number but has no need to see the loan amount. loan number but has no need to see the loan amount. This person should see a relation described, in the This person should see a relation described, in the relational algebra, by relational algebra, by

customer-name, loan-number customer-name, loan-number ((borrower loanborrower loan))

Any relation that is not of the conceptual model but is Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a made visible to a user as a “virtual relation” is called a viewview..

Page 57: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

View DefinitionView Definition A view is defined using the A view is defined using the create view create view statement statement

which has the formwhich has the form

create view create view v v as as <<query expression>query expression>

Once a view is defined, the view name can be used to Once a view is defined, the view name can be used to refer to the virtual relation that the view generates.refer to the virtual relation that the view generates.

View definition is not the same as creating a new View definition is not the same as creating a new relation by evaluating the query expression relation by evaluating the query expression

Rather, a view definition causes the saving of an Rather, a view definition causes the saving of an expression; the expression is substituted into expression; the expression is substituted into queries using the view.queries using the view.

Page 58: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

View ExamplesView Examples Consider the view (named Consider the view (named all-customerall-customer) )

consisting of branches and their customers.consisting of branches and their customers.

We can find all customers of the Perryridge branch

by writing:

create view all-customer as

branch-name, customer-name (depositor account)

branch-name, customer-name (borrower loan)

customer-name

(branch-name = “Perryridge” (all-customer))

Page 59: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Updates Through ViewUpdates Through View Database modifications expressed as views must be Database modifications expressed as views must be

translated to modifications of the actual relations in the translated to modifications of the actual relations in the database.database.

Consider the view is defined as: Consider the view is defined as: create view create view branch-loan branch-loan asas branch-name, loan-number branch-name, loan-number (loan)(loan)

We may write:We may write:branch-loan branch-loan branch-loan branch-loan {(“Perryridge”, L-37)} {(“Perryridge”, L-37)}

This insertion must be represented by an insertion into the This insertion must be represented by an insertion into the actual relation actual relation loanloan from which the view from which the view branch-loanbranch-loan is is constructed.constructed.

An insertion into An insertion into loanloan requires a value for requires a value for amountamount. The . The insertion can be dealt with by either:insertion can be dealt with by either: rejecting the insertion and returning an error message rejecting the insertion and returning an error message

to the user.to the user. inserting a tuple (“L-37”, “Perryridge”, inserting a tuple (“L-37”, “Perryridge”, nullnull) into the ) into the loanloan

relationrelation

Page 60: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Updates Through Views (Cont.)Updates Through Views (Cont.)

Some updates through views are Some updates through views are impossible to translate into database impossible to translate into database relation updatesrelation updatescreate view v as create view v as branch-namebranch-name = = “Perryridge”“Perryridge” ((accountaccount))))

v v v v (L-99, Downtown, 23) (L-99, Downtown, 23) Others cannot be translated uniquelyOthers cannot be translated uniquely

all-customerall-customer all-customerall-customer {(“Perryridge”, {(“Perryridge”, “John”)}“John”)}

create view all-customer as

branch-name, customer-name (depositor account)

branch-name, customer-name (borrower loan)

Page 61: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

Views Defined Using Other ViewsViews Defined Using Other Views One view may be used in the expression One view may be used in the expression

defining another view defining another view A view relation A view relation vv11 is said to is said to depend directlydepend directly on a on a

view relation view relation vv22 if if vv22 is used in the expression is used in the expression defining defining vv11

A view relation A view relation vv11 is said to is said to depend ondepend on view view relation relation vv22 if either vif either v1 1 depends directly to depends directly to vv2 2 or or

there is a path of dependencies from vthere is a path of dependencies from v11 to v to v22

A view relation A view relation vv is said to be is said to be recursiverecursive if it if it depends on itself.depends on itself.

Page 62: Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.

View ExpansionView Expansion A way to define the meaning of views defined in terms A way to define the meaning of views defined in terms

of other views.of other views.

Let view Let view vv11 be defined by an expression be defined by an expression ee11 that may that may

itself contain uses of view relations.itself contain uses of view relations. View expansion of an expression repeats the following View expansion of an expression repeats the following

replacement step:replacement step:

repeatrepeat Find any view relation Find any view relation vvii in in ee11

Replace the view relation Replace the view relation vvii by the expression defining by the expression defining vvii

untiluntil no more view relations are present in no more view relations are present in ee11

As long as the view definitions are not recursive, this As long as the view definitions are not recursive, this loop will terminateloop will terminate