0% found this document useful (0 votes)
43 views71 pages

DE Module2 RelationalModel PPT

The relational model uses tables to represent data and relationships. Each table has multiple columns with unique names. Rows are called tuples and columns are called attributes. A relation schema specifies a table's name, attributes, and domains. Relations, tuples, attributes, and domains form the core components of the relational model for structuring data in tables. Relational algebra provides basic operations like select, project, join to manipulate data in relational databases.

Uploaded by

wowafer745
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)
43 views71 pages

DE Module2 RelationalModel PPT

The relational model uses tables to represent data and relationships. Each table has multiple columns with unique names. Rows are called tuples and columns are called attributes. A relation schema specifies a table's name, attributes, and domains. Relations, tuples, attributes, and domains form the core components of the relational model for structuring data in tables. Relational algebra provides basic operations like select, project, join to manipulate data in relational databases.

Uploaded by

wowafer745
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/ 71

RELATIONAL MODEL

PREPARED BY:
SIMANTIKA RAY
Structure of relational model:
 The relational model uses a collection of tables to represent both data
and the relationships among those data.

 Each table has multiple columns, and each column has a unique name.

 Relational model is a simple model in which the database is represented


as a collection of relations, where each relation is represented by a two
dimensional table.

 Tables are referred to as relations, the rows as tuples or records and the
columns as attributes or fields.

 Domain is the pool of values from which the actual values of a column
are drawn.
 Relation (or tables)
 It is a two dimensional array of rows and columns
containing single valued entries (entities) and no
duplicate rows.
 Each cell can have only one value and no two rows may be
identical.
 It is represented by combining attributes of the entities.

 Tuples (or rows or records)


 The rows of the relation, which represented individual
records is known as tuples.

 Attributes(or columns or fields)


 They are the columns of the relation which represent the
properties of the entities.

 Domain
 It is a set of permissible values that can be assigned to an
attributes.
 Relation schema
 It specifies the relation’s name, its attributes and the domain of each
attribute.
 If R is the relation name and A1, A2, A3,……………. An is the list of attributes

then R(A1, A2, A3……………. An) is the relation schema.


 Each attribute in the relation schema takes a value from some specific
domain i.e Domain(Ai ).
 The relation schema is also called as intension.

 Ex: student (Rollno: integer, Name :string, Address: string)

 Degree of a relation (or Arity)


 It is the total number of attributes in a relation(ie number of columns in
a table).

 Cardinality of a relation
 It is the total number of tuples in a relation(i.e number of rows ina
table).

 Relational Instance (or State)

 It is the collection of tuples for a given relational schema at a particular


moment of time. It is denoted by r.

Ex: For R(A1, A2, …………. An), relational instance is r(R).

r = { t1, t2, t3, t4……………. tm } (ie m rows)


t = < V1, V2, V3, V4……………. Vn > (ie n columns)
where Vi belongs to domain (Ai, ).

 The relational state or instance is also called extension.


MAPPING E-R MODEL TO RELATIONAL MODEL
 A relational database is a collection of tables, which is constructed
from an ER diagram.

1. Tabular Representation of Strong Entity Sets


 Each strong Entity set (E) is converted into a relation (R) ie table.,
containing all simple attributes ie Columns.
 Primary key is identified.
 Example:

Student
Tabular Representation of Weak Entity Sets
 The weak Entity Set(W) is converted into a relation(R) ie
table, containing all simple attributes ie columns.
 If a strong entity set (E) is owner of a weak entity set then
primary key of that strong entity set is also
included(which acts as a foreign key).
 Now the combination of primary key of the strong entity
and key of weak entity acts as primary key for the weak
entity.
Tabular Representation of Relationship Sets
 One-to-one relationship

 Given a relationship R (one-to-one) involving two entities E1 and


E2. One of the entity is chosen (preferably with total
participation) and primary key of the other entity is added to it
as a foreign key in the table.

 If any attributes are present for the relationship R, then those are
also included.
 One-to-Many Relationship / Many-to-one Relationship :

 Given a relationship R (Many-to-one) involving two entities


E1 and E2. The entity at the N-side of R (ie many side of
relationship) is taken and primary key of the other entity is
added to it as a foreign key, in the table.

 The attributes of the relationship (if any) are also included.


Many-to-many relationship

 Given a relationship R (many-to-many) involving two entities


E1 and E2. Create a new table for the relationship R, which
includes the primary keys of both entities, as foreign
keys.

 The attributes of the relationship(if any) are also included in


