0% found this document useful (0 votes)
27 views20 pages

What Is Relational Algebra

Relational algebra is a set of operations used to manipulate and query data in relational databases, including fundamental operators like UNION, INTERSECT, and JOIN. The document explains the SQL UNION operator, which combines results from multiple SELECT statements while removing duplicates, and provides examples of various JOIN types, including theta, natural, and outer joins. Additionally, it covers characteristics of DBMS, relational calculus, physical storage media, and methods of file organization like sequential and direct file organization.

Uploaded by

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

What Is Relational Algebra

Relational algebra is a set of operations used to manipulate and query data in relational databases, including fundamental operators like UNION, INTERSECT, and JOIN. The document explains the SQL UNION operator, which combines results from multiple SELECT statements while removing duplicates, and provides examples of various JOIN types, including theta, natural, and outer joins. Additionally, it covers characteristics of DBMS, relational calculus, physical storage media, and methods of file organization like sequential and direct file organization.

Uploaded by

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

What is Relational Algebra?

Relational algebra consists of a certain set of rules or operations that are widely used to
manipulate and query data from a relational database

Fundamental Operators

These are the basic/fundamental operators used in Relational Algebra

What is SQL UNION Operator?

The SQL UNION operator combines the results of two or more SELECT statements into one
result set. By default, UNION removes duplicate rows, ensuring that the result set contains
only distinct records.

Examples of SQL UNION

Let’s look at an example of UNION operator in SQL to understand it better.

Let’s create two tables “Emp1” and “Emp2”;

Emp1 Table

Write the following SQL query to create Emp1 table.

CREATE TABLE Emp1(

EmpID INT PRIMARY KEY,

Name VARCHAR(50),

Country VARCHAR(50),

Age int(2),

mob int(10)

);

-- Insert some sample data into the Customers table

INSERT INTO Emp1 (EmpID, Name,Country, Age, mob)

VALUES (1, 'Shubham', 'India','23','738479734'),

(2, 'Aman ', 'Australia','21','436789555'),

(3, 'Naveen', 'Sri lanka','24','34873847'),

(4, 'Aditya', 'Austria','21','328440934'),

(5, 'Nishant', 'Spain','22','73248679');


SELECT* FROM Emp1;

Output:

Emp1 Table

Emp2 Table

Write the following SQL query to create Emp2 table

CREATE TABLE Emp2(

EmpID INT PRIMARY KEY,

Name VARCHAR(50),

Country VARCHAR(50),

Age int(2),

mob int(10)

);

-- Insert some sample data into the Customers table

INSERT INTO Emp2 (EmpID, Name,Country, Age, mob)

VALUES (1, 'Tommy', 'England','23','738985734'),

(2, 'Allen', 'France','21','43678055'),

(3, 'Nancy', 'India','24','34873847'),

(4, 'Adi', 'Ireland','21','320254934'),

(5, 'Sandy', 'Spain','22','70248679');

SELECT * FROM Emp2;

Output:
Emp2 Table

Example 1: SQL UNION Operator

In this example, we will find the cities (only unique values) from both the “Table1” and the
“Table2” tables:

Query:

SELECT Country FROM Emp1

UNION

SELECT Country FROM Emp2

ORDER BY Country;

Output:

2.INTERSECTION

the INTERSECT clause is used to retrieve the common records between two SELECT queries.

This makes INTERSECT an essential clause when we need to find overlapping data between
two or more queries.
Let’s consider two tables: the Customers table, which holds customer details, and
the Orders table, which contains information about customer purchases. By applying
the INTERSECToperator, we can retrieve customers who exist in both tables, meaning those
who have made purchases.

Customers Table

Customers Table

Orders Table

Orders Table

Example 1: Basic INTERSECT Query

In this example, we retrieve customers who exist in both the Customers and Orders tables.
The INTERSECT operator ensures that only those customers who have placed an order
appear in the result.
Query:

SELECT CustomerID
FROM Customers
INTERSECT
SELECT CustomerID
FROM Orders;

Output:

CustomerID

3.JOIN

Join is a combination of a Cartesian product followed by a selection process. A Join

condition is satisfied. It is denoted by ⋈.


