RDBMS Unit-3
RDBMS Unit-3
RELATIONAL ALGEBRA
Relational algebra is a formal query language used to manipulate and
retrieve data stored in a relational database. It provides a set of operations
that can be applied to relations (tables) to produce desired results.
Relational algebra serves as the theoretical foundation for the Structured
Query Language (SQL) used in relational database management systems
(RDBMS). It's a way to express queries and transformations on data in a
precise and logical manner. Here are the fundamental operations of
relational algebra:
1. Selection (σ):
The selection operation filters rows from a relation based on a given
condition.
It's denoted by the Greek letter σ and involves specifying a predicate
that determines which rows to include in the result.
2. Projection (π):
Projection operation extracts specific columns from a relation while
discarding the rest.
It's denoted by the Greek letter π and involves specifying the
attributes (columns) to retain.
3. Union (∪):
The union operation combines the rows from two relations,
removing duplicate rows.
Both relations must have the same set of attributes.
4. Difference (−):
The difference operation returns the rows that are in one relation but
not in another.
Both relations must have the same set of attributes.
5. Cartesian Product (×):
The Cartesian product operation combines all rows from one relation
with all rows from another relation.
It results in a new relation with a combination of attributes from both
input relations.
6. Join (⨝):
The join operation combines rows from two relations based on a
common attribute.
Different types of joins include inner join, left outer join, right outer
join, and full outer join.
7. Intersection (∩):
The intersection operation returns the rows that are common to both
input relations.
Both relations must have the same set of attributes.
8. Division (÷):
The division operation is more complex and used less frequently.
It involves finding all values in one relation that, when combined,
match all values in another relation.
SYNTAX
Relational algebra provides a formal way to express database operations
and queries on relational data. Although it's used primarily in theoretical
discussions, it serves as the foundation for many aspects of SQL and
database query languages in RDBMS. Here's an overview of the syntax for
some common relational algebra operations:
1. Selection (σ):
Selection operation filters rows based on a condition.
Syntax: σ<condition>(relation)
2. Projection (π):
Projection operation extracts specific attributes (columns).
Syntax: π<attribute1, attribute2, ...>(relation)
3. Union (∪):
Union operation combines rows from two relations (tables).
Syntax: relation1 ∪ relation2
4. Difference (−):
Difference operation returns rows that are in one relation but not in
another.
Syntax: relation1 − relation2
5. Cartesian Product (×):
Cartesian product operation combines all rows from two relations.
Syntax: relation1 × relation2
6. Join (⨝):
Join operation combines rows from two relations based on a common
attribute.
Syntax: relation1 ⨝<condition> relation2
7. Intersection (∩):
Intersection operation returns common rows from two relations.
Syntax: relation1 ∩ relation2
8. Division (÷):
Division operation involves finding values in one relation that match
all values in another relation.
Syntax: relation1 ÷ relation2
It's important to note that these notations are typically used in theoretical
discussions or educational contexts. In practice, SQL provides a more
practical and expressive way to perform these operations in a relational
database.
In SQL, the corresponding operations have more intuitive syntax and are
closer to how you would express these operations in actual queries. For
example, the selection operation in SQL uses the WHERE clause, and the
projection operation uses the SELECT clause. Here's an example of how
SQL syntax for selection and projection might look:
sql
1. Syntax Semantics:
Syntax refers to the structure and grammar of statements or
expressions within a language.
Syntax semantics involve determining whether the syntax of a
program is valid and follows the rules of the language.
If the syntax is incorrect, the program or query may not even be
executed.
2. Operational Semantics:
Operational semantics describe how the statements or expressions in
a language are executed or evaluated step by step.
It defines the behavior of a program or query in terms of the effects
of each operation on the program state.
Operational semantics may involve specifying the order of execution,
handling of variables, control flow, and data manipulation.
SQL Semantics:
In SQL, the semantics of queries involve the interpretation of SQL
statements, such as SELECT, INSERT, UPDATE, and DELETE.
Semantics dictate how these statements interact with the database,
retrieve or modify data, and handle transactions.
Transaction Semantics:
In databases, transaction semantics ensure that database operations
follow the ACID properties (Atomicity, Consistency, Isolation,
Durability).
Transaction semantics ensure that a group of operations is executed
as a single unit of work, maintaining data integrity.
Query Optimization Semantics:
Query optimization involves determining the most efficient way to
execute a query to retrieve data from the database.
Query optimization semantics consider factors like indexing, join
strategies, and access paths to improve performance.
Concurrency Control Semantics:
In multi-user database environments, concurrency control semantics
ensure that multiple transactions can run concurrently without
interfering with each other.
Semantics define the rules for handling simultaneous access to the
same data by different transactions.
1. Aggregate Functions:
Aggregate functions (e.g., SUM, AVG, COUNT, MIN, MAX) perform
calculations on a set of values and return a single value.
They are used to summarize data and provide insights into the
dataset.
2. Group By:
The GROUP BY clause is used to group rows that have the same
values in specified columns.
It's often used in combination with aggregate functions to summarize
data for each group.
3. Having Clause:
The HAVING clause is used with the GROUP BY clause to filter the
results of aggregate functions based on specified conditions.
4. Subqueries:
Subqueries are queries nested within other queries. They can be used
to retrieve data for use in the main query.
Subqueries can be used in SELECT, FROM, WHERE, and other clauses.
5. Exists and Not Exists:
The EXISTS operator checks if a subquery returns any rows. It's often
used with correlated subqueries.
The NOT EXISTS operator is used to check for the absence of rows
returned by a subquery.
6. IN and NOT IN:
The IN operator checks if a value matches any value in a list or
subquery.
The NOT IN operator checks if a value does not match any value in a
list or subquery.
7. ANY and ALL:
The ANY operator compares a value to a set of values returned by a
subquery and returns true if the value matches any of the values.
The ALL operator compares a value to all values returned by a
subquery and returns true if the value matches all of the values.
8. Window Functions:
Window functions perform calculations across a set of table rows
that are related to the current row.
They are often used to calculate running totals, rankings, and other
analytical values.
9. Common Table Expressions (CTEs):
CTEs provide a way to define temporary result sets that can be
referenced within a query.
They improve query readability and can help break down complex
queries into more manageable parts.
10. Recursive Queries:
Some RDBMSs support recursive queries, which allow you to
perform operations on hierarchical or recursive data structures.
11. Stored Procedures and User-Defined Functions:
Stored procedures and user-defined functions allow you to
encapsulate complex operations and reuse them within SQL queries.
RELATIONAL CALCULUS
Relational calculus is a non-procedural query language used to retrieve
data from relational databases. It provides a high-level, declarative
approach to specifying queries without describing the specific steps for
retrieving the data. There are two main forms of relational calculus: Tuple
Relational Calculus (TRC) and Domain Relational Calculus (DRC). Let's
explore these concepts further:
TRC uses quantifiers (∃ for existential and ∀ for universal) along with
logical operators to express queries. Here's a basic overview of how TRC
works and its syntax:
1. Basic Syntax:
A TRC query is written as an expression that specifies the conditions
for the desired tuples.
You use quantifiers (∃, ∀) to define variables that represent rows and
express the conditions.
Attributes of tuples are referenced using the dot notation
(relation.attribute).
2. Existential Quantifier (∃):
The existential quantifier (∃) specifies that at least one tuple exists
that satisfies the given conditions.
It is used to retrieve tuples that match a certain criterion.
Example: Retrieve the names of employees who work in the 'Sales'
department.
lessCopy code
{ t.FirstName , t.LastName | ∃ d ( EMPLOYEE (t) ∧ DEPARTMENT (d) ∧
t.DepartmentID = d.DepartmentID ∧ d.DepartmentName = 'Sales' )}
3. Universal Quantifier (∀):
The universal quantifier (∀) specifies that all tuples satisfy the given
conditions.
It is used to express constraints that apply to all tuples.
Example: Retrieve the names of departments where all employees
have a salary greater than $50000.
scssCopy code
{d .DepartmentName | ∀e (EMPLOYEE(e) ∧ DEPARTMENT (d) ∧
e .DepartmentID = d .DepartmentID ∧ e .Salary > 50000 )}
4. Logical Operators:
TRC expressions can use logical operators such as AND, OR, NOT to
combine conditions.
Parentheses are used to group conditions for clarity.
It's important to note that while TRC provides a theoretical foundation for
expressing queries, it is less commonly used in practice compared to SQL.
SQL offers a more practical and user-friendly way to interact with
relational databases due to its procedural nature and widespread adoption.
CALCULUS Vs ALGEBRA
Relational Calculus and Relational Algebra are two different approaches to
querying and manipulating data in Relational Database Management
Systems (RDBMS). Both approaches are used to express queries and
operations on relational databases, but they have distinct characteristics
and use cases. Let's compare Relational Calculus and Relational Algebra:
Relational Calculus:
1. Nature:
Relational Calculus is a non-procedural language.
It describes what needs to be retrieved rather than how to retrieve it.
2. Focus:
Relational Calculus focuses on specifying the conditions that data
must satisfy to be retrieved.
It emphasizes logical predicates and constraints.
3. Expressiveness:
It is more expressive and can represent complex conditions and
constraints elegantly.
4. Use Cases:
Relational Calculus is used more in theoretical discussions, formal
database theory, and understanding the principles of query
languages.
It is less commonly used in practical applications compared to
Relational Algebra.
5. Examples:
Tuple Relational Calculus (TRC) and Domain Relational Calculus
(DRC) are two forms of Relational Calculus.
Relational Algebra:
1. Nature:
Relational Algebra is a procedural language.
It describes how to perform operations step by step to retrieve or
manipulate data.
2. Focus:
Relational Algebra focuses on specifying operations and
transformations to be performed on relations.
3. Expressiveness:
It is less expressive than Relational Calculus for expressing complex
logical conditions.
4. Use Cases:
Relational Algebra is commonly used in practical scenarios for
writing database queries and performing data manipulation tasks.
It is the foundation for most query languages, including SQL.
5. Examples:
Basic operations of Relational Algebra include selection, projection,
union, difference, cartesian product, join, intersection, and division.
Comparison:
DOMAIN CALCULUS
"Domain calculus" is not a widely recognized term in the context of
relational databases or query languages. It's possible that there might be a
misunderstanding or miscommunication. However, if you're referring to
the concept of "Domain Relational Calculus" (DRC) within the context of
relational databases, I can provide information on that.
Example DRC query: "Retrieve the names of employees who work in the
'Sales' department."
scss
1. Data Retrieval:
SELECT Statement: The core of SQL, used to retrieve data from one
or more tables.
JOIN: Combines data from multiple tables based on related columns.
Subqueries: Nested queries that retrieve data from another query's
result.
2. Data Modification:
INSERT: Adds new rows of data into a table.
UPDATE: Modifies existing data in a table.
DELETE: Removes rows from a table.
3. Data Definition Language (DDL):
CREATE: Creates new database objects such as tables, indexes, and
views.
ALTER: Modifies existing database objects, such as adding or
dropping columns from a table.
DROP: Deletes database objects like tables or indexes.
4. Constraints:
PRIMARY KEY: Enforces uniqueness and ensures each row has a
unique identifier.
FOREIGN KEY: Establishes relationships between tables.
CHECK: Defines conditions that must be met before data is inserted
or updated.
UNIQUE: Ensures values in a column are unique.
5. Indexes:
CREATE INDEX: Improves query performance by providing faster
access to data.
6. Views:
CREATE VIEW: Creates a virtual table based on the result of a query,
simplifying complex queries.
7. Transactions:
BEGIN TRANSACTION: Starts a transaction.
COMMIT: Saves changes to the database.
ROLLBACK: Reverts changes made during the current transaction.
8. Aggregation:
SUM, AVG, COUNT, MIN, MAX: Aggregate functions used to perform
calculations on groups of data.
9. Window Functions:
ROW_NUMBER, RANK, DENSE_RANK: Perform calculations across a
set of table rows related to the current row.
10. String and Date Functions:
Functions for manipulating strings and working with date and time
data.
11. Stored Procedures and Functions:
Custom code blocks that can be executed from SQL.
12. User-Defined Types and Functions:
Define custom data types and functions in the database.
13. Full-Text Search:
Specialized search techniques for searching text-based data.
14. JSON and XML Support:
Support for storing, querying, and manipulating JSON and XML data.
15. Security and Authorization:
GRANT: Provides specific privileges to users or roles.
REVOKE: Removes privileges from users or roles.
16. Analytical Functions:
Functions for performing complex analytics, such as ranking,
lead/lag, and percentiles.