the new table.
Multivalued Attributes Conversion
 Example:

For each multivalued attribute A, create a new table containing the attribute
and the primary key as a foreign key.

The foreign key along with the attribute forms the primary key of the new
table.
Generalization and Specialization conversions:
 All the specialized entity sets are converted to tables, if they are disjoint ie nothing
in common or no overlapping entities.
 If they are overlapping then convert into an extra table.

Case -1: disjoint generalization:


we have two tables:
 savings-account, with attributes account-number, balance, and interest-
rate.(primary key : account-number)
 checking-account, with attributes account-number, balance, and overdraft-
amount(primary key : account-number)
Case -2 : Overlapping generalization:
we have three tables:
 account, with attributes account-number and balance

 savings-account, with attributes account-number and interest-rate

 checking-account, with attributes account-number and overdraft-amount


Relational Constraints
 Constraints refer to the various restrictions imposed on the data , which
can be specified on a relational database.
 Constraints are basically of three types

1. Domain Constraints:
 They specify that each attribute in a relation (ie table) must contain an
atomic value, only from the corresponding domain.
 They are mainly created for defining the logically correct values for an
attribute of a relation.

2. Key Constraints
 It states that the key attribute value in each tuple (ie record) must be
unique (ie no duplicates).
Integrity Constraints
 Integrity refers to maintaining the consistency of data. Thus integrity
constraints ensures that changes made to the database doesn’t hamper
consistency.

 There are two types of integrity constraints:


1. Entity Integrity Constraints: It states that primary key
value can not be null. NULL represents missing value.

2. Referential Integrity Constraints


 It states that the database must not contain any unmatched foreign

key values.
 The foreign key must exactly match with the values of primary key
in referenced relation( ie table) or it must be null.
RELATIONAL ALGEBRA
 The relational algebra is a procedural query language.

 Relational algebra is a set of basic operations used to manipulate the data in


relational model.

 It consists of a set of operations that take one or two relations as input and
produce a new relation as their result.

 The basic operations can be classified as


1. Basic Set Operations

2. Relational Operations
Basic Set Operations

 These are the binary operations ie operate on two sets or


relations.

 The two relations should be union compatible.

 Two relations r and s are said to be union compatible if :


1. The relations r and s must be of the same
arity/degree. That is, they must have the same
number of columns.
2. The domains of the ith attribute of r and the ith
attribute of s must be the same, for all i.
Various Set operations are as follows:

1. Union

2. Intersection

3. Set difference

4. Cartesian Product
Union( U ):
Example:

 If R1 and R2 are two union compatible relations then R3 = R1 U R2 is the


relation containing tuples(ie rows) that are either in R1 or in R2 or in both(but
duplicates are eliminated)

 It is commutative ie R1 U R2 = R2 U R1
 It is associative ie R1 U(R2UR3) = (R1 U R2) U R3
INTERSECTION(∩):
 If R1 and R2 are two union compatible relations then R3 =
R1 ∩ R2 is the relation containing tuples(ie rows) that are
in both R1 and R2.

 It is commutative ( ie R1 ∩ R2 = R2 ∩ R1)
 It is associative ( ie R1 ∩(R2 ∩ R3 = (R1 ∩ R2) ∩ R3)

 Degree of resultant table is same as original tables


SET DIFFERENCE (− )
 If R1 and R2 are two union compatible relations then R3 = R1- R2 is the
relation containing tuples(ie rows) that are in R1 but not in R2.

 It is not commutative ie R1 − R2 ≠ R2 − R1
 It is not associative ie R1 − (R2 − R3 )≠ (R1 − R2) − R3
 Degree of resultant table is same as original tables
CARTESIAN PRODUCT ( X )

Degree(R3) = Degree(R1) + Degree(R2)

 It is commutative ( ie R1 X R2 = R2 X R1)
 It is associative ie R1 X (R2 X R3) = (R1 X R2) X R3)
Relational operations

 These operations were developed for relational databases.

 They operate on one or more relations to form another relation.

 Various relational operations are:


1.

2. PROJECT ( Π )
3. JOIN ( ⋈ )
The Select Operation
 The select operation selects tuples that satisfy a given condition or predicate.

 We use the lowercase Greek letter sigma (σ) to denote selection.

 The predicate appears as a subscript to σ.

 Ex : Let’s assume a relation Employee containing the attributes Empid,


