Professional Documents
Culture Documents
Lec 5 Relational Model
Lec 5 Relational Model
_____________________________________________________________________________________
Concepts
Tables: In relation data model, relations are saved in the format of Tables. This
format stores the relation among entities. A table has rows and columns, where
rows represent records and columns represents the attributes.
Ex: 1. Customer relation
2. Branch relation
3. Account relation
4. Loan relation
Tuple: A single row of a table, which contains a single record for that relation, is
called a tuple.
Relation schema: This describes the relation name (table name), attributes and
their names.
Relation key: Each row has one or more attributes which can identify the row in
the relation (table) uniquely, is called the relation key.
Ex: 1. Barrower relation
2. Depositor relation
Attribute domain: Every attribute has some pre-defined value scope, known as
attribute domain.
Constraints
Every relation has some conditions that must hold for it to be a valid relation.
These conditions are called Relational Integrity Constraints. There are three main
integrity constraints.
1. Key Constraints
2. Domain constraints
3. Referential integrity constraints
2. Domain constraints
Attributes have specific values in real-world scenario. For example, age can
only be positive integer. The same constraints has been tried to employ on
the attributes of a relation. Every attribute is bound to have a specific range
of values. For example, age cannot be less than zero and telephone number
cannot be an outside 0-9.
A database schema, along with primary key and foreign key dependencies, can be
depicted pictorially by schema diagrams.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output. It uses operators to
perform queries. An operator can be either unary or binary. They accept relations
as their input and yield relations as their output. Relational algebra is performed
recursively on a relation and intermediate results are also considered relations.
2. Find all tuples in which the amount lent is more than $1200
σ amount>1200 (loan)
3. Find the tuples pertaining to loans of more than $1200 made by the Perryridge
branch
σ branch-name =“Perryridge”∧ amount>1200 (loan)
Ex 2: Find the names of all bank customers who have either an account or a loan or
both.
1. First find the names of all customers with a loan in the bank as
Π customer-name (borrower)
2. Now find the names of all customers with an account in the bank as
Π customer-name (depositor)
3. Now the union of these two sets will give the names of all bank customers
who have either an account or a loan or both.
Π customer-name (borrower ) ∪ Π customer-name (depositor)
The result is
Ex 2: Find all customers of the bank who have an account but not a loan
Π customer-name (depositor) – Π customer-name (borrower )
The result is
Ex 2: Find the names of all customers who have a loan at the Perryridge branch.
σ branch-name =“Perryridge”(borrower × loan)
The result is
Additional operations
1. Set intersection Operation
2. Natural join Operation
3. Division Operation
4. Assignment Operation
1 The Set-Intersection Operation (∩): We can find all customers who have both
a loan and an account using set intersection as below.
Π customer-name (borrower) ∩ Π customer-name (depositor)
The result is
Notation: r ∩ s
Note that we can rewrite any relational algebra expression that uses set intersection
by replacing the intersection operation with a pair of set-difference operations as:
r ∩ s = r − (r − s)
Ex 2: Find the names of all customers who have a loan at the bank, along with the
loan number and the loan amount.
Here we first form the Cartesian product of the borrower and loan relations. Then,
we select those tuples that pertain to only the same loan-number, followed by the
projection of the resulting customer-name, loan-number, and amount:
Π customer-name, loan.loan-number, amount (σ borrower .loan-number =loan.loan-number (borrower × loan))
Ex 3: Find the names of all customers who have a loan at the bank, and find the
amount of the loan.
Π customer-name, loan-number, amount (borrower loan)
The result is
Ex 4: Find the names of all branches with customers who have an account in the
bank and who live in Harrison.
Π branch-name (σ customer-city =“Harrison” (customer account depositor))
The result is
Ex 2: Suppose that we wish to find all customers who have an account at all the
branches located in Brooklyn.
r1 = Π branch-name (σ branch-city =“Brooklyn” (branch))
The result is
Ex 3: Find all (customer-name, branch-name) pairs for which the customer has an
account at a branch
r2 = Π customer-name, branch-name (depositor account)
The result is
Now, we need to find customers who appear in r2 with every branch name in r1.
The operation that provides exactly those customers is the divide operation. We
formulate the query by writing
Π customer-name, branch-name (depositor account) ÷ Π branch-name (σ branch-city =“Brooklyn”
(branch))
The result of this expression is a relation that has the schema (customer-name) and
that contains the tuple (Johnson).
r ÷ s = Π R−S ((Π R−S (r) × s) – Π R−S,S(r))
4. The Assignment Operation: The assignment operation, denoted by ←, works
like assignment in a programming language.
To illustrate this operation, write r ÷ s as
temp1 ← ΠR−S (r)
temp2 ← ΠR−S ((temp1 × s) − ΠR−S,S(r))
result = temp1 − temp2