• 검색 결과가 없습니다.

Example: customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city y = {Harrison, Rye, Pittsfield

N/A
N/A
Protected

Academic year: 2022

Share "Example: customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city y = {Harrison, Rye, Pittsfield"

Copied!
37
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

CHAPTER 2 CHAPTER 2

RELATIONAL MODEL

RELATIONAL MODEL

(2)

Chapter 2: Relational Model p

ƒ Structure of Relational Databases

ƒ Fundamental Relational Algebra Operations

ƒ Additional Relational Algebra Operations

ƒ Additional Relational Algebra Operations

ƒ Extended Relational-Algebra-Operations

ƒ Null Values

ƒ Modification of the Database

(3)

Basic Structure

ƒ Formally,

given sets D 1 , D 2 , …. D n a relation r is a subset of D 1 x D 2 x … x D n

R ⊆ D 1 , X ….. X D (n: degree of R) R ⊆ D 1 , X ….. X D n (n: degree of R) or

R = { <d 1 , ..., d n > | d 1D 1 , ..., d nD n } (set of tuples)

ƒ Example: customer-name = {Jones, Smith, Curry, Lindsay}

customer-street = {Main, North, Park}

customer-city y = {Harrison, Rye, Pittsfield} { , y , }

Then r = { (Jones, Main, Harrison), (Smith, North, Rye),

(4)

Attribute Types yp

ƒ Each attribute of a relation has a name

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

of the attribute

ƒ Attribute values are (normally) required to be atomic, that is, indivisible

indivisible

à E.g. multivalued attribute values are not atomic

à E g composite attribute values are not atomic E.g. composite attribute values are not atomic

ƒ The special value null is a member of every domain

Th ll l li i i h d fi i i f

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

d

à we shall ignore the effect of null values in our main presentation and

consider their effect later

(5)

Relation Schema

ƒ A 1 1 , A 2 2 , …, A n n are attributes

ƒ R = (A 1 , A 2 , …, A n ) is a relation schema

E g Customer schema = (customer name customer street customer city) E.g. Customer-schema = (customer-name, customer-street, customer-city)

ƒ r(R) is a relation (variable) on the relation schema R

E.g. customer(Customer-schema)

(6)

Relational Algebra g

ƒ Algebra : operators and operands

à Relational algebra

‚ operands : relations

b i (+ ddi i l i )

‚ operators : basic operators (+ additional operations)

 take two or more relations as inputs and give a new relation as a result.

ƒ Procedural language Procedural language

ƒ 6 Fundamental Operators

à select

à select

à project

à union

à set difference

à Cartesian product

à rename

(7)

Example Queries p Q

ƒ Find all loans of over $1200 σ amount >1200 (loan)

ƒ Find the loan number for each loan of an amount greater than

$1200

$1200

loan-numberamount>1200 (loan))

(8)

Example Queries p Q

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

customer-name (borrower) ∪ ∏ customer-name (depositor)

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

customer-name customer name (borrower) ∩ ∏ customer-name customer name (depositor)

(9)

Example Queries p Q

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

customer-namebranch-name=“Perryridge”

( σ b l b = l l b (borrower x 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

ƒ 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.

t ( σ b h “P id ”

customer-name ( σ branch-name = “Perryridge”

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

– ∏ customer-name ( depositor )

(10)

Example Queries p Q

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

Query 1

∏ (

customer-namebranch-name = “Perryridge”

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

Query 2

∏ (

customer-name (

loan.loan-number = borrower.loan-number (

( σ branch name = “Perryridge” (loan) ) x borrower

( branch-name = Perryridge ( ) ) x w

)

))

(11)

Example Queries p Q

ƒ Find the largest account balance g

à Rename account relation as d

à The query is:

balance (account) –

balance ( )

account.balance (

σ ( t x ρ ( t) )

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

)

(12)

Formal Definition

ƒ A basic expression in the relational algebra consists of either one of the following:

à A relation in the database

A l

à A constant relation

ƒ Let E 1 and E 2 be relational-algebra expressions; the following are ll l ti l l b p i

all relational-algebra expressions:

à E 1 ∪ E 2

à E 1 – E 2

à E 1 x E 2

à σ p (E 1 ), P is a predicate on attributes in E 1

à ∏ s (E 1 ), S is a list consisting of some of the attributes in E 1

à ρ N (E 1 ), N is the new name for the result of E 1

(13)

Additional Operations p

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

ƒ Set intersection

ƒ Natural join Natural join

ƒ Division A i

ƒ Assignment

(14)

Division Operation p

r ÷ s

ƒ Suited to queries that include the phrase “for all”

ƒ Suited to queries that include the phrase for all .

ƒ Let r and s be relations on schemas R and S respectively where

à R = (A 1 , …, A m , B 1 , …, B n )

à S = (B 1 , …, B n )

Th l f i l i h

The result of r ÷ s is a relation on schema R – S = (A 1 , …, A m )

r ÷ s = { t | t ∈ ∏ R-S (r) ∧ ∀ u ∈ s ( tu ∈ r ) }

(15)

Division Operation – Example p p

„ Relations r s: „ r ÷ s:

B A A B

„ Relations r, s: „ r ÷ s:

B A

α 1

2 α α 1

2 2 β

α β γ

3 1

1 s

δ γ δ δ

1 1 3 δ 4

∈ ∈

4

6

1

(16)

Division Operation – Example p p

„ Relations r s: „ r ÷ s:

A B C D E D E A B C

„ Relations r, s: „ r ÷ s:

A B α a

C D α a

E 1 1

D a b

E 1 1

A B α a

C α γ

α β

a a a

γ γ γ

a b a

1 1 1

b 1 γ a γ

s β γ

γ

a a a

γ γ γ

b a b

3 1 γ 1

γ a γ

β b 1

r

(17)

Division Operation p

ƒ Property

à Let q = r ÷ s

à Then q is the largest relation satisfying q × s ⊆ r

ƒ Definition in terms of the basic algebra operation Let r(R) and s(S) be relations, and let S ⊆ R

r ÷ s = ∏ R-S (r) –∏ R-S ( (∏ R-S (r) × s) – ∏ R-S,S (r)) To see why

à ∏ R-S,S (r) simply reorders attributes of r

(18)

Example Queries

ƒ Find all customers who have an account from at least the

p Q

“Downtown” and the “Uptown” branches.

à Query 1

customer-name ( σ branch-name=“Downtown (depositor account)) ∩

∏ ( σ (depositor account))

customer-name ( σ branch-name=“Uptown (depositor account))

à Query 2

customer-name, branch-name (depositor account)

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

(19)

Example Queries p Q

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

customer-name, branch-name customer name, branch name (depositor ( p account) )

÷ ∏ branch-namebranch-city = “Brooklyn” (branch))

(20)

Extended Relational-Algebra Operations g p

adds power and convenience to the relational algebra p g

ƒ Generalized Projection

ƒ Generalized Projection

ƒ Aggregate Functions

ƒ Outer Join

(21)

Generalized Projection j

ƒ Extends the projection operation by allowing arithmetic p j p y g functions to be used in the projection list.

F1, F2, , , , Fn , (E)

à E is any relational-algebra expression

à Each of F 1 , F 2 , …, F n are are arithmetic expressions involving constants and attributes in the schema of E.

ƒ Find how much more each person can spend credit-info(customer-name, limit, credit-balance) f ( )

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

(22)

Aggregate Functions and Operations

ƒ Aggregation function

gg g p

gg g

à takes a collection of values and returns a single value as a result.

à avg, min, max, sum, count

ƒ Aggregate operation in relational algebra

g (E)

G1, G2, …, Gn g F1(A1), F2(A2), …, Fm(Am) (E)

à E is any relational-algebra expression

à G 1 , G 2, G n is a list of attributes on which to group (can be empty)

F i f i d

à F i is an aggregate function, and

à A i is an attribute name, for i=1, …, m

(23)

Aggregate Operation – Example gg g p p

ƒ Relation r:

A B α α

C α 7

α β

α β β

7 7 β 3

β

β β

3 10

sum C

g sum(c) (r) sum-C

27

(24)

Aggregate Operation – Example

ƒ Relation account

gg g p p

branch-name account-number n h n o n n balance n Perryridge

Perryridge

A-102 A-201

400 900 Brighton

Brighton Redwood

A-217 A-215 A-222

750 750 700

g (account) branch-name balance

branch-name g sum(balance) (account)

Perryridge Brighton Redwood

1300 1500 700

ƒ Result of aggregation does not have a name

C i i i ( i SQL)

Redwood 700

à Can use rename operation to give it a name (as in SQL)

(25)

Outer Join J

ƒ An extension of the join operation that avoids loss of j p information

à Compute the join

à add tuples that do not have matching tuples in the other relation

‚ fill in undefined attribute values with null

ƒ Three types

à left outer join

à right outer join

à full outer join

ƒ Conventional join is called inner join

(26)

Outer Join – Example J p

loan-number branch-name amount

ƒ Relation loan o n n o n

L-170 L-230 L 260

3000 4000 1700 Downtown

Redwood

P id

L-260 Perryridge 1700

ƒ Relation borrower

customer-name loan-number Jones

S i h

L-170 L 230 Smith

Hayes

L-230 L-155

ƒ Inner join: loan Borrower

ƒ Inner join: loan Borrower

loan-number branch-name amount customer-name

L-170 Downtown 3000 Jones

(27)

Outer Join – Example

ƒ Left outer join:

J p

loan-number branch-name amount customer-name loan borrower

o n n o n

L-170 L-230 L 260

3000 4000 1700

o n

Jones Smith

ll n h n

Downtown Redwood

P id

L-260 Perryridge 1700 null

ƒ Right outer join : loan borrower

loan-number amount

L-170 L 230

3000 4000

customer-name Jones

Smith branch-name

Downtown Redwood L-230

L-155

4000 null

Smith Hayes Redwood

null

ƒ Full outer join : loan-number branch-name amount customer-name

(28)

Null Values

ƒ null: an unknown or non-existing value g

ƒ The result of any arithmetic expression involving null is null

ƒ Aggregate functions

ƒ Aggregate functions

à simply ignore null values (SQL semantics)

à Is an arbitrary decision Could have returned null as result instead

à Is an arbitrary decision. Could have returned null as result instead.

ƒ For duplicate elimination and grouping

ll i d lik h l d ll d b h

à null is treated like any other value, and two nulls are assumed to be the same (SQL semantics)

à Alternative: assume each null is different from each other Alternative: assume each null is different from each other

à Both are arbitrary decisions

(29)

Null Values

ƒ Comparisons with null values return the special truth value unknown

à Why do we need unknown?

à If false was used instead of unknown, then

not (A < 5) would not be equivalent to A >= 5 not (A < 5) would not be equivalent to A > 5

ƒ Three-valued logic using the truth value unknown:

à 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 ( )

à In SQL, “P is unknown” evaluates to true if predicate P evaluates to unknown

ƒ Result of select predicate is treated as false if it evaluates to unknown

(30)

Modification of the Database

ƒ The content of the database may be modified using the y g following operations:

à Deletion

à Insertion

à Updating

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

(31)

Deletion

ƒ A delete request is expressed similarly to a query, except instead q p y q y p of displaying tuples to the user, the selected tuples are removed from the database.

ƒ Deletion operation in relational algebra : r ← r E

r ← r – E

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

ƒ You can only delete whole tuples;

à cannot delete values on only particular attributes

=> should use update operation

(32)

Deletion Examples

ƒ Delete all account records in the Perryridge branch.

p

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

ƒ Delete all loan records with amount in the range of 0 to 50 loan ← loan – σ amount 0 and amount 50 (loan)

ƒ Delete all accounts at branches located in Needham.

r 1 ← σ branch-city = “Needham” (account branch)

r ← ∏ b h b b l (r )

r 2 ← ∏ branch-name, account-number, balance (r 1 )

r 3 ← ∏ customer-name, account-number (r 2 depositor)

t ← t

account ← account – r 2

depositor ← depositor – r 3

(33)

Insertion

ƒ To insert data into a relation, we either:

à specify a tuple to be inserted

à write a query whose result is a set of tuples to be inserted

ƒ Insertion operation in relational algebra:

r ← r ∪ E 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.

(34)

Insertion Examples p

ƒ Insert information in the database specifying that Smith has p y g

$1200 in account A-973 at the Perryridge branch.

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

account ← account ∪ {( Perryridge , A 973, 1200)}

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

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

account number for the new savings account.

r 1 ← (σ branch-name = “Perryridge” (borrower loan))

account ← account ∪ ∏ branch-name account-number 200 branch-name, account-number,200 (r ( 1 1 ) )

depositor ← depositor ∪ ∏ ,(r )

(35)

Updating p g

ƒ A mechanism to change a value in a tuple without changing all g p g g values in the tuple

ƒ Use the generalized projection operator to do this task Use the generalized projection operator to do this task r ← ∏ F1, F2, …, FI, (r)

Each F i , is either

à the ith attribute of r: the attribute is not updated, or,

à an expression, involving only constants and the attributes of r, which gives

h l f h ib h ib i d d

the new value for the attribute: the attribute is updated

(36)

Update Examples p p

ƒ Make interest payments by increasing all balances by 5 percent. p y y g y p account ← ∏ account#, branch-name, balance*1.05 (account)

ƒ Pay all accounts with balances over $10,000 6 percent interest

d ll h 5

and pay all others 5 percent account ←

account#, branch-name, balance*1.06 , ,balance > 10000 (account) ) ∪

account#, branch-name, balance*1.05 balance 10000 (account) )

(37)

END OF CHAPTER 2

END OF CHAPTER 2

참조

관련 문서

à the equilibrium constant for reaction with HCl is.. § Strong base reacts “completely” with a weak acid. à because the equilibrium constant is, again, very large. § If HA

- how magnetic, inertial, and pressure forces interact within an ideal perfectly conducting plasma in an arbitrary magnetic geometry.. - Any fusion reactor must

As shown in the results of this study through research methods and data processing described above, since customer service is increasingly important, dance

à Element nodes have child nodes, which can be attributes or subelements à Text in an element is modeled as a text node child of the element. à Children of a node

(select branch name customer name (select branch_name, customer_name from borrower, loan. where borrower.loan_number

The purpose of this study is to determine customer satisfaction and repurchase intentions based on various characteristics when Korean Chinese consumers actually

„ Assumption: an active customer type i only binding incentive compatibility constraint is the one for type j=i -1.. t* is lowest type of

- Divergence: independent of (initial a.o.a) and airfoil camber the increase in aerodynamic moment about the elastic axis due to an arbitrary change in a.o.a is equal