0% found this document useful (0 votes)
6 views

Relational Models.

Uploaded by

hoboho1456
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Relational Models.

Uploaded by

hoboho1456
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 2: Intro to Relational Model

Example of a Relation

attributes
(or columns)

tuples
(or rows)
Attribute Types

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
The special value null is a member of every domain. Indicated
that the value is “unknown”
The null value causes complications in the definition of many
operations
Relation Schema and Instance
A1, A2, …, An are attributes

R = (A1, A2, …, An ) is a relation schema


Example:
instructor = (ID, name, dept_name, salary)
Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai  Di

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
Relations are Unordered

Order of tuples is irrelevant (tuples may be stored in an arbitrary order)


Example: instructor relation with unordered tuples
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)
Example: {ID} and {ID,name} are both superkeys of instructor.
Superkey K is a candidate key if K is minimal
Example: {ID} is a candidate key for Instructor
One of the candidate keys is selected to be the primary key.
which one?
Foreign key constraint: Value in one relation must appear in another
Referencing relation
Referenced relation
Example – dept_name in instructor is a foreign key from instructor
referencing department
Schema Diagram for University Database
Relational Query Languages
Procedural vs .non-procedural, or declarative
“Pure” languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
The above 3 pure languages are equivalent in computing power
We will concentrate in this chapter on relational algebra
Not turning-machine equivalent
consists of 6 basic operations
Select Operation

Notation:  p(r)
p is called the selection predicate
Defined as:
p(r) = {t | t  r and p(t)}
Where p is a formula in propositional calculus consisting of
terms connected by :  (and),  (or),  (not)
Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, , >, . <. 
Example of selection:
 branch-name=“Perryridge”(account)
Select Operation – selection of rows (tuples)

Relation r

 A=B ^ D > 5 (r)


Project Operation
Notation:

A1, A2, …, Ak (r)


where A1, A2 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
account-number, balance (account)
Project Operation – selection of columns (Attributes)

Relation r:

A,C (r)
Union Operation
Notation: r  s
Defined as:
r  s = {t | t  r or t  s}

For r  s to be valid.
1. r, s must have the same arity (same number of attributes)
2. 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
customer-name (depositor)  customer-name (borrower)
Union of two relations
Relations r, s:

r  s:
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
Set difference of two relations
Relations r, s:

r – s:
Set intersection of two relations

Relation r, s:

rs

Note: r  s = r – (r – s)
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. (That is,
R  S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming must be
used.
joining two relations -- Cartesian-product
Relations r, s:

r x s:
Cartesian-product – naming issue
Relations r, s: B

r x s: r.B s.B
Renaming a Table
Allows us to refer to a relation, (say E) by more than one name.
 x (E)

returns the expression E under the name X

Relations r

r x  s (r) r.A r.B s.A s.B


α 1 α 1
α 1 β 2
β 2 α 1
β 2 β 2
Composition of Operations
Can build expressions using multiple operations
Example: A=C (r x s)

rxs

A=C (r x s)
Joining two relations – Natural Join

Let r and s be relations on schemas R and S respectively.


Then, the “natural join” of relations R and 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
Natural Join Example
Relations r, s:

Natural Join
r s

 A, r.B, C, r.D, E ( r.B = s.B ˄ r.D = s.D (r x s)))


Notes about Relational Languages
Each Query input is a table (or set of tables)
Each query output is a table.
All data in the output table appears in one of the input tables
Relational Algebra is not Turning complete
Can we compute:
SUM
AVG
MAX
MIN
Summary of Relational Algebra Operators
Symbol (Name) Example of Use
σ
(Selection) σ salary > = 85000 (instructor)
Return rows of the input relation that satisfy the predicate.
Π
(Projection) Π ID, salary (instructor)
Output specified attributes from all rows of the input relation. Remove
duplicate tuples from the output.
x
(Cartesian Product) instructor x department

Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.

(Union) Π name (instructor) ∪ Π name (student)

Output the union of tuples from the two input relations.


-
(Set Difference) Π name (instructor) -- Π name (student)

Output the set difference of tuples from the two input relations.

(Natural Join) instructor ⋈ department

Output pairs of rows from the two input relations that have the same value on
all attributes that have the same name.
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)


Example Queries

Find all loans of over $1200


amount > 1200 (loan)
Find the loan number for each loan of an amount greater than $1200
loan-number (amount > 1200 (loan))
Find the name of the depositor who has an account with a balance
greater than $6000 at the “Uptown” branch
Find the name of each branch located in “Chicago”
Find the name of the borrower who has a loan in branch “Downtown”
Example Queries

Find the names of all customers who have a loan, an account, or both,
from the bank
customer-name (borrower)  customer-name (depositor)
Find the names of all customers who have a loan and an account at
bank.
customer-name (borrower)  customer-name (depositor)
Example Queries
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)
employee (person name, street, city)
works (person name, company name, salary)
company (company name, city)

Consider the relational database above. Give an expression in the


relational algebra to express each of the following queries:
Find the names of all employees who live in city “Miami”.
Find the names of all employees whose salary is greater than $100,000.
Find the names of all employees who live in “Miami” and whose salary is
greater than $100,000.
Find the names of all employees who work for “First Bank Corporation”.
Find the names and cities of residence of all employees who work for
“First Bank Corporation”.
Find the names, street address, and cities of residence of all employees
who work for “First Bank Corporation” and earn more than $10,000.
Queries on University Schema
a. Find the ID and name of each instructor in the physics department
b. Find the id and name of each instructor in a department located in the
building “Watson”
c. Find the set of all courses taught in the Fall semester 2017
Πcourse id (σsemester =“Fall”∧ year=2017 (section))
a. Find the set of all courses taught in the Spring semester 2018
Πcourse id (σsemester =“Spring” ∧ year=2018 (section))
a. Find the set of all courses taught in both the Fall semester 2017 and
Spring semester 2018
Πcourse id (σsemester =“Fall”∧ year=2017 (section)) ∪ Πcourse id
(σsemester =“Spring” ∧ year=2018 (section))
a. Find the set of all courses taught in the Fall semester 2017 but not in
Spring semester 2018
Πcourse id (σsemester =“Fall”∧ year=2017 (section)) − Πcourse id
(σsemester =“Spring” ∧ year=2018 (section))
a. Find the id and name of each student who has taken at least one
course in the “CSE” department
b. Find the ID and name of each student who has taken at least one
course section in the year 2018
c. Find the ID and name of each student who has not taken any course
section in the year 2018
Formal Definition
A basic expression in the relational algebra consists of either one of
the following:
A relation in the database
A constant relation
Let E1 and E2 be relational-algebra expressions; the following are all
relational-algebra expressions:
E1  E2
E1 - E2

E1 x E2

p (E1), P is a predicate on attributes in E1


s(E1), S is a list consisting of some of the attributes in E1
 x (E1), x is the new name for the result of E1

You might also like