SlideShare une entreprise Scribd logo
1  sur  97
©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
©Silberschatz, Korth and Sudarshan3.2Database System Concepts
Example of a Relation
©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)
©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.
©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
©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.
©Silberschatz, Korth and Sudarshan3.7Database System Concepts
©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
©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)
©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
©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
©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
©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.
©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.
©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
©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
©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)
©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
©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'
©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.
©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
©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
©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.
©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
©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.
©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
©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.
©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
©Silberschatz, Korth and Sudarshan3.29Database System Concepts
©Silberschatz, Korth and Sudarshan3.30Database System Concepts
Join Operations
©Silberschatz, Korth and Sudarshan3.31Database System Concepts
©Silberschatz, Korth and Sudarshan3.32Database System Concepts
©Silberschatz, Korth and Sudarshan3.33Database System Concepts
©Silberschatz, Korth and Sudarshan3.34Database System Concepts
©Silberschatz, Korth and Sudarshan3.35Database System Concepts
©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.
©Silberschatz, Korth and Sudarshan3.37Database System Concepts
©Silberschatz, Korth and Sudarshan3.38Database System Concepts
©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
©Silberschatz, Korth and Sudarshan3.40Database System Concepts
©Silberschatz, Korth and Sudarshan3.41Database System Concepts
©Silberschatz, Korth and Sudarshan3.42Database System Concepts
Right Outer Join:
©Silberschatz, Korth and Sudarshan3.43Database System Concepts
©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
©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
©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
©Silberschatz, Korth and Sudarshan3.47Database System Concepts
©Silberschatz, Korth and Sudarshan3.48Database System Concepts
©Silberschatz, Korth and Sudarshan3.49Database System Concepts
©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)
©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))
©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)
©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
©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
©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 (snameInterlake(Boats))
List ids of boats reserved on 10/10/12
bid (sday10/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
©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
©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]))}
©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])))}
©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] )) )}
©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.
©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
©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}
©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”))}
©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).
End of Chapter 3
©Silberschatz, Korth and Sudarshan3.66Database System Concepts
Result of s branch-name = “Perryridge” (loan)
©Silberschatz, Korth and Sudarshan3.67Database System Concepts
Loan Number and the Amount of the Loan
©Silberschatz, Korth and Sudarshan3.68Database System Concepts
Names of All Customers Who Have
Either a Loan or an Account
©Silberschatz, Korth and Sudarshan3.69Database System Concepts
Customers With An Account But No Loan
©Silberschatz, Korth and Sudarshan3.70Database System Concepts
Result of borrower  loan
©Silberschatz, Korth and Sudarshan3.71Database System Concepts
Result of s branch-name = “Perryridge” (borrower  loan)
©Silberschatz, Korth and Sudarshan3.72Database System Concepts
Result of Pcustomer-name
©Silberschatz, Korth and Sudarshan3.73Database System Concepts
Result of the Subexpression
©Silberschatz, Korth and Sudarshan3.74Database System Concepts
Largest Account Balance in the Bank
©Silberschatz, Korth and Sudarshan3.75Database System Concepts
Customers Who Live on the Same Street and In the
Same City as Smith
©Silberschatz, Korth and Sudarshan3.76Database System Concepts
Customers With Both an Account and a Loan
at the Bank
©Silberschatz, Korth and Sudarshan3.77Database System Concepts
Result of Pcustomer-name, loan-number, amount
(borrower loan)
©Silberschatz, Korth and Sudarshan3.78Database System Concepts
Result of Pbranch-name(scustomer-city =
“Harrison”(customer account depositor))
©Silberschatz, Korth and Sudarshan3.79Database System Concepts
Result of Pbranch-name(sbranch-city =
“Brooklyn”(branch))
©Silberschatz, Korth and Sudarshan3.80Database System Concepts
Result of Pcustomer-name, branch-name(depositor account)
©Silberschatz, Korth and Sudarshan3.81Database System Concepts
The credit-info Relation
©Silberschatz, Korth and Sudarshan3.82Database System Concepts
Result of Pcustomer-name, (limit – credit-balance) as
credit-available(credit-info).
©Silberschatz, Korth and Sudarshan3.83Database System Concepts
The pt-works Relation
©Silberschatz, Korth and Sudarshan3.84Database System Concepts
The pt-works Relation After Grouping
©Silberschatz, Korth and Sudarshan3.85Database System Concepts
Result of branch-name  sum(salary) (pt-works)
©Silberschatz, Korth and Sudarshan3.86Database System Concepts
Result of branch-name  sum salary, max(salary) as
max-salary (pt-works)
©Silberschatz, Korth and Sudarshan3.87Database System Concepts
The employee and ft-works Relations
©Silberschatz, Korth and Sudarshan3.88Database System Concepts
The Result of employee ft-works
©Silberschatz, Korth and Sudarshan3.89Database System Concepts
The Result of employee ft-works
©Silberschatz, Korth and Sudarshan3.90Database System Concepts
Result of employee ft-works
©Silberschatz, Korth and Sudarshan3.91Database System Concepts
Result of employee ft-works
©Silberschatz, Korth and Sudarshan3.92Database System Concepts
Tuples Inserted Into loan and borrower
©Silberschatz, Korth and Sudarshan3.93Database System Concepts
Names of All Customers Who Have a
Loan at the Perryridge Branch
©Silberschatz, Korth and Sudarshan3.94Database System Concepts
E-R Diagram
©Silberschatz, Korth and Sudarshan3.95Database System Concepts
The branch Relation
©Silberschatz, Korth and Sudarshan3.96Database System Concepts
The loan Relation
©Silberschatz, Korth and Sudarshan3.97Database System Concepts
The borrower Relation