Name, Deptid and Salary.
The Project Operation:

 It is used to extract some specific attributes( ie columns or


fields) from one relation based on names of the attributes.
 Any duplicate rows are eliminated.

 Projection is denoted by the uppercase Greek letter pi (Π).

 Example:

 Result:
 List the customer names who are either depositors or borrowers of the bank.

 Show the name of the employee with Empid as 100.

 Show the details of employees of department number 10 getting salary more


than 20,000.
 Show the details of employees who either work in
department number 20 or has name “Smith” with salary
greater than 30000.
JOIN OPERATION
 It allows combing of two relations to form a single new relation. It is used
to select related tuples(ie rows from two relations).
 It requires that both the joined relations must have at least one domain
compatible attribute, which is known as the join attribute .
 Join is a Cartesian product along with a condition.
 It is symbolized by ⋈ .
There are various types of joins such as

 Inner Join
 Equi Join
 Natural Join
 Non Equi Join
 Theta Join
 Self Join

 Outer Join
 Left Outer join
 Right Outer Join
 Full Outer Join
INNER JOIN
 It retrieves all the combined tuples from the two relations,
which satisfy the join conditions.
 Example:

 Resultant relation:
Equi Join
 It is a join in which equlity operator (ie. =) is only used
in each of the join conditions.
Non Equi join
 It is a join in which any operator can be used, strictly
except the equality operator, in each of the join condition (ie
<=, >=, < , >, or ≠ ).
 The converse of non equi join is equi join.
Theta Join
 It is a join in which any operator can be used, including the
equality operator, in each of the join conditions (ie <=, >=, < ,
>, or ≠ ).
 It is denoted as join condition Ɵ.

Example
Natural Join:

 It is a special case of equi join.

 It requires that the attribute names used in the condition must be the same
in both the relations.

 It retains only one attribute from the pairs.

 It is denoted by * and the condition need not be shown as it is assumed by


default.

 If the attribute names differ then the attribute name of one relation must be
renamed before applying natural join.

 Rho (ie ρ ) can be used for renaming the fields of a relation.


 Exmple:

 We need to change the column name from Deptid to Dept-


no in “Department” table.
 Now the equi-join will be written as:
 Display names of employees who work in “HR” department

 Join is a binary operation as it needs two tables to operate


on.
Question: Display employee id and name of employees who work in
department located at “BBSR”.

Question : Display name and address of employees who belong to


department name “Finance” and has age greater than 30.

Question: Display name,salary and department name of employees


staying in BBSR and earning salary more than 30000.

Question: Display name,salary and department name of those


employees staying in BBSR or having age more than 35 with
salary more than 30000.
 Example

 Result Relation

 Degree of resultant relation = (Degree of relation1


+Degree of relation2) -1
Rename operator
 Rho (ie ρ ) can be used for renaming the fields of a relation.

 To change the name of Emp table to employee:

ρemployee(Emp)

 ρ x (E): returns the result of expression E under the name x.


 ρ x (A1, A2, A3… An) (E): returns the result of expression E under
the name x with attributes renamed to A1, A2, A3… An.
Self Join
 It is a join in which the relation is joined with itself(ie as if two different relational).
EXAMPLES OF QUERIES IN RELATIONAL ALGEBRA
Given the tables:
Sailor(Sid, Sname, Rating, Age)
Boat (Bid, Bname, Bcolor)
Reserve(Sid, Bid,Date)
 Display the name of the sailors who have reserved boat with
id number 105.
Ans: ∏ Sname (Sailor * (бBid=105 (Reserve)))

 Display the name of the sailors who have reserved a red color
boat.
Ans: ∏ Sname (Sailor *(Reserve * (бBcolor = ‘red’ (Boat))))

 Find the color of the boat reserved by Mr.X


Ans : ∏ Bcolor (Boat * (Reserve * ( бSname = ‘X’ (Sailor))))

 Find the name of the sailors who have reserved any boat.
Ans: ∏ Sname (Sailor * Reserve )
 Find the name of the sailors who have reserved both a red
and a green color boat.
Ans: ∏ Sname (Sailor *(Reserve * (бBcolor = ‘red’ (Boat))))

OUTER JOIN
 Outer joins are used to retrieve additional tuples from one or both tables, even
