You are on page 1of 34

II nd Module-Relational Model

Relational Model
• A relational database consists of a collection
of tables,with a unique name.
• A row in a table represents a relationship
among a set of values.
• A row in a table is called a tuple.
• For each attribute,there is a set of permitted
values called as domain
Example of a Relation-account
Basic Structure of Relational Model
• Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
• Example
• customer_name = {Jones, Smith, Curry, Lindsay, …}
• /* Set of all customer names */
– customer_street = {Main, North, Park, …} /* set of all street names*/
– customer_city = {Harrison, Rye, Pittsfield, …} /* set of all city names */
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
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. the value of an attribute can be an account number,
but cannot be a set of account numbers
• Domain is said to be atomic if all its members are
atomic
• The special value null is a member of every domain
Relation Schema
• The overall design of the relation is termed as Relation
schema.

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


An are attributes

Example:
Customer_schema = (customer_name,
customer_street, customer_city)
Relation Instance
• The current values (relation instance) of a relation
are specified in a relation instance.
• A tuple, represented by a row in a table attributes
(or columns)
customer_name customer_street customer_city

Jones Main Harrison


Smith North Rye tuples
Curry North Rye (or rows)
Lindsay Park Pittsfield
Order of tuples is irrelevant
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
account : stores information about accounts
depositor : stores information about which
customer owns which account
customer : stores information about customers
The customer Relation
Keys
• Primary key
• Super Key
• Cadidate Key
• Foreign Key
Schema Diagram
• A database schema ,along with primary key and
foreign key dependencies can be depicted pictorically
by schema Diagram.
• Each relation appears as a box,with the attributes
listed above it and the relation name above it.If there
are primary key attributes,a horizontal line crosses the
box,with primary key attributes listed above the line.
• Foreign key dependencies appear as arrows from the
foreign key attributes of the refererencing relation to
the primary key of the referenced relation.
Schema diagram
Query Languages
• Language in which user requests information from the
database.
• Categories of languages
– Procedural
– Non-procedural, or declarative
– In procedural langauge,user describes the information
by specifying a procedure for obtaining that information.
– In non procedural langauge,user describes the
information without specifying a procedure for
obtaining that information
Relational Algebra
• Relational Algebra is a Procedural language.It consists of a set of operations that
take one or more relations as input and produce a new relation as output.
• 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.
Select Operation
• The select operation selects tuples that satisfy
a given predicate.
• Notation:  p(r)
The lower case letter  is used to denote
selection.
The predicate appears as a subscript to  .
The argument relation is in paranthesis after
the 
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
• Relation r
A B C D A B C D

  1 7   1 7
 A=B ^ D > 5 (r)
  5 7   23 10
  12 3
  23 10

Example of selection:

 branch_name=“Perryridge”(account)
Project operation
• The project operation is a unary operation that returns its argument
relation,by certain attributes left out.
• Uppercase letter  is used to denote project operation.
• Notation: A1,A2....An(r)
• where A1, A2 are attribute names and r is a relation name.

• Duplicate rows removed from result


• .
• Example: To eliminate the branch_name attribute of account

account_number, balance (account)


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: 2nd
column
of r deals with the same type of values as does the 2nd
column of s)
• Example: to find all customers with either an account or a loan
customer_name (depositor)  customer_name (borrower)
Set Difference Operation
• The set difference operation,denoted by -,allows to
find tuples that are in one relation,but not in another.
• 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
• Example: to find all customers who has an
account but not loan
• customer_name (depositor) - customer_name
(borrower)
Cartesian-Product Operation
• This operation denoted by x,allows us to
combine information from any two
relations.Notation r x s
• Cartesian-Product Operation – Example
A B C D E
Relations r, s
 1  10 a
 2  10 a
 20 b
r  10 b

 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
Rename Operation
• used to name, and therefore to refer to, the results of relational-algebra expressions.
• It is denoted by the lowercase Greek letter rho(  )
• Example:
 x (E)

returns the expression E under the name X


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

 x(A,A1...An) (E)

returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
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
• It is used to find tuples that are present
commonly in both tables.
• Notation: r  s
• Assume:
– r, s have the same arity
– attributes of r and s are compatible

• Example: to find all customers who has both an


account and a loan.
customer_name (depositor)  customer_name
(borrower)
Natural join
• The associated tables have one or more pairs
of identically named columns.
- The columns must be the same data type.
• The SQL NATURAL JOIN is structured in such a
way that, columns with the same name of
associated tables will appear once only.
marks
Student

SELECT * FROM Student NATURAL JOIN Marks;


Output:
Division Operation
• Notation:
• Suited to queries that include the phrase “for
all”.
• Let r and s be relations on schemas R and S
respectively
Division Operation – Example
• Relations r, s:
A B B

 1 1
 2 2
 3
 1 s
 1
 1
 3  r  s: A
 4
 6 
 1
 2 

r
• We can find all (customer name,branch
name)pairs for which the customer has an
account by
r1=customer_name, branch_name (depositor ⋈
account)
• We can obtain all branches in Brooklyn by
r2=branch_name (branch_city = “Brooklyn” (branch))
• Find all customers who have an account at all
branches located in Brooklyn city.
• r1 r2=
customer_name, branch_name (depositor ⋈
account)
 branch_name (branch_city = “Brooklyn” (branch))
Assignment Operation
• The assignment operation () provides a
convenient way to express complex queries.
– The result to the right of the  is assigned
to the relation variable on the left of the .
– May use variable in subsequent expressions.
– Example:
temp1  R-S (r )

You might also like