Relational Model Notes
Relational Model
Module 3 focuses on the relational model, covering its structure, algebra, calculus, extended operations, modifications, and views.
Topics Covered
Structure of Relational Databases
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Extended Relational-Algebra-Operations
Modification of the Database
Views
Example of a Relation
Illustrative table with attributes account-number, branch-name, and balance.
account-number | branch-name | balance |
|---|---|---|
A-101 | Downtown | 500 |
A-102 | Perryridge | 400 |
A-201 | Brighton | 900 |
A-215 | Mianus | 700 |
A-217 | Brighton | 750 |
A-222 | Redwood | 700 |
A-305 | Round Hill | 350 |
Why Relations?
Simple model.
Good match for how we think about data.
Abstract model underlying SQL, the most important language in DBMS.
SQL uses "bags" while the abstract relational model is set-oriented.
Relational Design
Simplest approach: convert each Entity Set (E.S.) to a relation and each relationship to a relation.
Entity Set → Relation
E.S. attributes become relational attributes.
Beers(name, manf)
Relational Model
Table = relation.
Column headers = attributes.
Row = tuple (Beers).
Relation schema = name(attributes) + other structure info., e.g., keys, other constraints.
Example: Beers(name, manf)
Order of attributes is arbitrary, but in practice, we need to assume the order given in the relation schema.
Relation instance: current set of rows for a relation schema.
Database schema = collection of relation schemas.
Constraints
Primary Key Constraint (P.Key Constraint)
Referential Integrity Constraint (Ref integ Cons)
Unique Constraint (Uniq Cons)
Null Constraint (Null Cons)
Domain Constraint (Domain Cons)
Check Constraint (Check Cons)
Default Constraint (DefaultCons)
Basic Structure
Formally, given sets , a relation is a subset of . Thus a relation is a set of n-tuples where each .
Example:
customer-name = {Jones, Smith, Curry, Lindsay}
customer-street = {Main, North, Park}
customer-city = {Harrison, Rye, Pittsfield}
Then r = {(Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} is a relation over customer-name x customer-street x customer-city.
Relational Data Model
Set theoretic.
Domain: set of values
Cartesian product (or product)
n-tuples s.t.,
Relation = subset of Cartesian product of one or more domains
FINITE only; empty set allowed
Tuples = members of a relation inst.
Arity = dimension of domains
Components = values in a tuple
Domains correspond to values in attributes
Cardinality = number of tuples
Relation as table
Rows = tuples
Columns = components
Names of columns = attributes
Set of attribute names = schema REL()
Arity, Cardinality, Attributes, Component, Tuple
Relation: Example
Domain of Relation: N (Name), A (Address), T (Telephone)
Cardinality of domain: 5 (Names) x 3 (Address) x 7 (Telephone) = 105
Relation:
N | A | T |
|---|---|---|
N1 | A1 | T1 |
N1 | A1 | T2 |
N1 | A1 | T3 |
… | … | … |
N1 | A1 | T7 |
N1 | A2 | T1 |
N1 | A3 | T1 |
N2 | A1 | T1 |
Arity = 3
Cardinality <= 5 x 3 x 7 of relation
Tuple, Domain, Component, Attribute.
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.
Attribute values are (normally) required to be atomic, that is, indivisible.
E.g., multivalued attribute values are not atomic.
E.g., composite attribute values are not atomic.
The special value null is a member of every domain.
The null value causes complications in the definition of many operations.
We shall ignore the effect of null values for now and consider their effect later.
Relation Schema
are attributes
R = ( ) is a relation schema
E.g. Customer-schema = (customer-name, customer-street, customer-city)
r(R) is a relation on the relation schema R
E.g. customer (Customer-schema)
Relation Instance
The current values (relation instance) of a relation specified by a table.
An element t of r is a tuple, represented by a row in a table.
Relation Instance Example
Name | Address | Telephone |
|---|---|---|
Bob | 123 Main St | 555-1234 |
Bob | 128 Main St | 555-1235 |
Pat | 123 Main St | 555-1235 |
Harry | 456 Main St | 555-2221 |
Sally | 456 Main St | 555-2221 |
Sally | 456 Main St | 555-2223 |
Pat | 12 State St | 555-1235 |
Relations are Unordered
Order of tuples is irrelevant (tuples may be stored in an arbitrary order).
E.g., account relation with unordered tuples.
Database
A database consists of multiple relations.
Information about an enterprise is broken up into parts, with each relation storing one part of the information.
E.g.:
account: stores information about accounts
depositor: stores information about which customer owns which account
customer: stores information about customers
Storing all information as a single relation such as bank(account-number, balance, customer-name, ..) results in
repetition of information (e.g., two customers own an account)
the need for null values (e.g., represent a customer without an account)
Normalization theory (Chapter) deals with how to design relational schemas.
E-R Diagram for the Banking Enterprise
Diagram illustrating relationships between entities such as customer, account, branch, loan, and depositor.
The Customer Relation
Table showing customer data with attributes:
customer-name
customer-street
customer-city
The Depositor Relation
Table associating customers with accounts, containing:
customer-name
account-number
Keys
Let K ⊆ R
K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R)
by “possible r” we mean a relation r that could exist in the enterprise we are modeling.
Example: {customer-name, customer-street} and {customer-name} are both superkeys of Customer if no two customers can possibly have the same name.
K is a candidate key if K is minimal
Example: {customer-name} is a candidate key for Customer since it is a superkey (assuming no two customers can possibly have the same name), and no subset of it is a superkey.
Determining Keys from E-R Sets
Strong entity set: The primary key of the entity set becomes the primary key of the relation.
Weak entity set: The primary key of the relation consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set.
Relationship set: The union of the primary keys of the related entity sets becomes a super key of the relation.
For binary many-to-one relationship sets, the primary key of the “many” entity set becomes the relation’s primary key.
For one-to-one relationship sets, the relation’s primary key can be that of either entity set.
For many-to-many relationship sets, the union of the primary keys becomes the relation’s primary key.
Schema Diagram for the Banking Enterprise
Diagram visually representing the database schema including relations for branch, account, depositor, loan, customer, and borrower, along with their respective attributes.
Query Languages
Language in which user requests information from the database.
Categories of languages
procedural
non-procedural
"Pure" languages:
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Pure languages form underlying basis of query languages that people use.
Relational Algebra
Procedural language
Six basic operators:
select
project
union
set difference
Cartesian product
rename
The operators take two or more relations as inputs and give a new relation as a result.
Select Operation – Example
Relation r:
A | B | C | D |
|---|---|---|---|
α | α | 1 | 5 |
α | β | 12 | 23 |
β | α | 7 | 7 |
α | β | 3 | 10 |
β | β | 3 | 10 |
A | B | C | D |
|---|---|---|---|
α | α | 1 | 5 |
α | β | 12 | 23 |
α | β | 3 | 10 |
α | β | 3 | 10 |
Select Operation
Notation:
p is called the selection predicate
Defined as: Where p is a formula in propositional calculus consisting of terms connected by: ∧ (and), ∨ (or), ¬ (not) Each term is one of: op or where op is one of: =, ≠, >, ≥, <, ≤
Example of selection:
Project Operation - Example
Relation r:
A | B | C |
|---|---|---|
α | 10 | 1 |
α | 20 | 1 |
β | 30 | 1 |
β | 40 | 2 |
A | C |
|---|---|
α | 1 |
β | 1 |
α | 1 |
β | 2 |
Project Operation
Notation: where are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by erasing the columns that are not listed.
Duplicate rows removed from result, since relations are sets.
E.g., To eliminate the branch-name attribute of account
Banking Example
Relations:
branch (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)
Tables for Banking Example
Snapshot of tables customer, account, and depositor with sample data.
Example Queries
Write the Relational Algebraic expression for the given queries:
Find all loans of over $1200.
Find the loan number for each loan of an amount greater than $1200.
Find the names of all customers who have a loan at the Perryridge branch.
Solution Queries
Find all loans of over $1200.
Find the loan number for each loan of an amount greater than $1200.
SQL equivalent: Select Loan_number From Loan Where amount>1200
Solution Queries
Find the names of all customers who have a loan at the Perryridge branch.
Query 1:
* Query 2:
Union Operation – Example
Relations r, s:
A | B |
|---|---|
α | 1 |
α | 2 |
β | 1 |
r
A | B |
|---|---|
α | 1 |
β | 3 |
s
r ∪ s:
A | B |
|---|---|
α | 1 |
α | 2 |
β | 1 |
β | 3 |
Union Operation
Notation: r ∪ s
Defined as: r ∪ s = {t | t ∈ r or t ∈ s}
For r ∪ s to be valid:
r, s must have the same arity (same number of attributes)
The attribute domains must be compatible (e.g., 2nd column of r deals with the same type of values as does the 2nd column of s)
E.g., to find all customers with either an account or a loan
Union Operation
SQL example:
select CID from depositor union select CID from borrowerWill Union and Union all give the same result?
Set Difference Operation - Example
Relations r, s:
A | B |
|---|---|
α | 1 |
α | 2 |
β | 1 |
r
A | B |
|---|---|
α | 2 |
β | 3 |
s
r - s:
A | B |
|---|---|
α | 1 |
β | 1 |
Set Difference Operation
Notation r – s
Defined as: r – s = {t | t ∈ r and t ∉ s}
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Difference
SQL Example
Except operation
select distinct CID from depositor except select CID from borrower
* Except all operation
select CID from depositor except all select CID from borrower
Cartesian-Product Operation-Example
Relations r, s:
A | B |
|---|---|
α | 1 |
β | 2 |
r
C | D | E |
|---|---|---|
α | 10 | a |
β | 10 | a |
γ | 20 | b |
s
r x s:
A | B | C | D | E |
|---|---|---|---|---|
α | 1 | α | 10 | a |
α | 1 | β | 10 | a |
α | 1 | γ | 20 | b |
β | 2 | α | 10 | a |
β | 2 | β | 10 | a |
β | 2 | γ | 20 | b |
Cartesian-Product Operation
Notation r x s
Defined as: r x s = {t q | t ∈ r and q ∈ s}
Assume that attributes of r(R) and s(S) are disjoint.
If attributes of r(R) and s(S) are not disjoint, then renaming must be used.
Rename Operation
This is used to rename both relations and attributes.
In relational algebra it is denoted by ρ
Relational Algebra expression
Renaming attributes :
Renaming Relation :
Example Tables
Snapshot of tables customer, depositor, and account with sample data.
Example Queries
Find the names of all customers who have a loan or an account, or both, from the bank.
Find the names of all customers who have a loan and an account at bank.
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.
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.
Example Queries
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.
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 is a predicate on attributes in E1
, S is a list consisting of some of the attributes in E1
, x is the new name for the result of E1
Additional Operations
We define additional operations that do not add any power to the relational algebra, but that simplify common queries.
Set intersection
Natural join
Division
Assignment
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)
Set-Intersection Operation - Example
Relation r, s:
A | B |
|---|---|
α | 1 |
α | 2 |
r |
A | B |
|---|---|
α | 1 |
B | 2 |
s
r ∩ s:
A | B |
|---|---|
α | 1 |
Intersection
Includes all tuples that are both in R and S.
Denoted as R ∩ S
Intersection
SQL:
intersect
select distinct CID from depositor intersect select CID from borrower
intersect all
select distinct CID from depositor intersect all select CID from borrower
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)
Natural Join Operation – Example
Relations r, s:
A | B | C | D |
|---|---|---|---|
α | 1 | α | a |
β | 2 | γ | a |
γ | 4 | β | b |
α | 1 | α | a |
δ | 2 | γ | b |
r |
B | D | E |
|---|---|---|
1 | a | α |
3 | a | β |
1 | b | γ |
2 | a | δ |
3 | b | ∈ |
s |
r ⋈ s
A | B | C | D | E |
|---|---|---|---|---|
α | 1 | α | a | α |
α | 1 | α | a | γ |
α | 1 | α | a | α |
α | 1 | α | a | γ |
δ | 2 | γ | a | δ |
Division Operation
Suited to queries that include the phrase “for all”.
Let r and s be relations on schemas R and S respectively where
* R = (, …, , , …, )
* S = (, …, )
The result of r ÷ s is a relation on schema R – S = (, …, )
r ÷ s = { t | t ∈ ∧ ∀ u ∈ s ( tu ∈ r ) }
r ÷ s
Division Operation – Example
Relations r, s:
A | B |
|---|---|
α | 1 |
α | 2 |
α | 1 |
β | 1 |
γ | 3 |
δ | 4 |
δ | 6 |
r
| B |
|---|---|
| 1 |
| 2 |
s
r ÷ s:
| A |
|---|---|
| α |
Another Division Example
Relations r, s:
A | B | C | D | E |
|---|---|---|---|---|
α | a | α | γ | 1 |
α | a | α | γ | 1 |
α | a | α | γ | 1 |
β | a | γ | γ | 1 |
β | a | γ | γ | 1 |
γ | a | γ | β | 3 |
γ | a | γ | a | 1 |
γ | a | γ | b | 1 |
r
C | D | E |
|---|---|---|
α | γ | 1 |
s
r ÷ s:
A | B |
|---|---|
α | a |
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: Write r ÷ s as
temp1 ← ΠR-S (r)
temp2 ← ΠR-S ((temp1 x s) – ΠR-S,S (r))
result = temp1 – temp2
The result to the right of the ← is assigned to the relation variable on the left of the ←.
May use variable in subsequent expressions.
Relational Algebra Defined: Where is it in DBMS?
Diagram illustrating the flow from SQL query to executable code through parsing, optimization, and code generation within a DBMS.
Putting it altogether
Overall summary on Relational Algebra
Operations (Unary): Selection, Projection
Selection:
Picks tuples from the relation
Projection:
Picks columns from the relation
Operations (Set): Union, Set Difference
Union: () U ()
New relation contains all tuples from both relations, duplicate tuples eliminated.
Set Difference: R – S
Produces a relation with tuples that are in R but NOT in S.
Operations (Set): Cartesian Product, Intersect
Cartesian Product: R x S
Produces a relation that is concatenation of every tuple of R with every tuple of S
The Above operations are the 5 fundamental operations of relational algebra.
Intersection: R ∩ S
All tuples that are in both R and S
Operations (Join): Theta Join, Natural Join
Theta Join:
Select all tuples from the Cartesian product of the two relations, matching condition F
When F contains only equality “=“, it is called Equijoin
Natural Join: R ⋈ S
Equijoin with common attributes eliminated
Operations: Division
Division: R ÷ S
Produce a relation consist of the set of tuples from R that matches the combination of every tuple in S
Translation to SQL
FROM clause produces Cartesian product (x) of listed tables
WHERE clause assigns rows to C in sequence and produces table containing only rows satisfying condition ( sort of like )
SELECT clause retains listed columns ( )