0% found this document useful (0 votes)
22 views83 pages

Relational Model & Relational Algebra

RDBMS

Uploaded by

chaitanyaharyan5
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)
22 views83 pages

Relational Model & Relational Algebra

RDBMS

Uploaded by

chaitanyaharyan5
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/ 83

SE IT

Semester – III

Database Management Systems


(ITC303)

Prof. Nilesh Mali


IT Dept, VPPCOE&VA College
SE IT
Semester – III
Module 03
Relational Model & Relational
Algebra

Prof. Nilesh Mali


IT Dept, VPPCOE&VA College
Syllabus
Module 3. Relational Model & Relational Algebra

Introduction to Relational Model, Relational Model


Constraints and Relational Database Schemas, Concept of
Keys: Primary Kay, Secondary key, Foreign Key, Mapping
the ER and EER Model to the Relational Model,
Introduction to Relational Algebra, Relational Algebra
expressions for Unary Relational Operations, Set Theory
operations, Binary Relational operation Relational
Algebra Queries

Self-learning Topics: Map the ER model designed in


module II to relational schema..
Relational Database

ID Name AGE Course


Relational Database Advantages
Relational Database
Relational Database
Relational Database
Relational Database
Relational Database
Relational Database
Relational Database
Relational Database
Relational Database
Relational Database
Relational Database
Concepts
● Relation instance: A finite set of tuples in the relational database
system represents relation instance. Relation instances do not have
duplicate tuples.

● Attribute domain − Every attribute has some pre-defined value


scope, known as attribute domain.

● Relational key: In the relational key, each row has one or more
attributes. It can identify the row in the relation uniquely.
Relational Databases

● Relational schema: A relational schema contains the name of


the relation and name of all columns or attributes.
Relational Databases
Why we have Keys in DB?
• A Key is an attribute or a set of attributes in a relation that
identifies a tuple (record) in a relation.

• Database key is helpful for finding unique record or row from


the table.
Types of Keys in Database
1. Primary Key
2. Super Key
3. Candidate Key
4. Alternate/Secondary Key
5. Composite Key
6. Foreign Key
7. Unique Key
Primary Key (PK)

Employee • Which is Unique & Can’t be have NULL


EmployeeID Value
EmployeeName • Is the column you choose to maintain
DeptID
uniqueness in a table at row level.
DOB • Here in Employee table we have EmployeeID
as a primary key.

Primary Key = Unique + Not NULL


To define a field as primary key, following
conditions had to be met :
● No two rows can have the same primary key value.
● Every row must have a primary key value.
● The primary key field cannot be null.
● Value in a primary key column can never be modified or
updated, if any foreign key refers to that primary key.
Super Key
Employee
• If you add any other Column / Attribute to a
EmployeeID Primary Key then it become a Super Key,
like EmployeeID + EmployeeName is a
EmployeeName
Super Key.
SSN

DeptID • Super key stands for superset of a key.


DOB

• A Super Key is a set of one or more


attributes that are taken collectively and
can identify all other attributes uniquely.
Candidate Key
Employee • Candidate Key is a set of attributes that uniquely
identify tuples in a table.
EmployeeID
EmployeeName
• Here in Employee table EmployeeID & SSN are eligible
for a Primary Key and thus are Candidate keys.
SSN
• Candidate Key is a super key with no repeated
DeptID
attributes.
DOB • The Primary key should be selected from the candidate
keys.
• A table can have multiple candidate keys but only a
single primary key.
Alternate/Secondary Key
Employee • Candidate column other the Primary column, like if
EmployeeID EmployeeID is set for a PK then SSN would be the
EmployeeName Alternate/Secondary key.
SSN
DeptID • From Candidate keys, all the keys which are not
DOB
primary key are called as Alternate/Secondary Keys.
Composite Key
Employee
• If a table do have a single column that qualifies
EmployeeName
for a Candidate key, then you have to select 2 or
more columns to make a row unique.
DeptID

DOB
• Like if there is no EmployeeID or SSN columns,
then you can make EmployeeName +
DateOfBirth (DOB) as Composite Primary Key.

● But still there can be a narrow chance of


duplicate rows.
Foreign Key (FK)
Department Employee
EmployeeID
DeptID
EmployeeName
DeptName
SSN

DeptID
DOB

• Here in above tables DeptID of Department table is


Primary Key where as DeptID of Employee is an Foreign
key.
• It means it has referred to another table. This concept is also
know as Referential Integrity.
Unique Key
Employee • Unique key is same as primary with the difference
EmployeeID being the existence of null.
EmployeeName • Unique key field allows one value as NULL value.
SSN

EmailID
DOB
Integrity Rules
Integrity constraints are used to ensure accuracy and consistency of the data in
a relational database.
Database integrity refers to the validity and consistency of stored data.
Integrity constraints are a set of rules.
It is used to maintain the quality of information.

