0% found this document useful (0 votes)
84 views39 pages

Chapter 2: Intro To Relational Model

The document provides an introduction to the relational model used in database management systems. It defines key concepts like relations, attributes, tuples, domains, keys, and relationships between relations. It also describes the basic operations of the relational algebra used to query and manipulate relations, including selection, projection, union, difference, Cartesian product, and rename. Finally, it provides examples of how to express queries over schemata for a banking database using these relational operations.

Uploaded by

Arey Entra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views39 pages

Chapter 2: Intro To Relational Model

The document provides an introduction to the relational model used in database management systems. It defines key concepts like relations, attributes, tuples, domains, keys, and relationships between relations. It also describes the basic operations of the relational algebra used to query and manipulate relations, including selection, projection, union, difference, Cartesian product, and rename. Finally, it provides examples of how to express queries over schemata for a banking database using these relational operations.

Uploaded by

Arey Entra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Chapter 2: Intro to Relational Model

Example of a Relation

attributes
(or columns)

tuples
(or rows)
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.

 Domain is said to be atomic if all its members are 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 in our main presentation
and consider their effect later
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
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
 instructor
 student

 advisor
 Bad design:
univ (instructor -ID, name, dept_name, salary, student_Id, ..)
results in

 repetition of information (e.g., two students have the same instructor)


 the need for null values (e.g., represent an student with no advisor)
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?
 Should choose an attribute whose value never, or very rarely,
changes.
Foreign Keys
 A relation schema may have an attribute that corresponds to the primary
key of another relation. The attribute is called a foreign key.

 E.g. dept_name attributes of instructor is a foreign key to


Department relation

 Only values occurring in the primary key attribute of the referenced


relation may occur in the foreign key attribute of the referencing
relation.
Schema Diagram for University Database
Relational Query Languages
 Language in which user requests information from the database.

 Categories of languages
 Procedural
 The user instructs the system to perform a sequence of operations on the database
to compute the desired result

 Non-procedural, or declarative
 The user describes the desired information without giving a specific procedure for
obtaining that information

 “Pure” query languages:


 Relational algebra (procedural)
 Tuple relational calculus ( non procedural)
 Domain relational calculus (non procedural)
 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: x
 rename: 

 The operators take one or two relations as inputs and produce a new
relation as a result.
Selection of tuples
 Relation r

 Select tuples with A=B


and D > 5
 σ A=B and D > 5 (r)
Select Operation
 Notation:  p(r)

 p is called the selection predicate


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:

 Salary>85000(Instructor)
Selection of Columns (Attributes)

 Relation r:

 Select A and C
Projection
 Π A, C (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

 Example:
 ID,Salary (Instructor)
Union of two relations
 Relations r, s:

 r  s:
Union Operation
 Notation: r  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
(example: 2n d column of r deals with the same type of values as
does the 2nd column of s)

 Example: To find the names of all the Instructors and the


students
name (Instructor)  name (Student)
Set difference of two relations
 Relations r, s:

 r – s:
Set Difference Operation

 Notation r–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
Joining two relations – Cartesian Product
 Relations r, s:

 r x 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.
Composition of Operations
 Can build expressions using multiple operations

 Example: A=C(r x s)
 rxs 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

 A=C(r x s)
A B C D E

 1  10 a
 2  10 a
 2  20 b
Rename Operation
 Allows us to name, and therefore to refer to, the results of relational-
algebra expressions.
 Allows us to refer to a relation by more than one name.
 Example:

 x (E)
returns the expression E under the name X

 If a relational-algebra expression E has arity n, then

x( A 1 , A2 ,...,An ) (E )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Relational Algebra operations
Banking Example

 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)


Example Queries
 Find all loans 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 names of all customers who have a loan, an account, or both,
from the bank

customer_name (borrower)  customer_name (depositor)


Example Queries
 Find the names of all customers who have a loan at the Perryridge branch.

 Query 1

customer_name (branch_name = “Perryridge” (


borrower.loan_number = loan.loan_number (borrower x loan)))

 Query 2

customer_name(loan.loan_number = borrower.loan_number
(
(branch_name = “Perryridge” (loan)) x borrower )
)
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.

customer_name (branch_name = “Perryridge”

(borrower.loan_number = loan.loan_number(borrower x loan))

)

customer_name(depositor)
Example Queries
 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.
 The query is:

balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
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
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 of two relations

 Relation r, s:

 rs
Natural Join Example
 Relations r, s:

 Natural Join

r s
Bank Example Queries
 Find the names of all customers who have a loan and an account at
bank.

customer_name (borrower)  customer_name (depositor)

 Find the name of all customers who have a loan at the bank and the
loan amount

customer_name, loan_number, amount (borrower loan)


Outer Join
 An extension of the join operation that avoids loss of information.

 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.

 Uses null values:


 null signifies that the value is unknown or does not exist
 All comparisons involving null are (roughly speaking) false by
definition.
Outer Join – Example
 Relation loan

loan_number branch_name amount


L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

 Relation borrower

customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example
 Join

loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

 Left Outer Join


loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
Outer Join – Example
 Right Outer Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes
 Full Outer Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes
End of Chapter 2

You might also like