Contenu connexe

Tendances

2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS
koolkampus
 

Tendances (20)

2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS2. Entity Relationship Model in DBMS
2. Entity Relationship Model in DBMS
 
basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptx
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Dbms architecture
Dbms architectureDbms architecture
Dbms architecture
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Data abstraction in DBMS
Data abstraction in DBMSData abstraction in DBMS
Data abstraction in DBMS
 
Entity Relationship Diagrams
Entity Relationship DiagramsEntity Relationship Diagrams
Entity Relationship Diagrams
 
Databases: Normalisation
Databases: NormalisationDatabases: Normalisation
Databases: Normalisation
 
Data Models
Data ModelsData Models
Data Models
 
joins in database
 joins in database joins in database
joins in database
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)
 
Dbms 3: 3 Schema Architecture
Dbms 3: 3 Schema ArchitectureDbms 3: 3 Schema Architecture
Dbms 3: 3 Schema Architecture
 
Dbms Introduction and Basics
Dbms Introduction and BasicsDbms Introduction and Basics
Dbms Introduction and Basics
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
Ppt of dbms e r features
Ppt of dbms e r featuresPpt of dbms e r features
Ppt of dbms e r features
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
 
Functional dependency
Functional dependencyFunctional dependency
Functional dependency
 
Unit01 dbms
Unit01 dbmsUnit01 dbms
Unit01 dbms
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Cost estimation for Query Optimization
Cost estimation for Query OptimizationCost estimation for Query Optimization
Cost estimation for Query Optimization
 

Similaire à Relational algebra.pptx

jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybbjhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
WrushabhShirsat3
 

Similaire à Relational algebra.pptx (20)

Ch3 a
Ch3 aCh3 a
Ch3 a
 
Ch3
Ch3Ch3
Ch3
 
relational algebra and calculus queries .ppt
relational algebra and calculus queries .pptrelational algebra and calculus queries .ppt
relational algebra and calculus queries .ppt
 
DBMS _Relational model
DBMS _Relational modelDBMS _Relational model
DBMS _Relational model
 
03 Relational Databases.ppt
03 Relational Databases.ppt03 Relational Databases.ppt
03 Relational Databases.ppt
 
Ch14
Ch14Ch14
Ch14
 
Sql.pptx
Sql.pptxSql.pptx
Sql.pptx
 
Relational Algebra and relational queries .ppt
Relational Algebra and relational queries .pptRelational Algebra and relational queries .ppt
Relational Algebra and relational queries .ppt
 