though they don’t satisfy the join condition.

 Left Outer Join


 It retrieves all additional extra tuples from the left table (ie. first table of the
join operation)
 It is denoted by the symbol

 Right Outer Join


 It retrieves all additional extra tuples from the right table (ie. second table of
the join operation)
 It is denoted by the symbol

 Full Outer Join


 It retrieves all additional extra tuples from both the tables (ie. first and second
tables of the join operation)
 It is denoted by the symbol
 Display the name, address, citycode, branch id and salary details of
employees posted in some branches.

 Display the name, address, citycode, branch id and salary details of


employees posted in some branch along with the employees who have not
yet posted in any branch.
 Display the name, address, citycode, branch id and salary details of
registered employees posted in some branches along with the branch
employees whose name is not in employee register.

 Display the details of all employees who may or may not be posted in any
branch along with employees who names are not uploaded in employee
register.
DIVISION ( ÷)

 It retrieves all values of the attributes in the first table(ie attributes


not present in second table) but those values should be related to all values
of the common attributes of the second table.

 If R1÷ R2 then it retrieves the values of R1 , related to every value of R2,


but displays only the attribute of R1.

 Second table columns must be a proper subset of first table columns.

 The division operation, denoted by ÷, is suited to queries that include


the phrase “for all.”
Division operation is useful for expressing “for all” queries like:
Find sids of sailors who have reserved all boats.
 Display the employees ids of employees who are working on
all projects running in the department.
Ans: Employee ÷ Project
EXTENDED RELATIONAL ALGEBRA OPERATIONS

 Extended relational algebra operations are as follows:

 Generalized Projection

 Aggregate Functions

 Outer Join

 Null Values∗∗
Generalized Projection

 The generalized-projection operation extends the projection


operation by allowing arithmetic functions to be used in
the projection list.

 The generalized projection operation has the form


 Example

 For every customer of the bank, display the id ,name and


how much more the person can spend.
Aggregate Functions
 Aggregate functions take a collection of values and return a single value as a
result.
 Example:

 Avg
 Count
 Min
 Max etc.

 The collections on which aggregate functions operate can have multiple


occurrences of a value; the order in which the values appear is not relevant.

 Such collections are called multisets.


Example:
 Display total credit spent by all customers of the bank.

 The relational-algebra operation signifies that aggregation is to be


applied, and its subscript specifies the aggregate operation to be applied.

 The symbol is the letter G in calligraphic font; read it as “calligraphic


G.”
 Example: Count distinct names of the customers.

 Display total credit spent by customers for every group of credit limit.
Question: Display the total credit spent and maximum credit spent for
every group of credit limit.

Answer:
 Question: Display the total salary and maximum salary for every
department.
OUTER JOIN
Outer joins are used to retrieve additional tuples from one or both tables, even
though they don’t satisfy the join condition.

 Left Outer Join


 It retrieves all additional extra tuples from the left table (ie. first table of the
join operation)
 It is denoted by the symbol

 Right Outer Join


 It retrieves all additional extra tuples from the right table (ie. second table of
the join operation)
 It is denoted by the symbol

 Full Outer Join


 It retrieves all additional extra tuples from both the tables (ie. first and
second tables of the join operation)
 It is denoted by the symbol
Null Values∗∗
 Since the special value null indicates “value unknown or nonexistent,” any
arithmetic operations(such as +,−,∗, /) involving null values must return a
null result.
 Any comparisons(such as <,<=,>,>=,=) involving a null value evaluate to
special value unknown.

Output of boolean operations:


 and : (true and unknown)=unknown;
(false and unknown)=false;
(unknown and unknown)=unknown.

o or : (true or unknown)=true;
(false or unknown)=unknown;
(unknown or unknown)=unknown.

o not : (not unknown)=unknown.


Relational operations with null values:
 select:

 The selection operation evaluates predicate P in on each tuple


t in E.
 If the predicate returns the value true, t is added to the result.
 Otherwise, if the predicate returns unknown or false, t is not added to the
result.

 Join:
 If two tuples from the participating tables have null value in common
attribute, then the tuples don’t match.

 Project:
 The projection operation treats nulls just like any other value when
eliminating duplicates.
 If two tuples in the projection result are exactly the same, and both have
nulls in the same fields, they are treated as duplicates.
 union,intersection,difference:
 These operations treat tuples that have the same values on all fields as
duplicates even if some of the fields have null values in both tuples.

 Aggregate:
 When nulls occur in aggregated attributes, the operation deletes null
values at the outset, before applying aggregation.

 Outer join:
 For tuples that do not occur in the join result may be added to the result
padded with nulls.

You might also like