0% found this document useful (0 votes)
13 views36 pages

Pca155 3

The document discusses the relational database model and its core concepts. It defines a relational database as one that stores data in a structured format using rows and columns. The relational model uses tables to store data with rows representing records and columns representing attributes. Relational algebra provides operations like selection, projection, union, intersection and more to query the data in relational tables. Key aspects of the relational model include its simplicity, structured data storage, use of relations/tables, and relational algebra operations.

Uploaded by

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

Pca155 3

The document discusses the relational database model and its core concepts. It defines a relational database as one that stores data in a structured format using rows and columns. The relational model uses tables to store data with rows representing records and columns representing attributes. Relational algebra provides operations like selection, projection, union, intersection and more to query the data in relational tables. Key aspects of the relational model include its simplicity, structured data storage, use of relations/tables, and relational algebra operations.

Uploaded by

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

Relational Model

PCA155-3 Madhav Subedi


Relational Database
• A relational database refers to a database that
stores data in a structured format, using rows and
columns.
• Relational data model is the primary data model,
which is used widely around the world for data
storage and processing.
• This model is simple and it has all the properties
and capabilities required to process data with
storage efficiency.

PCA155-3 Madhav Subedi


Relational Database Terminologies
• Table/Relation − In rela(onal data model, data are
saved in the format of Tables/Relations. A table has
rows and columns, where rows represents records
and columns represent the attributes.
• Tuple − A single row of a table, which contains a
single record for that relation is called a tuple.
• Attribute − The columns of the table represent the
attribute of data stored in that table.
• Relation key − Each row has one or more a+ributes,
known as relation key, which can identify the row in
the relation (table) uniquely.
• Attribute domain − Every a+ribute has some pre-
defined value scope, known as attribute domain.
PCA155-3 Madhav Subedi
Characteristics of Relations
• Values are atomic.
• All of the values in a column have the same data
type.
• Each row is unique.
• The sequence of rows is insignificant.
• Each column has a unique name.
• Integrity constraints maintain data consistency
across multiple tables.

PCA155-3 Madhav Subedi


Relational model notation
• Relational notation is a shorthand way to represent
E/R diagrams.
• It is a half-way step between an abstract tool and an
implementation specific tool (SQL create table
commands).
• Relational notation takes the names of each
table and its attributes and ordering them in a
specific order.
– always start with the primary key(s), which are commonly
notated with the underscore, next, all other attributes are
added
• Eg: Student(student_id, name, address, dob)

PCA155-3 Madhav Subedi


The Relational Algebra
• Relational algebra is a 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.

PCA155-3 Madhav Subedi


The Relational Algebra
• The fundamental operations of relational algebra
are as follows −
– Select
– Project
– Union
– Set different
– Cartesian product
– Rename

PCA155-3 Madhav Subedi


Select Operation (σ)…
• It selects tuples that satisfy the given predicate
from a relation.
• Notation − σp(r)
• Where σ stands for selection predicate
and r stands for relation. p is prepositional logic
formula which may use connectors like and,
or, and not. These terms may use relational
operators like − =, ≠, ≥, < , >, ≤.

PCA155-3 Madhav Subedi


Select Operation (σ)
• Examples
– σsubject = "database"(Books)
Selects tuples from books where subject is 'database'.
– σsubject = "database" and price = "450"(Books)
Selects tuples from books where subject is 'database'
and 'price' is 450.
– σsubject = "database" and price = "450" or year > "2010"(Books)
Selects tuples from books where subject is 'database'
and 'price' is 450 or those books published after 2010.

PCA155-3 Madhav Subedi


Project Operation (∏)
• It projects column(s) that satisfy a given
predicate.
• Nota(on − ∏A1, A2, An (r)
– Where A1, A2 , An are attribute names of relation r.
• Duplicate rows are automatically eliminated, as
relation is a set.
• For example
– ∏subject, author (Books) Selects and projects columns
named as subject and author from the relation Books.

PCA155-3 Madhav Subedi


Union operation (υ)
• UNION is symbolized by ∪ symbol.
• It returns all tuples that are in tables A or in B.
• It also eliminates duplicate tuples.
• A UNION set B would be expressed as: A ∪ B
• For a union operation to be valid, the following
conditions must hold -
– R and S must be the same number of attributes.
– Attribute domains need to be compatible.
• This is also defined as union compatibility
PCA155-3 Madhav Subedi
Union operation (υ)

PCA155-3 Madhav Subedi


Intersec(on (∩)
• An intersec(on is defined by the symbol ∩
• A ∩ B defines a rela(on consis(ng of a set of all
tuple that are in both A and B.
• However, A and B must be union-compatible.
• Result of A ∩ B for previous example is:

PCA155-3 Madhav Subedi


Set Difference (-)
• Difference is denoted by –
• The result of A - B, is a relation which includes all
tuples that are in A but not in B.
• The two-operand relations A and B should be
Union compatible.
• Result of A - B for previous example is:

• A ∪ B = B ∪ A, A ∩ B = B ∩ A, but A – B != B – A
PCA155-3 Madhav Subedi
Cartesian Product(X)
• Cartesian Product in DBMS is an operation used to
merge columns from two relations.
• Generally, a Cartesian Product is never a meaningful
operation when it performs alone.
• However, it becomes meaningful when it is followed
by other operations.
• It is also called Cross Product or Cross Join.
• Result of Cartesian Product of A and B (A X B) has:
– columns: columns of A + columns of B
– Rows: rows of A X rows of B
• The result has every row of A in combination with
every row of B.

PCA155-3 Madhav Subedi


Join Operations
• Join operation is essentially a cartesian product followed
by a selection criterion.
• JOIN operation also allows joining variously related tuples
from different relations.
• Various forms of join operation are:
• Inner Joins: Theta join, EQUI join, Natural join
• Outer join: Left Outer Join, Right Outer Join, Full Outer Join
• In an inner join, only those tuples that satisfy the matching
criteria are included, while the rest are excluded.
• In an outer join, along with tuples that satisfy the matching
criteria, we also include some or all tuples that do not
match the criteria.

PCA155-3 Madhav Subedi


Theta Join
• The general case of JOIN operation is called a
Theta join. It is denoted by symbol θ
• A ⋈θ B, where θ represents a condition.
• Theta join can use any conditions in the selection
criteria.
• For above example, A ⋈ A.column 2 > B.column 2 (B)
results:

PCA155-3 Madhav Subedi


Equi Join
• When a theta join uses only equivalence condition, it
becomes a equi join.
• For above example, A ⋈ A.column 2 = B.column 2 (B) results:

• EQUI join is the most difficult operations to


implement efficiently using SQL in an RDBMS and
one reason why RDBMS have essential performance
problems.
PCA155-3 Madhav Subedi
Natural Join( )
• Natural join can only be performed if there is a
common attribute (column) between the
relations.
• The name and type of the attribute must be
same.
• For example, natural join of following tables:

PCA155-3 Madhav Subedi


Left Outer Join(A B)
• In the left outer join, operation allows keeping all
tuple in the left relation.
• However, if there is no matching tuple is found in
right relation, then the attributes of right relation
in the join result are filled with null values.

PCA155-3 Madhav Subedi


Left Outer Join(A B)…

PCA155-3 Madhav Subedi


Right Outer Join(A B)
• In the right outer join, operation allows keeping
all tuple in the right relation.
• However, if there is no matching tuple is found in
the left relation, then the attributes of the left
relation in the join result are filled with null
values.

PCA155-3 Madhav Subedi


Right Outer Join(A B)…

PCA155-3 Madhav Subedi


Full Outer Join: ( A B)
• In a full outer join, all tuples from both relations
are included in the result, irrespective of the
matching condition.
• Full outer join result of above tables A and B:

PCA155-3 Madhav Subedi


Operation(Symbols) Purpose
Select(σ) The SELECT operation is used for selecting a
subset of the tuples according to a given
selection condition
Projection(π) The projection eliminates all attributes of the
input relation but those mentioned in the
projection list.
Union Operation(∪) UNION is symbolized by symbol. It includes
all tuples that are in tables A or in B.
Set Difference(-) - Symbol denotes it. The result of A - B, is a
relation which includes all tuples that are in
A but not in B.
Intersec(on(∩) Intersection defines a relation consisting of a
set of all tuple that are in both A and B.

PCA155-3 Madhav Subedi


Operation(Symbols) Purpose
Cartesian Product(X) Cartesian operation is helpful to merge
columns from two relations.
Inner Join Inner join, includes only those tuples that
satisfy the matching criteria.
Theta Join(θ) The general case of JOIN operation is called a
Theta join. It is denoted by symbol θ.
EQUI Join When a theta join uses only equivalence
condition, it becomes a equi join.
Natural Join(⋈) Natural join can only be performed if there is
a common attribute (column) between the
relations.

PCA155-3 Madhav Subedi


Operation(Symbols) Purpose
Outer Join In an outer join, along with tuples that satisfy
the matching criteria.
Left Outer Join( ) In the left outer join, operation allows
keeping all tuple in the left relation.
Right Outer join() In the right outer join, operation allows
keeping all tuple in the right relation.
Full Outer Join() In a full outer join, all tuples from both
relations are included in the result
irrespective of the matching condition.

PCA155-3 Madhav Subedi


Relational Calculus
• Relational calculus is a non-procedural query
language.
• In the non-procedural query language, the user is
concerned with the details of what to obtain as
the end results.
• The relational calculus tells what to do but never
explains how to do.
• Relational Calculus exists in two forms:
– Tuple Relational Calculus (TRC)
– Domain Relational Calculus (DRC)

PCA155-3 Madhav Subedi


Relational Algebra & Relational Calculus
RELATIONAL ALGEBRA RELATIONAL CALCULUS

It is a Procedural language. Relational Calculus is Declarative language.

Relational Algebra means how to obtain Relational Calculus means what result we
the result. have to obtain.

In Relational Algebra, The order is


In Relational Calculus, The order is not
specified in which the operations have to
specified.
be performed.

Relational Algebra is independent on Relation Calculus can be a domain


domain. dependent.

Relational Algebra is nearer to a Relational Calculus is not nearer to


programming language. programming language.

PCA155-3 Madhav Subedi


Tuple Relational Calculus (TRC)
• In tuple relational calculus, we work on filtering
tuples based on the given condition.
• In this form of relational calculus, we define a
tuple variable, specify the table(relation) name in
which the tuple is to be searched for, along with a
condition.
• We can also specify column name using a . dot
operator, with the tuple variable to only get a
certain attribute(column) in result.

PCA155-3 Madhav Subedi


Tuple Relational Calculus (TRC)…
• In Tuple Calculus, a query is expressed as
• {t| P(t)}
– where t = resulting tuples,
– P(t) = known as Predicate and these are the conditions that are
used to fetch t
• Thus, it generates set of all tuples t, such that Predicate
P(t) is true for t.
• P(t) may have various conditions logically combined with
∨ AND (∧),
OR (∨), ∧ NOT(¬).
• It also uses quantifiers:
– ∃ t ∈ r (Q(t)) = “there exists” a tuple in t in relation r such that
predicate Q(t) is true.
– ∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.

PCA155-3 Madhav Subedi


Tuple Relational Calculus (TRC)…
• Customer(customer_name, street, city)
• Branch(branch_name, branch_city)
• Account(account_number, branch_name, balance)
• Loan(loan_number, branch_name, amount)
• Borrower(customer_name, loan_number)
• Depositor(customer_name, account_number)

PCA155-3 Madhav Subedi


Tuple Relational Calculus (TRC)…
• list the records in loan table having loans of
greater than or equal to 10000 amount.
– {t | Loan(t) ∧ t.amount>=10000}
– {t| t ∈ Loan ∧ t.amount>=10000}
• Find the loan number for each loan of an amount
greater or equal to 10000.
– {t| ∃ s ∈ loan(t.loan number = s.loan number ∧
s.amount>=10000)}

PCA155-3 Madhav Subedi


Tuple Relational Calculus (TRC)…
• Find the names of all customers who have a loan
and an account at the bank.
– {t | ∃ s ∈ borrower( t.customer_name =
s.customer_name) ∧ ∃ u ∈ depositor( t.customer_name
= u.customer-name)}
• Find the names of all customers having a loan at the
“ABC” branch.
– {t | ∃ s ∈ borrower(t.customer_name =
s.customer_name ∧ ∃ u ∈ loan(u.branch_name = “ABC”
∧ u.loan_number = s.loan_number))}

PCA155-3 Madhav Subedi


Domain Relational Calculus (DRC)
• In domain relational calculus, filtering variable uses
the domain of attributes.
• Domain relational calculus uses the same operators
as tuple calculus.
– It uses logical connectives ∧ (and), ∨ (or) and ┓ (not).
• It uses Existential (∃) and Universal Quantifiers (∀) to
bind the variable.
• Notation:
{≺a1, a2, a3, ..., an≻ | P (a1, a2, a3, ... ,an)}
– a1, a2 are attributes
– P stands for formula built by inner attributes

PCA155-3 Madhav Subedi


Domain Relational Calculus (DRC)
• Consider the table structures mentioned above in
TRC.
• Find the loan number, branch, amount of loans of
greater than or equal to 100 amount.
– {≺l,
≺ b, a≻
≻|≺
≺l, b, a≻
≻ ∈ loan ∧ (a ≥ 100)}
• Find the loan number for each loan of an amount
greater or equal to 150.
– {≺l≻
≺ ≻ | ∃ b, a (≺l,
≺ b, a≻
≻ ∈ loan ∧ (a ≥ 150)}
• Find the names of all customers having a loan at the
“Main” branch and find the loan amount .
– {≺c, a≻ | ∃ l (≺c, l≻ ∈ borrower ∧ ∃ b (≺l, b, a≻ ∈ loan ∧
(b = “Main”)))}

PCA155-3 Madhav Subedi

You might also like