Relational Model
Relational ModelRelational Model
Relational Model
 
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybbjhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
 
ch2.ppt
ch2.pptch2.ppt
ch2.ppt
 
ch3[1].ppt
ch3[1].pptch3[1].ppt
ch3[1].ppt
 
Extended relational algebra
Extended relational algebraExtended relational algebra
Extended relational algebra
 
Ch3
Ch3Ch3
Ch3
 
DBMS_INTRODUCTION OF SQL
DBMS_INTRODUCTION OF SQLDBMS_INTRODUCTION OF SQL
DBMS_INTRODUCTION OF SQL
 
Ch 2.pdf
Ch 2.pdfCh 2.pdf
Ch 2.pdf
 
Lecture_W9_Normalization.ppt
Lecture_W9_Normalization.pptLecture_W9_Normalization.ppt
Lecture_W9_Normalization.ppt
 
Relational database concept
Relational database conceptRelational database concept
Relational database concept
 
Ch4
Ch4Ch4
Ch4
 
Intro to Relational Model
Intro to Relational ModelIntro to Relational Model
Intro to Relational Model
 

Plus de RUpaliLohar (20)

Lecture 08 mapping-converted
Lecture 08 mapping-convertedLecture 08 mapping-converted
Lecture 08 mapping-converted
 
Relational model
Relational modelRelational model
Relational model
 
Mobile ip
Mobile ipMobile ip
Mobile ip
 
Mac
MacMac
Mac
 
gprs
gprsgprs
gprs
 
Dbms keys
Dbms keysDbms keys
Dbms keys
 
Relational model
Relational modelRelational model
Relational model
 
EER MODEL
EER MODELEER MODEL
EER MODEL
 
Gsm security algorithms A3 , A5 , A8
Gsm security algorithms A3 , A5 , A8Gsm security algorithms A3 , A5 , A8
Gsm security algorithms A3 , A5 , A8
 
Localization &amp; calling
Localization  &amp; callingLocalization  &amp; calling
Localization &amp; calling
 
Gsm signaling protocol
Gsm signaling protocolGsm signaling protocol
Gsm signaling protocol
 
Mapping cardinality (cardinality constraint) in ER MODEL
Mapping cardinality (cardinality constraint) in ER MODELMapping cardinality (cardinality constraint) in ER MODEL
Mapping cardinality (cardinality constraint) in ER MODEL
 
GSM Radio interface
GSM Radio interfaceGSM Radio interface
GSM Radio interface
 
Gsm services
Gsm servicesGsm services
Gsm services
 
Electromagnetic wave
Electromagnetic waveElectromagnetic wave
Electromagnetic wave
 
Gsm architecture
Gsm architectureGsm architecture
Gsm architecture
 
Entity relationship model
Entity relationship modelEntity relationship model
Entity relationship model
 
Spread spectrum
Spread spectrumSpread spectrum
Spread spectrum
 
Data model
Data modelData model
Data model
 
DBMS architecture &; system structure
DBMS architecture &; system  structureDBMS architecture &; system  structure
DBMS architecture &; system structure
 

Dernier

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 

