Relational Model
Relational Model
The relational model uses a collection of tables to represent both data &
the relationships among those data.
Each table has multiple columns, and column has a unique name. Each
attribute of a relation has a name.
The set of allowed values for each attribute is called the domain of the
attribute.
The special value null is a member of every domain. The null value causes
complications in the definition of many operations
Relational Model…contd.
Database schema: The database schema is a logical design of database
customer
Relational Model…contd.
Attribute Types
Each attribute of a relation has a name
The set of allowed values for each attribute is called the domain of the
attribute
- repetition of information
• Example:{customer_name,customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can possibly have the
same name
Banking Enterprise
Relational Model…contd.
K is a candidate key if K is minimal
Example: {customer_name} is a candidate key for Customer, since it is a superkey and no
subset of it is a superkey.
Primary key: a candidate key chosen as the principal means of identifying tuples within
a relation
• Should choose an attribute whose value never, or very rarely, changes.
• E.g. email address is unique, but may change
Foreign Key: A relation schema may have an attribute that corresponds to the
primary key of another relation. The attribute is called a foreign key.
- Only values occurring in the primary key attribute of the referenced relation may
occur in the foreign key attribute of the referencing relation.
Relational Model…contd.
Query Languages
A query language is a language in which a user requests information
from the database.
The operators take one or two relations as inputs and produce a new relation as a
result.
Select Operation
A B C D
Relation r
1 7
5 7
12 3
23 10
1 7
23 10
Project Operation
A B C
Relation r:
10 1
20 1
30 1
40 2
A,C (r) A C A C
1 1
1 = 1
1 2
2
Union Operation
Relations r, s: A B A B
1 2
2 3
1 s
r
A B
r s: 1
2
1
3
Set Difference Operation
Relations r, s: A B A B
1 2
2 3
1 s
r
r – s: A B
1
1
Cartesian-Product Operation
Relations r, s: A B C D E
1 10 a
10 a
2
20 b
r 10 b
s
r x s: A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
Rename Operation
x ( A ,A ,...,A ) (E )
1 2 n
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Relational Algebra Queries:
Find the loan number for each loan of an amount greater than Rs1200
Find the names of all customers who have a loan, an account, or both, from the bank
Relational Algebra Queries:
Find the loan number for each loan of an amount greater than Rs1200
loan_number (amount > 1200 (loan))
Find the names of all customers who have a loan, an account, or both, from the bank
customer_name (borrower) customer_name (depositor)
Project Operation
Find the names of all customers who have a loan at the Perryridge branch.
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.
Project Operation
Find the names of all customers who have a loan at the Perryridge branch.
customer_name (branch_name=“Perryridge” (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
have an account at any branch of the bank.
customer_name (branch_name = “Perryridge” (borrower.loan_number = loan.loan_number(borrower x
loan))) – customer_name(depositor)
Project Operation
Find the names of all customers who have a loan at the Perryridge branch
Project Operation
Find the names of all customers who have a loan at the Perryridge branch
Additional Operations
A B A B
Relation r, s: 1 2
2 3
1
r s
A B
2
• rs
Natural Join Operation
• Relations r, s:
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s
A B C D E
1 a
1 a
1 a
1 a
2 b
Natural-Join Operation
Notation: r s
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))
Natural Join
Find the name of all customers who have a loan at the bank and the
loan amount
Natural Join
• Find the name of all customers who have a loan at the bank and the loan
amount
• Find all customers who have an account from at least the “Downtown” and the
Uptown” branches.
Project Operation
• Find all customers who have an account from at least the “Downtown” and the
Uptown” branches.
customer_name (branch_name = “Downtown” (depositor account ))
customer_name (branch_name = “Uptown” (depositor account))
Project Operation
• Find the largest account balance.
Project Operation
• 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.
balance(account) - account.balance (account.balance < d.balance (account x d (account)))
Aggregate Functions and Operations
Aggregate functions that summarize data from tables
• Relation r:
A B C
7
7
3
10
27
Aggregate Operation
• Relation account grouped by branch-name:
• Computes the join and then adds tuples form one relation that does not match tuples in
the other relation to the result of the join.
Loan Borrower
Example: loan_number branch_name amount customer_name loan_number
The result of any arithmetic expression involving null is null.. Aggregate functions
simply ignore null values (as in SQL)
For duplicate elimination and grouping, null is treated like any other value, and two
nulls are assumed to be the same (as in SQL) Comparisons with null values return the
special truth value: unknown
Notation: r s
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
Division Operation
Relations r, s:
A B B
1 1
2
3 2
1 s
1
1
3
4
6
1
2
r s: A r
Division Operation
Relations r, s:
A B C D E D E
a a 1 a 1
a a 1 b 1
a b 1 s
a a 1
a b 3
a a 1
a b 1
a b 1
r
r s:
A B C
a
a
Division Operation
• Property
– Let q = r s
– Then q is the largest relation satisfying q x s r
• Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S R
To see why
– R-S,S (r) simply reorders attributes of r
Brooklyn city .
Natural Join and Division
• Find all customers who have an account at all branches located in Brooklyn
city.