operation combines related tuples from different relations, if and only if a given join

EMPLOYEE

EMP_ID EMP_NAME

101 Stephan

102 Jack
103 Harry

SALARY

EMP_ID SALARY

101 50000

102 30000

103 25000

1. Operation: (EMPLOYEE ⋈ SALARY)

Result:

EMP_CODE EMP_NAME SALARY

101 Stephan 50000

102 Jack 30000

103 Harry 25000

1.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

such that the attributes don’t have anything in common,

2.Natural Join (⋈)

Natural join does not use any comparison operator. It does not concatenate the way a
Cartesian product does.

Natural join acts on those matching attributes where the values of attributes in both the
relations are same.

3. Outer Joins
Therefore, we need to use outer joins to include all the tuples from the participating
relations in the resulting relation. There are three kinds of outer joins − left outer join,
right outer join,Full outer join

EMPLOYEE

EMP_NAME STREET CITY

Ram Civil line Mumbai

Shyam Park street Kolkata

Ravi M.G. Street Delhi

Hari Nehru nagar Hyderabad

FACT_WORKERS

EMP_NAME BRANCH SALARY

Ram Infosys 10000

Shyam Wipro 20000

Kuber HCL 30000

Hari TCS 50000

Input:

1. (EMPLOYEE ⋈ FACT_WORKERS)

Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000


Hari Nehru nagar Hyderabad TCS 50000

An outer join is basically of three types:

a. Left outer join

b. Right outer join

c. Full outer join

a. Left outer join:

o Left outer join contains the set of tuples of all combinations in R and S that are
equal on their common attribute names.

o In the left outer join, tuples in R have no matching tuples in S.

o It is denoted by ⟕.

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

1. EMPLOYEE ⟕ FACT_WORKERS

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000

Ravi M.G. Street Delhi NULL NULL

b. Right outer join:

o Right outer join contains the set of tuples of all combinations in R and S that are
equal on their common attribute names.

o In right outer join, tuples in S have no matching tuples in R.

o It is denoted by ⟖.
Example: Using the above EMPLOYEE table and FACT_WORKERS Relation

Input:

1. EMPLOYEE ⟖ FACT_WORKERS

Output:

EMP_NAME BRANCH SALARY STREET CITY

Ram Infosys 10000 Civil line Mumbai

Shyam Wipro 20000 Park street Kolkata

Hari TCS 50000 Nehru street Hyderabad

Kuber HCL 30000 NULL NULL

c. Full outer join:

o Full outer join is like a left or right join except that it contains all rows from both
tables.

o In full outer join, tuples in R that have no matching tuples in S and tuples in S that
have no matching tuples in R in their common attribute name.

o It is denoted by ⟗.

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

1. EMPLOYEE ⟗ FACT_WORKERS

Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000


Ravi M.G. Street Delhi NULL NULL

Kuber NULL NULL HCL 30000

Characteristics of DBMS
Some well-known characteristics are present in the DBMS (Database Management
System). These are explained below.

1. Real World Entity

o The reality of DBMS (Database Management System) is one of the most important
and easily understandable characteristics. The DBMS (Database Management
System) is developed in such a way that it can manage huge business organizations
and store their business data with security.

o The Database can store information such as the cost of vegetables, milk, bread, etc.
In DBMS (Database Management System), the entities look like real-world entities.

o A database management system is able to store any kind of data in a


database.
o The database management system has to support ACID (atomicity,
consistency, isolation, durability) properties.
o The Database management system allows so many users to access
databases at the same time.
o Backup and recovery are the two main methods which allow users to
protect the data from damage or loss.
o It also provides multiple views for different users in a single
organization.
o It follows the concept of normalization which is helpful to minimize the
redundancy of a relation.
o It also provides users query language, helpful to insert, retrieve,
update, and delete the data in a database.

Why it is called Relational Calculus?

Relational calculus in DBMS is a non-procedural query language that uses mathematical


predicate calculus to describe what data to retrieve from a relational database, without
specifying how to retrieve it, unlike procedural languages like relational algebra. It exists in
two forms: Tuple Relational Calculus (TRC) and Domain Relational Calculus (DRC).
 Tuple Relational Calculus (TRC):