Dernier (20)

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 

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
  • 2. ©Silberschatz, Korth and Sudarshan3.2Database System Concepts Example of a Relation
  • 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.
  • 7. ©Silberschatz, Korth and Sudarshan3.7Database System Concepts
  • 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
  • 29. ©Silberschatz, Korth and Sudarshan3.29Database System Concepts
  • 30. ©Silberschatz, Korth and Sudarshan3.30Database System Concepts Join Operations
  • 31. ©Silberschatz, Korth and Sudarshan3.31Database System Concepts
  • 32. ©Silberschatz, Korth and Sudarshan3.32Database System Concepts
  • 33. ©Silberschatz, Korth and Sudarshan3.33Database System Concepts
  • 34. ©Silberschatz, Korth and Sudarshan3.34Database System Concepts
  • 35. ©Silberschatz, Korth and Sudarshan3.35Database System Concepts
  • 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.
  • 37. ©Silberschatz, Korth and Sudarshan3.37Database System Concepts
  • 38. ©Silberschatz, Korth and Sudarshan3.38Database System Concepts
  • 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
  • 40. ©Silberschatz, Korth and Sudarshan3.40Database System Concepts
  • 41. ©Silberschatz, Korth and Sudarshan3.41Database System Concepts
  • 42. ©Silberschatz, Korth and Sudarshan3.42Database System Concepts Right Outer Join:
  • 43. ©Silberschatz, Korth and Sudarshan3.43Database System Concepts
  • 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
  • 47. ©Silberschatz, Korth and Sudarshan3.47Database System Concepts
  • 48. ©Silberschatz, Korth and Sudarshan3.48Database System Concepts
  • 49. ©Silberschatz, Korth and Sudarshan3.49Database System Concepts
  • 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 (snameInterlake(Boats)) List ids of boats reserved on 10/10/12 bid (sday10/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).
  • 66. ©Silberschatz, Korth and Sudarshan3.66Database System Concepts Result of s branch-name = “Perryridge” (loan)
  • 67. ©Silberschatz, Korth and Sudarshan3.67Database System Concepts Loan Number and the Amount of the Loan
  • 68. ©Silberschatz, Korth and Sudarshan3.68Database System Concepts Names of All Customers Who Have Either a Loan or an Account
  • 69. ©Silberschatz, Korth and Sudarshan3.69Database System Concepts Customers With An Account But No Loan
  • 70. ©Silberschatz, Korth and Sudarshan3.70Database System Concepts Result of borrower  loan
  • 71. ©Silberschatz, Korth and Sudarshan3.71Database System Concepts Result of s branch-name = “Perryridge” (borrower  loan)
  • 72. ©Silberschatz, Korth and Sudarshan3.72Database System Concepts Result of Pcustomer-name
  • 73. ©Silberschatz, Korth and Sudarshan3.73Database System Concepts Result of the Subexpression
  • 74. ©Silberschatz, Korth and Sudarshan3.74Database System Concepts Largest Account Balance in the Bank
  • 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)
  • 81. ©Silberschatz, Korth and Sudarshan3.81Database System Concepts The credit-info Relation
  • 82. ©Silberschatz, Korth and Sudarshan3.82Database System Concepts Result of Pcustomer-name, (limit – credit-balance) as credit-available(credit-info).
  • 83. ©Silberschatz, Korth and Sudarshan3.83Database System Concepts The pt-works Relation
  • 84. ©Silberschatz, Korth and Sudarshan3.84Database System Concepts The pt-works Relation After Grouping
  • 85. ©Silberschatz, Korth and Sudarshan3.85Database System Concepts Result of branch-name  sum(salary) (pt-works)
  • 86. ©Silberschatz, Korth and Sudarshan3.86Database System Concepts Result of branch-name  sum salary, max(salary) as max-salary (pt-works)
  • 87. ©Silberschatz, Korth and Sudarshan3.87Database System Concepts The employee and ft-works Relations
  • 88. ©Silberschatz, Korth and Sudarshan3.88Database System Concepts The Result of employee ft-works
  • 89. ©Silberschatz, Korth and Sudarshan3.89Database System Concepts The Result of employee ft-works
  • 90. ©Silberschatz, Korth and Sudarshan3.90Database System Concepts Result of employee ft-works
  • 91. ©Silberschatz, Korth and Sudarshan3.91Database System Concepts Result of employee ft-works
  • 92. ©Silberschatz, Korth and Sudarshan3.92Database System Concepts Tuples Inserted Into loan and borrower
  • 93. ©Silberschatz, Korth and Sudarshan3.93Database System Concepts Names of All Customers Who Have a Loan at the Perryridge Branch
  • 94. ©Silberschatz, Korth and Sudarshan3.94Database System Concepts E-R Diagram
  • 95. ©Silberschatz, Korth and Sudarshan3.95Database System Concepts The branch Relation
  • 96. ©Silberschatz, Korth and Sudarshan3.96Database System Concepts The loan Relation
  • 97. ©Silberschatz, Korth and Sudarshan3.97Database System Concepts The borrower Relation