TYPES OF INTEGRITY CONSTRAINTS


1. Domain Integrity
2. Entity Integrity Constraint
3. Referential Integrity Constraint
4. Key Constraints
Integrity Constraints
1. Domain constraints

○ Domain constraints can be defined as the definition of a valid set of values for an
attribute.
○ The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the attribute must be available in the corresponding domain.
Integrity Constraints
2. Entity integrity constraints
○ The entity integrity constraint states that primary key value can't be null.
○ This is because the primary key value is used to identify individual rows in relation and
if the primary key has a null value, then we can't identify those rows.
○ A table can contain a null value other than the primary key field.
Integrity Constraints
3. Referential Integrity Constraints
○ A referential integrity constraint is specified between two tables.
○ In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2,
then every value of the Foreign Key in Table 1 must be null or be available in Table 2.
Integrity Constraints
4. Key constraints

○ Keys are the entity set that is used to identify an entity within its entity set uniquely.
○ An entity set can have multiple keys, but out of which one key will be the primary key.
Relational Algebra
• The relational algebra is a procedural query language
• It consists of a set of operations that take one or two relations as
input and produce a new relation as their result.
• It uses operators to perform queries.

• Relational algebra mainly provides a theoretical foundation for


relational databases and SQL
Cont.
• The fundamental operations in the relational algebra are select,
project, union, set difference, Cartesian product, and rename

• The select, project, and rename operations are called unary


operations, because they operate on one relation

• The other three operations operate on pairs of relations and


are, therefore, called binary operations
Unary Relational Operations (operate on one table)
▪ SELECT (symbol: σ (sigma))
▪ Selects a subset of rows from relation

▪ PROJECT (symbol: π (pi))


▪ Selects columns from relation

▪ RENAME (symbol: ρ (rho))


SELECTION
Select Operator
Produce table containing subset of rows of argument table
satisfying condition
σcondition relation
Example:
Person σHobby=‘stamps’(Person)
Id Name Address Hobby Id Name Address Hobby
1123 John 123 Main stamps 1123 John 123 Main stamps
1123 John 123 Main coins 9876 Bart 5 Pine St stamps
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps

39
Selection Condition

Operators: <, ≤, ≥, >, =, ≠

Simple selection condition:


<attribute> operator <constant>

<attribute> operator <attribute>

<condition> AND <condition>

<condition> OR <condition>

NOT <condition>
40
Selection Condition - Examples
σ Id>3000 Or Hobby=‘hiking’ (Person)

σ Id>3000 AND Id <3999 (Person)


σ NOT(Hobby=‘hiking’) (Person)

σ Hobby≠‘hiking’ (Person)

41
Selection Condition - Examples
• Select the EMPLOYEE tuples whose department number is 4:
σ DNO = 4 (EMPLOYEE)
• Select the employee tuples whose salary is greater than
$30,000:
σ (EMPLOYEE)
SALARY > 30,000

• Select the instructors in Physics with a salary greater than $90,000,


(and (∧), or (∨), and not (¬))

σ dept name =“Physics” ∧ salary>90000 (instructor )


PROJECTION

Note: By Default, projection removes duplicate data.


Project Operator
Produces table containing subset of columns of argument table
Πattribute list(relation)

Example:
Person ΠName,Hobby(Person)

Id Name Address Hobby Name Hobby

1123 John 123 Main stamps John stamps


1123 John 123 Main coins John coins
5556 Mary 7 Lake Dr hiking Mary hiking
9876 Bart 5 Pine St stamps Bart stamps
44
Project Operator
• Example:
Person ΠName,Address(Person)
Id Name Address Hobby Name Address

1123 John 123 Main stamps John 123 Main


1123 John 123 Main coins Mary 7 Lake Dr
5556 Mary 7 Lake Dr hiking Bart 5 Pine St
9876 Bart 5 Pine St stamps

Result is a table (no duplicates)


45
Composition of Relational Operations
Expressions

Id Name Address Hobby Id Name


1123 John 123 Main stamps 1123 John
1123 John 123 Main coins 9876 Bart
5556 Mary 7 Lake Dr hiking
9876 Bart 5 Pine St stamps Result
Person

47
Example
• “Find the name of all instructors in the Physics
department.
π name (σ dept name =“Physics”(instructor))
RENAME
Rename operator returns an existing relation under a new name.

Rename is denoted by the Greek letter rho (ρ)


Relational Algebra Operations: Set Theory

• Union
• Intersection
• Set Difference / Minus
UNION
• It is a Binary operation, denoted by ∪

•SYNTAX : (Query1) ∪ (Query2)

– The result of R ∪ S, is a relation that includes all tuples that


are either in R or in S or in both R and S

– Duplicate tuples are eliminated.