TRC uses tuple variables to represent rows (tuples) in a relation and checks if a tuple
satisfies a given predicate.

 Domain Relational Calculus (DRC):

DRC uses domain variables to represent attribute values and checks if a combination of
attribute values satisfies a predicate.

Many of the calculus expressions involves the use of Quantifiers. There are two types of
quantifiers:

o Universal Quantifiers: The universal quantifier denoted by ∀ is read as for all which
means that in a given set of tuples exactly all tuples satisfy a given condition.

o Existential Quantifiers: The existential quantifier denoted by ∃ is read as for all


which means that in a given set of tuples there is at least one occurrences whose
value satisfy a given condition.

Physical Storage Media:


In the context of Database Management Systems (DBMS), "physical media access" refers
to how data is physically stored and accessed on storage devices, including details like
table layouts, data column names, indexes, and storage methods.

Here's a more detailed explanation:

 Physical Storage Media:

DBMS stores data on physical storage devices, such as main memory (RAM) and secondary
(external) storage (hard drives, SSDs).

 Key Aspects of Physical Media Access:

 Table Layouts: How tables are structured and organized on storage.

 Data Column Names: The names used to identify data columns.

 Indexes: Structures used to speed up data retrieval.

 Sizing Information: Details about the size of data (e.g., bytes per row, number
of rows).

 Data Storage Methods: How data is physically stored (e.g., sequential,


indexed).

 Types of Physical Storage Media:


 Cache: The fastest but most expensive storage (managed by the operating
system).

 Main Memory (RAM): Very fast but volatile storage (data is lost when power
is off).

 Secondary Storage (Hard Drives, SSDs): More persistent storage, but slower
than RAM.

What is Sequential File Organisation in DBMS?

Sequential File organization is the easiest type of file organization in which the
files are sequentially stored one after the other; rather than storing the various
records of the files in rows and column format(tabular form), it stores the records
in a single row.

Two methods can implement sequential file organization

1. Pile File method

2. Sorted File method

Pile File method

It is the easiest sequential file organization method in which the records are stored on a first
come basis, meaning whichever records come first would be stored first in the sequence.
There is no fixed sequence. In this method, the order in which the records come decides the
order in which they will be stored.

Sorted File Method

As the name suggests in the method, files are stored in some sorted format( ascending or
descending). Order can be defined by a primary key or any other key/attribute.

direct file organization


In DBMS, direct file organization, also known as hash file organization, uses a hash
function to map a record's key to a specific location in the file, enabling very fast retrieval
and insertion of data.

What is Hashing in DBMS?


The hashing technique uses a hash function to store data records in an auxiliary hash
table. There are three major components in hashing:
 Hash Table: The total number of data records in the database determines the size
of a hash table, which is an array or data structure. The precise location of a data
record is stored in each memory location in a hash table, which is referred to as a
“bucket” or hash index and is accessible via a hash function.

 Bucket: In the hash table where the data record is stored, a bucket is a memory
index. Typically, a disk block that holds numerous records is stored in these
buckets. Another name for it is the hash index.

 Hash Function: A hash function is an algorithm or mathematical equation that


computes the hash index as the output after receiving the main key of one data
record as input.

Types of Hashing in DBMS

There are two primary hashing techniques in DBMS:

 Static Hashing

 Dynamic Hashing

Static Hashing in DBMS

Static hashing in a Database Management System (DBMS) is a technique where the size
and structure of the hash table are fixed when it is created. Here are some key points
about static hashing:

 Fixed Number of Buckets: The number of data buckets remains constant


throughout. Each bucket is a storage location where records are stored.

 Hash Function: A hash function is used to map search-key values to bucket


addresses. For example, if the hash function is mod 5, it will always map a given
key to the same bucket address.

Dynamic Hashing in DBMS

Dynamic hashing is a technique used in DBMS that handles the limitations of static
hashing like bucket overflow. Here are the key aspects of dynamic hashing:

 Variable Number of Buckets: Unlike static hashing, the number of buckets in


dynamic hashing can grow or shrink based on the number of records.

 Directory: A directory is used to keep track of the buckets. The directory itself can
