Upload
darleen-park
View
235
Download
1
Embed Size (px)
DESCRIPTION
©Silberschatz, Korth and Sudarshan2.3Database System Concepts - 5 th Edition, Oct 5, 2006 Examples Find out the student names and course titles they registered to π name, title ( Student.roll = Registered.roll^ Registered.code=Course.code (Student X Registered X Course)) Consider the following relations: Student(roll, name, address, phone) Course(code, title) Registered(roll, code) Name of student who are registered to ‘Database’ or ‘Algorithms’ course π name ( Student.roll = Registered.roll^ Registered.code=Course.code (Student X Registered X (σ title=’Database’ Course)) ) ∪ π name ( Student.roll = Registered.roll^ Registered.code=Course.code (Student X Registered X (σ title=’Algorithm’ Course)) )
Citation preview
Database System Concepts, 5th Ed.©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Chapter 2: Relational Model
©Silberschatz, Korth and Sudarshan2.2Database System Concepts - 5th Edition, Oct 5, 2006
ExamplesConsider the following relations: Student(roll, name, address, phone) Course(code, title) Registered(roll, code)
• Find out the course code in which at least one student is registeredπcode ( Registered)
• Find out the titles of registered courses πtitle
(Course.code = Registered.code (Course X Registered ))
• Find out the course code in which no student is registered πcode
( Course ) - πcode ( Registered )
©Silberschatz, Korth and Sudarshan2.3Database System Concepts - 5th Edition, Oct 5, 2006
Examples
• Find out the student names and course titles they registered toπname, title
(Student.roll = Registered.roll^ Registered.code=Course.code (Student X Registered X Course))
Consider the following relations: Student(roll, name, address, phone) Course(code, title) Registered(roll, code)
• Name of student who are registered to ‘Database’ or ‘Algorithms’ course πname(Student.roll = Registered.roll^ Registered.code=Course.code(Student X Registered X (σ title=’Database’
Course)) ) ∪πname(Student.roll = Registered.roll^ Registered.code=Course.code(Student X Registered X (σ title=’Algorithm’
Course)) )
©Silberschatz, Korth and Sudarshan2.4Database System Concepts - 5th Edition, Oct 5, 2006
Rename Operation Allows us to name and therefore to refer to the results of relational-
algebra expressions. Allows us to refer to a relation by more than one name. Example:
x (E)
returns the expression E under the name X If a relational-algebra expression E has arity n, then
returns the result of expression E under the name X, and with theattributes renamed to A1 , A2 , …., An .
)(),...,,( 21E
nAAAx
©Silberschatz, Korth and Sudarshan2.5Database System Concepts - 5th Edition, Oct 5, 2006
Banking Examplebranch (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)
©Silberschatz, Korth and Sudarshan2.6Database System Concepts - 5th Edition, Oct 5, 2006
Example Queries Find the largest account balance
Strategy: Find those balances that are not the largest
– Rename account relation as d so that we can compare each account balance with all others
Use set difference to find those account balances that were not found in the earlier step.
The query is:
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
©Silberschatz, Korth and Sudarshan2.7Database System Concepts - 5th Edition, Oct 5, 2006
Example Queries Find the names of all customers who have a loan at the Dhanmondi
branch.
Query 2
customer_name(loan.loan_number = borrower.loan_number (
(branch_name = “Dhanmondi” (loan)) x borrower))
Query 1
customer_name (branch_name = “Dhanmondi” (
borrower.loan_number = loan.loan_number (borrower x loan)))
©Silberschatz, Korth and Sudarshan2.8Database System Concepts - 5th Edition, Oct 5, 2006
Formal Definition
A basic expression in the relational algebra consists of either one of the following: A relation in the database A constant relation
Let E1 and E2 be relational-algebra expressions; the following are all 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 some of the attributes in E1
x (E1), x is the new name for the result of E1
©Silberschatz, Korth and Sudarshan2.9Database System Concepts - 5th Edition, Oct 5, 2006
Additional Operations
We define additional operations that do not add any power to therelational algebra, but that simplify common queries.
Set intersection Natural join Division Assignment
©Silberschatz, Korth and Sudarshan2.10Database System Concepts - 5th Edition, Oct 5, 2006
Set-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)
©Silberschatz, Korth and Sudarshan2.11Database System Concepts - 5th Edition, Oct 5, 2006
Set-Intersection Operation – Example
Relation r, s:
r s
A B
121
A B
23
r s
A B
2
©Silberschatz, Korth and Sudarshan2.12Database System Concepts - 5th Edition, Oct 5, 2006
Example Query Find the names of all customers who have a loan and an account at
the bank.
customer_name (borrower) customer_name (depositor)
©Silberschatz, Korth and Sudarshan2.13Database System Concepts - 5th Edition, Oct 5, 2006
Notation: r s
Natural-Join Operation
Let r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows:
Consider each pair of tuples tr from r and ts from s.
If tr and ts have the same value on each of the attributes in R S, add a tuple t to the result, where
t has the same value as tr on r
t has the same value as ts on s
Example:R = (A, B, C, D)S = (E, B, D) Result schema = (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))
©Silberschatz, Korth and Sudarshan2.14Database System Concepts - 5th Edition, Oct 5, 2006
Natural Join Operation – Example 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
©Silberschatz, Korth and Sudarshan2.15Database System Concepts - 5th Edition, Oct 5, 2006
Example Query
Find the name and loan amount of all customers who have a loan at the bank.
customer_name, amount (borrower loan)
customer. customer_name (customer_city = “Jessore” (customer depositor account ))
Find the names of all customers who have an account in the bank and who live in Jessore
Natural Join is associative
©Silberschatz, Korth and Sudarshan2.16Database System Concepts - 5th Edition, Oct 5, 2006
Example Query Find the names of all customers who have a loan and an account at
the bank.
customer_name (borrower) customer_name (depositor)
customer_name (borrower depositor)
When R S =R X S ?
If R S= φ
©Silberschatz, Korth and Sudarshan2.17Database System Concepts - 5th Edition, Oct 5, 2006
Summary
©Silberschatz, Korth and Sudarshan2.18Database System Concepts - 5th Edition, Oct 5, 2006
Division Operation
Notation: Suited to queries that include the phrase “for all”.
Let r and s be relations on schemas R and S respectively where R = (A1, …, Am , B1, …, Bn )
S = (B1, …, Bn)
The result of r s is a relation on schema
R – S = (A1, …, Am)
r s = { t | t R-S (r) u s ( tu r ) }
Where tu means the concatenation of tuples t and u to produce a single tuple
r s
©Silberschatz, Korth and Sudarshan2.19Database System Concepts - 5th Edition, Oct 5, 2006
Division Operation – Example
Relations r, s:
r s:
B
A
1
2
A B
12311134612
r
s
©Silberschatz, Korth and Sudarshan2.20Database System Concepts - 5th Edition, Oct 5, 2006
Another 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
©Silberschatz, Korth and Sudarshan2.21Database System Concepts - 5th Edition, Oct 5, 2006
Find all customers who have an account at all branches located in Brooklyn city.
Bank Example Queries
customer_name, branch_name (depositor account)
branch_name (branch_city = “Brooklyn” (branch))
©Silberschatz, Korth and Sudarshan2.22Database System Concepts - 5th Edition, Oct 5, 2006
Assignment Operation
The assignment operation () provides a convenient way to express complex queries. Write query as a sequential program consisting of
a series of assignments followed by an expression whose value is displayed as a result of
the query. Assignment must always be made to a temporary relation variable.
Example:
The result to the right of the is assigned to the relation variable on the left of the .
May use variable in subsequent expressions.
r1 branch_city = “dhaka” (account branch )
r2 account_number (r1)