Union Compatible Relations

Two relations are union compatible if


● Both have same number of columns
● Names of attributes are the same in both

Union compatible relations can be combined using union,


intersection, and set difference

52
UNION
Example
Consider the following relations
▪Borrower (customer-name, loan-number)
▪Depositor (customer-name, account-number)
▪Customer (customer-name, street-number, customer-city)

Q. List all the customers who have either an account or a loan or


both

Ans: π customer-name (Borrower) ∪ π customer-name (Depositor)


INTERSECTION
• INTERSECTION is denoted by ∩

• SYNTAX : (Query1) ∩(Query2)

• The result of the operation R ∩ S, is a relation that includes


all tuples that are in both R and S
INTERSECTION Example
Consider the following relations
▪Borrower (customer-name, loan-number)
▪Depositor (customer-name, account-number)
▪Customer (customer-name, street-number, customer-city)

Q. List all the customers who have both a loan and an account.

Ans: π customer-name (Borrower) ∩ π customer-name (Depositor)


Difference (Minus) Operator
SYNTAX : (Query1) – (Query2)

R-S: returns all rows that occur in R but not in S.

R and S must be union-compatible.

Scheme of the relation is the scheme of the R.


Difference (Minus) Operator Example
Consider the following relations
▪Borrower (customer-name, loan-number)
▪Depositor (customer-name, account-number)
▪Customer (customer-name, street-number, customer-city)

Q. Find the names of all customers who have an account but not a
loan.

Ans: π customer-name (Depositor) - π customer-name (Borrower)


Cartesian-Product Operation
• Denoted by x
• Also known as cross product or cross-join
• Notation r x s
• Combines tuples members of one relation to other relation
• R (a1, a2 … an) x S(b1, b2 … bn)
– T (a1, a2 … an, b1, b2 … bn)
• If we need the all borrowers and loan holder in chennai
• σ branch = “chennai” (borrower x loan)
Cartesian-Product Operation
■ Relations r, s:

■ r x s:
DIVISION OPERATOR
SYNTAX: (Query1) ÷ (Query2)

Division operator A÷B or A/B can be applied if and only if:


● Attributes of B is proper subset of Attributes of A.
● The relation returned by division operator will have attributes = (All
attributes of A – All Attributes of B)
● The relation returned by division operator will return those tuples
from relation A which are associated to every B’s tuple.
DIVISION OPERATOR
Produce the tuples in one relation r, that match all tuples in another relation s

SYNTAX: (Query1) ÷ (Query2)

R1 ÷ R2
DIVISION OPERATOR Example
Aggregate Functions
Aggregation function takes a collection of values and returns a single value as
a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra
G1, G2, …, Gn
g F1( A1), F2( A2),…, Fn( An) (E)
E is any relational-algebra expression
G1, G2 …, Gn is a list of attributes on which to group (can be empty)

Each Fi is an aggregate function

Each Ai is an attribute name


Aggregate Operation – Example
Relation r:
A B C

a a 7
a b 7
b b 3
b b 10

sum-C
g sum(c) (r)
27
Aggregate Operation – Example

Relation account grouped by branch-name:


branch-name account-number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch-name
g sum(balance)
(account)
branch-name balance
Perryridge 1300
Brighton 1500
Redwood 700
JOIN OPERATOR
JOIN is used to combine related tuples from two relations

•In its simplest form the JOIN operator is just the cross product of the two relations.

•As the join becomes more complex, tuples are removed within the cross product to
make the result of the join more meaningful.

•JOIN allows you to evaluate a join condition between the attributes of the relations o
which the join is undertaken

SYNTAX : (Relation1) <join condition> (Relation2)


Natural-Join Operation
● Notation: r s
Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R ∪ S obtained as follows:
Consider each pair of tuples t from r and t from s.
r s
If t and t have the same value on each of the attributes in R ∩ S, add a tuple t to the result, where
r s

t has the same value as t on r


r
t has the same value as t on s
s
Natural-Join Operation
Example
Natural-Join Operation
Example
Natural-Join Operation
Example
Theta (θ) Join
Theta join combines tuples from different relations provided they satisfy the theta
condition.

The join condition is denoted by the symbol θ.

Notation
R1 ⋈θ R2

R1 and R2 are relations having attributes (A1, A2, .., An) and
(B1, B2,.. ,Bn) such that the attributes don’t have anything in common, that is R1 ∩ R2 = Φ

Theta join can use all kinds of comparison operators.


Theta (θ) Join/ Equijoin
Outer Join
● An extension of the join operation that avoids loss of information.

● Computes the join and then adds tuples form one relation that do 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 1
Left Outer Join – Example 1
Right Outer Join – Example 1
Full Outer Join – Example 1
Outer Join – Example 2
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 2
Inner 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 2
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

inprotected.com

You might also like