grow or shrink dynamically.
 Hash Function: The hash function generates a hash value, and the directory uses a
certain number of bits from this hash value to determine the bucket address.

 Bucket Splitting: When a bucket overflows, it is split into two, and the directory is
updated to reflect this change. This helps in distributing the records more evenly.

Convert ER Model to Relational Model

Er model to relational model


ER Model, when conceptualized into diagrams, gives a good overview of entity-
relationship, which is easier to understand. ER diagrams can be mapped to relational
schema, that is, it is possible to create relational schema using ER diagram. We cannot
import all the ER constraints into relational model, but an approximate schema can be
generated.

There are several processes and algorithms available to convert ER Diagrams into
Relational Schema. Some of them are automated and some of them are manual. We may
focus here on the mapping diagram contents to relational basics.

ER diagrams mainly comprise of −

 Entity and its attributes

 Relationship, which is association among entities.

Mapping Entity

An entity is a real-world object with some attributes.

Mapping Process (Algorithm)

 Create table for each entity.

 Entity's attributes should become fields of tables with their respective data types.
 Declare primary key.

Mapping Relationship

A relationship is an association among entities.

Mapping Process

 Create table for a relationship.

 Add the primary keys of all participating Entities as fields of table with their
respective data types.

 If relationship has any attribute, add each attribute as field of table.

 Declare a primary key composing all the primary keys of participating entities.

 Declare all foreign key constraints.

Mapping Weak Entity Sets

A weak entity set is one which does not have any primary key associated with it.

Mapping Process
 Create table for weak entity set.

 Add all its attributes to table as field.

 Add the primary key of identifying entity set.

 Declare all foreign key constraints.

Mapping Hierarchical Entities

ER specialization or generalization comes in the form of hierarchical entity sets.

Mapping Process

 Create tables for all higher-level entities.

 Create tables for lower-level entities.

 Add primary keys of higher-level entities in the table of lower-level entities.

 In lower-level tables, add all other attributes of lower-level entities.

 Declare primary key of higher-level table and the primary key for lower-level table.

 Declare foreign key constraints.


Examples of 1nf
nam state_cod
employee_id e job_code job e home_state

E001 Alice J01 Chef 26 Michigan

E001 Alice J02 Waiter 26 Michigan

E002 Bob J02 Waiter 56 Wyoming

E002 Bob J03 Bartender 56 Wyoming

E003 Alice J01 Chef 56 Wyoming

o the table is in the first normal form (1NF).

Example of Second Normal Form (2NF)

employee_roles Table

employee_i
d job_code

E001 J01

E001 J02

E002 J02

E002 J03

E003 J01

employees Table

state_cod
employee_id name e home_state

E001 Alice 26 Michigan


state_cod
employee_id name e home_state

E002 Bob 56 Wyoming

E003 Alice 56 Wyoming

jobs table

job_cod
e job

J01 Chef

J02 Waiter

J03 Bartender

home_state is now dependent on state_code. So, if you know the state_code, then you
can find the home_state value.

Example of Third Normal Form (3NF)

employee_roles Table

employee_i
d job_code

E001 J01

E001 J02

E002 J02

E002 J03

E003 J01

employees Table
employee_i
d name state_code

E001 Alice 26

E002 Bob 56

E003 Alice 56

jobs Table

job_cod
e job

J01 Chef

J02 Waiter

J03 Bartender

states Table

state_code home_state

26 Michigan

56 Wyoming

Now our database is in 3NF.

BCNF in DBMS Explained

DBMSDatabaseBig Data Analytics

BCNF (Boyce Codd Normal Form) is the advanced version of 3NF. A table is in BCNF if every
functional dependency X->Y, X is the super key of the table. For BCNF, the table should be in
3NF, and for every FD. LHS is super key.
Example

Consider a relation R with attributes (student, subject, teacher).

Student Teacher Subject

Jhansi P.Naresh Database

jhansi K.Das C

subbu P.Naresh Database

subbu R.Prasad C

F: { (student, Teacher) -> subject

(student, subject) -> Teacher

Teacher -> subject}

Candidate keys are (student, teacher) and (student, subject).

You might also like