Contenu connexe
Similaire à Relational algebra.pptx (20)
Relational algebra.pptx
- 1. ©Silberschatz, Korth and Sudarshan3.1Database System Concepts
Chapter 3: Relational Model
Structure of Relational Databases
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Extended Relational-Algebra-Operations
Modification of the Database
Views
- 3. ©Silberschatz, Korth and Sudarshan3.3Database System Concepts
Relation Instance
The current values (relation instance) of a relation are
specified by a table
An element t of r is a tuple, represented by a row in a table
Jones
Smith
Curry
Lindsay
customer-name
Main
North
North
Park
customer-street
Harrison
Rye
Rye
Pittsfield
customer-city
customer
attributes
(or columns)
tuples
(or rows)
- 4. ©Silberschatz, Korth and Sudarshan3.4Database System Concepts
Query Language
A query language is a language in which a user requests
information from the database.
These languages are usually on a level higher than that of a
standard programming language.
Query languages can be categorized as either procedural or
nonprocedural.
In a procedural language, the user instructs the system to perform a
sequence of operations on the database to compute the desired
result.
In a nonprocedural language, the user describes the desired
information without giving a specific procedure for obtaining that
information.
The relational algebra is procedural.
the tuple relational calculus and domain relational calculus are
nonprocedural.
- 5. ©Silberschatz, Korth and Sudarshan3.5Database System Concepts
RELATIONAL ALGEBRA
The relational algebra is a procedural query language. It consists
of a set of operations that take one or two relations (tables) as
input and produce a new relation, on the request of the user to
retrieve the specific information, as the output.
The relational algebra contains the following operations
- 6. ©Silberschatz, Korth and Sudarshan3.6Database System Concepts
Relational Algebra
Procedural language
Six basic operators
select
project
union
set difference
Cartesian product
rename
The operators take one or more relations as inputs and give a
new relation as a result.
- 8. ©Silberschatz, Korth and Sudarshan3.8Database System Concepts
Unary Relational Operations
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: ρ)
Relational Algebra Operations From Set Theory
UNION (υ)
INTERSECTION ( ),
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
Binary Relational Operations
JOIN
DIVISION
- 9. ©Silberschatz, Korth and Sudarshan3.9Database System Concepts
SELECTION (σ)
• The SELECT operation is used for selecting a subset
of the tuples according to a given selection condition.
Sigma(σ)Symbol denotes it.
• Ex:- find all employees born after 1st Jan
1950:
•
dob σ
'01/JAN/1950'(employee)
- 10. ©Silberschatz, Korth and Sudarshan3.10Database System Concepts
σp(r)
σ is the predicate
r stands for relation which is the name of the table
p is prepositional logic
- 11. ©Silberschatz, Korth and Sudarshan3.11Database System Concepts
Example 1
σ topic = "Database" (Tutorials)
Output - Selects tuples from Tutorials where topic = 'Database'.
Example 2
σ topic = "Database" and author = "guru99"( Tutorials)
Output - Selects tuples from Tutorials where the topic is
'Database' and 'author' is guru99.
Example 3
σ sales > 50000 (Customers)
Output - Selects tuples from Customers where sales is greater
than 50000
- 12. ©Silberschatz, Korth and Sudarshan3.12Database System Concepts
sSalary > 40000 (Employee)
SSN Name Salary
1234545 John 200000
5423341 Smith 600000
4352342 Fred 500000
SSN Name Salary
5423341 Smith 600000
4352342 Fred 500000
- 13. ©Silberschatz, Korth and Sudarshan3.13Database System Concepts
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and
'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and
'price' is 450 or those books published after 2010.
- 14. ©Silberschatz, Korth and Sudarshan3.14Database System Concepts
PROJECTION(∏ )Pi
• ∏ (pi) symbol used to choose attributes
from a relation.
This helps to extract the values of
specified attributes to eliminates
duplicate values. (pi) symbol is used to
choose attributes from a relation.
- 15. ©Silberschatz, Korth and Sudarshan3.15Database System Concepts
SELECTION & PROJECTION Example
Id
Person
Name Address Hobby
1123
1123
5556
9876
John
John
Mary
Bart
123 Main
123 Main
7 Lake Dr
5 Pine St
stamps
coins
hiking
stamps
Id Hobby
1123
Name Address
John 123 Main stamps
9876 Bart 5 Pine St stamps
σ Hobby=‘stamps’(Person)
∏Name,Hobby(Person)
Name Hobby
John
John
Mary
Bart
stamps
coins
Hiking
stamps
- 16. ©Silberschatz, Korth and Sudarshan3.16Database System Concepts
P Name,Salary (Employee)
SSN Name Salary
1234545 John 200000
5423341 John 600000
4352342 John 200000
Name Salary
John 20000
John 60000
- 17. ©Silberschatz, Korth and Sudarshan3.17Database System Concepts
Project 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)
- 18. ©Silberschatz, Korth and Sudarshan3.18Database System Concepts
CustomerID CustomerName Status
1 Google Active
2 Amazon Active
3 Apple Inactive
4 Alibaba Active
Here, the projection of CustomerName and status will give
Π CustomerName, Status (Customers)
CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active
- 19. ©Silberschatz, Korth and Sudarshan3.19Database System Concepts
The results of relational algebra are also relations but without
any name. The rename operation allows us to rename the output
relation. 'rename' operation is denoted with small Greek letter rho
ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x
'rename'
- 20. ©Silberschatz, Korth and Sudarshan3.20Database System Concepts
UNION
UNION is symbolized by ∪ symbol. It includes all tuples that are
in tables A or in B. It also eliminates duplicate tuples. So, set A
UNION set B would be expressed as:
The result <- A ∪ B
For a union operation to be valid, the following conditions must
hold -
R and S must be the same number of attributes.
Attribute domains need to be compatible.
Duplicate tuples should be automatically removed.
- 21. ©Silberschatz, Korth and Sudarshan3.21Database System Concepts
Union Operation – Example
Relations r, s:
r s:
A B
1
2
1
A B
2
3
r
s
A B
1
2
1
3
- 22. ©Silberschatz, Korth and Sudarshan3.22Database System Concepts
TABLE A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
- 23. ©Silberschatz, Korth and Sudarshan3.23Database System Concepts
Set Difference (-)
- Symbol denotes it. The result of A - B, is a relation which
includes all tuples that are in A but not in B.
The attribute name of A has to match with the attribute name in
B.
- 24. ©Silberschatz, Korth and Sudarshan3.24Database System Concepts
Set Difference Operation – Example
Relations r, s:
r – s:
A B
1
2
1
A B
2
3
r
s
A B
1
1
- 25. ©Silberschatz, Korth and Sudarshan3.25Database System Concepts
What about Intersection ?
An intersection is defined by the symbol ∩
A ∩ B
Defines a relation consisting of a set of all tuple that are in both
A and B. However, A and B must be union-compatible.
- 26. ©Silberschatz, Korth and Sudarshan3.26Database System Concepts
Table A ∩ B
TABLE A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
column 1 column 2
1 1
- 27. ©Silberschatz, Korth and Sudarshan3.27Database System Concepts
Cartesian-Product Operation
artesian Product in DBMS is an operation used to merge
columns from two relations.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles
written by tutorialspoint.
- 28. ©Silberschatz, Korth and Sudarshan3.28Database System Concepts
Cartesian-Product Operation-Example
Relations r, s:
r x s:
A B
1
2
A B
1
1
1
1
2
2
2
2
C D
10
10
20
10
10
10
20
10
E
a
a
b
b
a
a
b
b
C D
10
10
20
10
E
a
a
b
br
s
- 36. ©Silberschatz, Korth and Sudarshan3.36Database System Concepts
NATURAL JOIN (⋈)
Natural join can only be performed if there is a common attribute
(column) between the relations. The name and type of the attribute
must be same.
- 39. ©Silberschatz, Korth and Sudarshan3.39Database System Concepts
Example: Find the employee names and city who have salary
details.
semp_name, salary, city ( employee ⋈ employee_works )
The join operation selects all employees with salary details, from
where we can easily project the employee names, cities and
salaries. Natural Join operation results in some loss of
information
- 44. ©Silberschatz, Korth and Sudarshan3.44Database System Concepts
Outer Join – Example
Relation loan
Relation borrower
customer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
3000
4000
1700
loan-number amount
L-170
L-230
L-260
branch-name
Downtown
Redwood
Perryridge
- 45. ©Silberschatz, Korth and Sudarshan3.45Database System Concepts
Outer Join – Example
Inner Join
loan Borrower
loan-number amount
L-170
L-230
3000
4000
customer-name
Jones
Smith
branch-name
Downtown
Redwood
Jones
Smith
null
loan-number amount
L-170
L-230
L-260
3000
4000
1700
customer-namebranch-name
Downtown
Redwood
Perryridge
Left Outer Join
loan Borrower
- 46. ©Silberschatz, Korth and Sudarshan3.46Database System Concepts
Outer Join – Example
Right Outer Join
loan borrower
loan borrower
Full Outer Join
loan-number amount
L-170
L-230
L-155
3000
4000
null
customer-name
Jones
Smith
Hayes
branch-name
Downtown
Redwood
null
loan-number amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer-name
Jones
Smith
null
Hayes
branch-name
Downtown
Redwood
Perryridge
null
- 50. ©Silberschatz, Korth and Sudarshan3.50Database System Concepts
Banking Example
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)
- 51. ©Silberschatz, Korth and Sudarshan3.51Database System Concepts
Example Queries
Find all loans of over $1200
Find the loan number for each loan of an amount greater than
$1200
samount > 1200 (loan)
loan-number (samount > 1200 (loan))
- 52. ©Silberschatz, Korth and Sudarshan3.52Database System Concepts
Example Queries
Find the names of all customers who have a loan, an account, or
both, from the bank
Find the names of all customers who have a loan and an
account at bank.
customer-name (borrower) customer-name (depositor)
customer-name (borrower) customer-name (depositor)
- 53. ©Silberschatz, Korth and Sudarshan3.53Database System Concepts
53
Examples
Sailors (sid, name,
rating, age) bid name color
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
Boats (bid, name,
color) sid bid day
1 101 10/10/12
1 102 10/10/12
1 101 10/7/12
2 102 11/9/12
2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
4 103 19/9/12
Reserves (sid, bid,
day)
List names of boats.
List ratings and ages sailors.
List names of sailors who are over 21 years old.
List names of red boats.
πname (
Boats)
πrating, age
(Sailors)
πname ( σage>21
(Sailors))πname ( σcolor=red
(Boats))
sid name rating age
1 Dustin 7 45
2 Rusty 10 35
3 Horatio 5 35
4 Zorba 8 18
5 Julius 25
- 54. ©Silberschatz, Korth and Sudarshan3.54Database System Concepts
54
Examples
Sailors (sid, name,
rating, age) bid name color
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
Boats (bid, name,
color) sid bid day
1 101 10/10/12
1 102 10/10/12
1 101 10/7/12
2 102 11/9/12
2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
4 103 19/9/12
Reserves (sid, bid,
day)
List ids of boats named Interlake
List ids of boats reserved on 10/10/12
sid name rating age
1 Dustin 7 45
2 Rusty 10 35
3 Horatio 5 35
4 Zorba 8 18
5 Julius 25
- 55. ©Silberschatz, Korth and Sudarshan3.55Database System Concepts
55
Examples (solution)
Sailors (sid, name,rating, age) Boats (bid, name,
color) Reserves (sid, bid,day)bid name color
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
sid bid day
1 101 10/10/12
1 102 10/10/12
1 101 10/7/12
2 102 11/9/12
2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
4 103 19/9/12List ids of boats named Interlake
bid (snameInterlake(Boats))
List ids of boats reserved on 10/10/12
bid (sday10/10/12 (Reserves))
sid name rating age
1 Dustin 7 45
2 Rusty 10 35
3 Horatio 5 35
4 Zorba 8 18
5 Julius 25
- 56. ©Silberschatz, Korth and Sudarshan3.56Database System Concepts
56
Examples
Sailors (sid, name,
rating, age) bid name color
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
Boats (bid, name,
color) sid bid day
1 101 10/10/12
1 102 10/10/12
1 101 10/7/12
2 102 11/9/12
2 102 7/11/12
3 101 7/11/12
3 102 7/8/12
4 103 19/9/12
Reserves (sid, bid,
day)
List ids of sailors who reserved boat 102
πsid ( σbid=102 Reserves)
List names of sailors who reserved boat 102
πname (Sailors ⨝ ( σbid=102
Reserves))
πname (σbid=102 (Sailors ⨝
Reserves))
both are correct!
which is better?
sid name rating age
1 Dustin 7 45
2 Rusty 10 35
3 Horatio 5 35
4 Zorba 8 18
5 Julius 25
- 57. ©Silberschatz, Korth and Sudarshan3.57Database System Concepts
Example Queries
Find the names of all customers having a loan at the Perryridge
branch
{t | s borrower( t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge”
u[loan-number] = s[loan-number]))
not v depositor (v[customer-name] =
t[customer-name]) }
Find the names of all customers who have a loan at the
Perryridge branch, but no account at any branch of the bank
{t | s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge”
u[loan-number] = s[loan-number]))}
- 58. ©Silberschatz, Korth and Sudarshan3.58Database System Concepts
Example Queries
Find the names of all customers having a loan from the
Perryridge branch, and the cities they live in
{t | s loan(s[branch-name] = “Perryridge”
u borrower (u[loan-number] = s[loan-number]
t [customer-name] = u[customer-name])
v customer (u[customer-name] = v[customer-name]
t[customer-city] = v[customer-city])))}
- 59. ©Silberschatz, Korth and Sudarshan3.59Database System Concepts
Example Queries
Find the names of all customers who have an account at all
branches located in Brooklyn:
{t | c customer (t[customer.name] = c[customer-name])
s branch(s[branch-city] = “Brooklyn”
u account ( s[branch-name] = u[branch-name]
s depositor ( t[customer-name] = s[customer-name]
s[account-number] = u[account-number] )) )}
- 60. ©Silberschatz, Korth and Sudarshan3.60Database System Concepts
Safety of Expressions
It is possible to write tuple calculus expressions that generate
infinite relations.
For example, {t | t r} results in an infinite relation if the
domain of any attribute of relation r is infinite
To guard against the problem, we restrict the set of allowable
expressions to safe expressions.
An expression {t | P(t)} in the tuple relational calculus is safe if
every component of t appears in one of the relations, tuples, or
constants that appear in P
NOTE: this is more than just a syntax condition.
E.g. { t | t[A]=5 true } is not safe --- it defines an infinite set with
attribute values that do not appear in any relation or tuples or
constants in P.
- 61. ©Silberschatz, Korth and Sudarshan3.61Database System Concepts
Domain Relational Calculus
A nonprocedural query language equivalent in power to the tuple
relational calculus
Each query is an expression of the form:
{ x1, x2, …, xn | P(x1, x2, …, xn)}
x1, x2, …, xn represent domain variables
P represents a formula similar to that of the predicate calculus
- 62. ©Silberschatz, Korth and Sudarshan3.62Database System Concepts
Example Queries
Find the loan-number, branch-name, and amount for loans of over
$1200
{ c, a | l ( c, l borrower b( l, b, a loan
b = “Perryridge”))}
or { c, a | l ( c, l borrower l, “Perryridge”, a loan)}
Find the names of all customers who have a loan from the
Perryridge branch and the loan amount:
{ c | l, b, a ( c, l borrower l, b, a loan a > 1200)}
Find the names of all customers who have a loan of over $1200
{ l, b, a | l, b, a loan a > 1200}
- 63. ©Silberschatz, Korth and Sudarshan3.63Database System Concepts
Example Queries
Find the names of all customers having a loan, an account, or
both at the Perryridge branch:
{ c | s, n ( c, s, n customer)
x,y,z( x, y, z branch y = “Brooklyn”)
a,b( x, y, z account c,a depositor)}
Find the names of all customers who have an account at all
branches located in Brooklyn:
{ c | l ({ c, l borrower
b,a( l, b, a loan b = “Perryridge”))
a( c, a depositor
b,n( a, b, n account b = “Perryridge”))}
- 64. ©Silberschatz, Korth and Sudarshan3.64Database System Concepts
Safety of Expressions
{ x1, x2, …, xn | P(x1, x2, …, xn)}
is safe if all of the following hold:
1.All values that appear in tuples of the expression are values
from dom(P) (that is, the values appear either in P or in a tuple
of a relation mentioned in P).
2.For every “there exists” subformula of the form x (P1(x)), the
subformula is true if and only if there is a value of x in dom(P1)
such that P1(x) is true.
3. For every “for all” subformula of the form x (P1 (x)), the
subformula is true if and only if P1(x) is true for all values x
from dom (P1).
- 68. ©Silberschatz, Korth and Sudarshan3.68Database System Concepts
Names of All Customers Who Have
Either a Loan or an Account
- 71. ©Silberschatz, Korth and Sudarshan3.71Database System Concepts
Result of s branch-name = “Perryridge” (borrower loan)
- 75. ©Silberschatz, Korth and Sudarshan3.75Database System Concepts
Customers Who Live on the Same Street and In the
Same City as Smith
- 76. ©Silberschatz, Korth and Sudarshan3.76Database System Concepts
Customers With Both an Account and a Loan
at the Bank
- 77. ©Silberschatz, Korth and Sudarshan3.77Database System Concepts
Result of Pcustomer-name, loan-number, amount
(borrower loan)
- 78. ©Silberschatz, Korth and Sudarshan3.78Database System Concepts
Result of Pbranch-name(scustomer-city =
“Harrison”(customer account depositor))
- 79. ©Silberschatz, Korth and Sudarshan3.79Database System Concepts
Result of Pbranch-name(sbranch-city =
“Brooklyn”(branch))
- 80. ©Silberschatz, Korth and Sudarshan3.80Database System Concepts
Result of Pcustomer-name, branch-name(depositor account)
- 82. ©Silberschatz, Korth and Sudarshan3.82Database System Concepts
Result of Pcustomer-name, (limit – credit-balance) as
credit-available(credit-info).
- 86. ©Silberschatz, Korth and Sudarshan3.86Database System Concepts
Result of branch-name sum salary, max(salary) as
max-salary (pt-works)
- 93. ©Silberschatz, Korth and Sudarshan3.93Database System Concepts
Names of All Customers Who Have a
Loan at the Perryridge Branch