Understanding Relational Algebra Basics
Understanding Relational Algebra Basics
Relational algebra consists of a certain set of rules or operations that are widely
used to manipulate and query data from a relational database. It can be facilitated
by utilizing SQL language and helps users interact with database tables based on
querying data from the database more efficiently and effectively.
Relational Algebra incorporates a collection of operations, such as filtering data
or combining data, that help us organize and manipulate data more efficiently.
This ” algebra ” is the foundation for most database queries, and it enables us to
extract the required information from the databases by using SQL query
language.
Fundamental Operators
Relational algebra consists of various operators that help us fetch and manipulate
data from relational tables in the database to perform certain operations on
relational data.
These are the basic/fundamental operators used in Relational Algebra
1. Selection(σ)
2. Projection(π)
3. Union(U)
4. Set Difference(-)
5. Set Intersection( )
6. Rename(ρ)
7. Cartesian Product(X)
8.
1. Selection(σ) : Selection Operation is basically used to filter out rows from a
given table based on certain given condition. It basically allows you to retrieve
only those rows that match the condition as per condition passed during SQL
Query.
It is used to select required tuples of the relations.
Illustration : If we want to get the details of all the work in the Department
“IT”, we would use the selection operator to filter out these based on the given
condition.
Example:
A B C
1 2 4
A B C
2 2 3
3 2 3
4 3 4
Output Table :
For the above relation, σ(c>3)R will select the tuples which have c more than 3.
A B C
1 2 4
4 3 4
Note: The selection operator only selects the required tuples but does not display them.
For display, the data projection operator is used.
B C
B C
2 4
2 3
3 4
3. Union(U) : Union Operator is basically used to combine the results of two queries
into a single result. The only condition is that both queries must return same number of
columns with same data types.
Illustration : If in case we want a list of all the employee from two different
department . Then in that case we should use union operation to merge both the list
from two different department.
Union operation in relational algebra is the same as union operation in set theory.
Example: FRENCH
Student_Name Roll_Number
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
Consider the following table of Students having different optional subjects in their
course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Vivek
Geeta
Shyam
Student_Name
Rohan
Note: The only constraint in the union of two relations is that both relations must have
the same set of Attributes.
4. Set Difference(-) : Set difference basically provides the rows that are present in one
table , but not in another tables.
Illustration : If you have two lists of employees, one from the IT Department and one
from the Marketing department & you want to know which employees are in IT but not
in Marketing , the set difference operator would used.
Set Difference in relational algebra is the same set difference operation as in set
theory.
Example: From the above table of FRENCH and GERMAN, Set Difference is used as
follows
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Note: The only constraint in the Set Difference between two relations is that both
relations must have the same set of Attributes.
5. Set Intersection( ) : Set Intersection basically allows to fetches only those rows of
data that are common between two sets of relational tables.
Illustration : If we want to find the number of employees who work in both IT and
Marketing Department , then in that case we use Intersection Operator.
Set Intersection in relational algebra is the same set intersection operation in set theory.
Example: From the above table of FRENCH and GERMAN, the Set Intersection is
used as follows:
π(Student_Name)FRENCH π(Student_Name)GERMAN
Student_Name
Vivek
Geeta
Note: The only constraint in the Set Difference between two relations is that both
relations must have the same set of Attributes.
6. Rename(ρ) : Rename operator basically allows you to give a temporary name to a
specific relational table or to its columns. It is very useful when we want to avoid
ambiguity, especially in complex Queries.
Rename is a unary operation used for renaming attributes of a relation.
ρ(a/b)R will rename the attribute 'b' of the relation by 'a'.
7. Cross Product(X) : Cartesian product Operator combines every row of one table
with every row of another table , producing all the possible combination. It’s mostly
used as a precursor to more complex operation like joins.
Cross-product between two relations. Let’s say A and B, so the cross product between
A X B will result in all the attributes of A followed by each attribute of B. Each record
of A will pair with every record of B.
Example:
A
Ram 14 M
Sona 15 F
Kim 20 M
B
ID Course
1 DS
2 DBMS
A X B
Notation:
For example:
For example:
Notation:
For example:
Relational Calculus is a
It is a Procedural
Language Type Declarative (non-procedural)
language.
language.
The completeness of a
language is measured in the
The expressiveness of manner that it is least as
any given language is powerful as calculus. That
Expressiveness judged using relational implies relation defined using
algebra operations as a some expression of the
standard. calculus is also definable by
some other expression, the
language is in question.
Sql constraints
SQL constraints are essential elements in relational database design that ensure
the integrity, accuracy, and reliability of the data stored in a database. By enforcing
specific rules on table columns, SQL constraints help maintain data consistency,
preventing invalid data entries and optimizing query performance.
In this article, we will explain the most common SQL constraints in detail, providing
clear examples and explaining how to implement them effectively.
What Are SQL Constraints?
SQL constraints are rules applied to columns or tables in a relational database to
limit the type of data that can be inserted, updated, or deleted. These rules ensure the
data is valid, consistent, and adheres to the business logic or database requirements.
Constraints can be enforced during table creation or later using the ALTER
TABLE statement. They play a vital role in maintaining the quality and integrity of
your database.
2. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are distinct across all rows
in a table. Unlike the PRIMARY KEY, which requires uniqueness and does not allow
NULLs, the UNIQUE constraint allows NULL values but still enforces uniqueness for
non-NULL entries.
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20)
);
Explanation: Here, the ID column must have unique values, ensuring that no two
students can share the same ID. We can have more than one UNIQUE constraint in a
table.
1 2253 3
2 3325 3
O_ID ORDER_NO C_ID
3 4521 2
4 8532 1
Customers Table:
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON
As we can see clearly that the field C_ID in Orders table is the primary key in
Customers table, i.e. it uniquely identifies each row in the Customers table. Therefore,
it is a Foreign Key in Orders table.
Example:
CREATE TABLE Orders
(
O_ID int NOT NULL,
ORDER_NO int NOT NULL,
C_ID int,
PRIMARY KEY (O_ID),
FOREIGN KEY (C_ID) REFERENCES Customers(C_ID)
)
Explanation: In this example, the C_ID column in the Orders table is a foreign key
that references the C_ID column in the Customers table. This ensures that only valid
customer IDs can be inserted into the Orders table.
5. CHECK Constraint
The CHECK constraint allows us to specify a condition that data must satisfy before it
is inserted into the table. This can be used to enforce rules, such as ensuring that a
column’s value meets certain criteria (e.g., age must be greater than 18)
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18)
);
Explanation: In the above table, the CHECK constraint ensures that only students
aged 18 or older can be inserted into the table.
6. DEFAULT Constraint
The DEFAULT constraint provides a default value for a column when no value is
specified during insertion. This is useful for ensuring that certain columns always have
a meaningful value, even if the user does not provide one
Example:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int DEFAULT 18
);
Unit - 3
SQL: QUERIES, CONSTRAINTS, TRIGGERS: form of basic SQL query, UNION,
INTERSECT, and EXCEPT, Nested Queries, aggregation operators, NULL values, complex
integrity constraints in SQL, triggers and active databases.
The basic form of an SQL query, specifically when retrieving data, is composed of a combination
of clauses. The most elementary form of an SQL query for data retrieval can be represented as
Syntax
SELECT [DISTINCT] column1, column2, ...
FROM tablename
WHERE condition;
Let's break it down:
1. SELECT Clause: This is where you specify the columns you want to retrieve. Use an
asterisk (*) to retrieve all columns.
2. FROM Clause: This specifies from which table or tables you want to retrieve the data.
3. WHERE Clause (optional): This allows you to filter the results based on a condition.
4. DISTINCT Clause (optional): is an optional keyword indicating that the answer should
not contain duplicates. Normally if we write the SQL without DISTINCT operator then it
does not eliminate the duplicates.
To provide a more holistic view, here are a few more SQL examples, keeping them as basic as
possible:
Syntax
SELECT * FROM tablename;
Syntax
SELECT column1, column2 FROM tablename;
Syntax
SELECT column1, column2 FROM tablename WHERE column1 = 'value';
Regular expressions: is a sequence of characters that define a search pattern, mainly for use in
pattern matching with strings, or string matching.
Examples:
Finds Names that start or ends with "a“
Finds names that start with "a" and are at least 3 characters in length.
LIKE: The LIKE operator is used in a 'WHERE' clause to search for a specified pattern in a
column
wild-card: There are two primary wildcards used in conjunction with the `LIKE` operator
percent sign (%) Represents zero, one, or multiple characters
underscore sign(_) Represents a single character
Here's a breakdown of how you can use these wildcards with the `LIKE` operator:
Example
SELECT FirstName
FROM Customers
WHERE FirstName LIKE 'Ma%';
2. Find values that end with a specific pattern:
Syntax
SELECT column_name
FROM table_name
WHERE column_name LIKE '%pattern';
Example, to find all books that have the word "life" anywhere in the title:
Example
SELECT BookTitle
FROM Books
WHERE BookTitle LIKE '%life%';
Using `_` Wildcard
1. Find values of a specific length where you only know some characters:
Syntax
SELECT column_name
FROM table_name
WHERE column_name LIKE 'p_ttern';
For instance, if we're looking for a five-letter word where you know the first letter is "h" and the
third letter is "l", you could use:
Example
SELECT Word
FROM Words
WHERE Word LIKE 'h_l__';
Combining `%` and `_`
We can use both wildcards in the same pattern. For example, to find any value that starts with
"A", followed by two characters, and then "o":
Example
SELECT column_name
FROM table_name
WHERE column_name LIKE 'A__o%';
Keep in mind that the actual symbols used for wildcards might vary depending on the database
system. For example, in SQL Server, the wildcard for a single character is `[?]` instead of `_`.
Always refer to the specific documentation of the database you're working with.
***************************************************************************
The SQL Set operation is used to combine the two or more SQL SELECT statements.
1. Union
2. Union All
3. Intersect
4. Minus/Except
1. Union
The SQL Union operation is used to combine the result of two or more SQL SELECT
queries.
In the union operation, all the number of data type and columns must be the same in both
the tables on which the UNION operation is being applied.
The union operation eliminates the duplicate rows from its result set.
Syntax
UNION
Example:
ID NAME
1 Jack
2 Harry
3 Jackson
ID NAME
3 Jackson
4 Stephan
5 David
ID NAME
1 Jack
Harry
3 Jackson
44Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing duplication
and sorting the data.
Syntax:
UNION ALL
UNION ALL
ID NAME
1 Jack
Harry
3 Jackson
33Jackson
4 Stephan
David
3. Intersect
It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
In the Intersect operation, the number of data types and columns must be the same.
It has no duplicates and it arranges the data in ascending order by default.
Syntax
INTERSECT
Example:
INTERSECT
ID NAME
3 Jackson
4. Minus/Except
It combines the result of two SELECT statements. Except operator is used to display the
rows which are present in the first query but absent in the second query.
Syntax:
Except
Example
Except
ID NAME
1 Jack
Harry
******************************************************************************
In independent nested queries, the execution order is from the innermost query to the outer query.
An outer query won't be executed until its inner query completes its execution. The outer query
uses the result of the inner query. Operators such as IN, NOT IN, ALL, and ANY are used to write
independent nested queries.
The IN operator checks if a column value in the outer query's result is present in the inner
query's result. The final result will have rows that satisfy the IN condition.
The NOT IN operator checks if a column value in the outer query's result is not present in
the inner query's result. The final result will have rows that satisfy the NOT IN condition.
The ALL operator compares a value of the outer query's result with all the values of the
inner query's result and returns the row if it matches all the values.
The ANY operator compares a value of the outer query's result with all the inner query's
result values and returns the row if there is a match with any value.
Co-related Nested Queries
In co-related nested queries, the inner query uses the values from the outer query to execute the
inner query for every row processed by the outer query. The co-related nested queries run slowly
because the inner query is executed for every row of the outer query's result.
We can write a nested query in SQL by nesting a SELECT statement within another SELECT
statement. The outer SELECT statement uses the result of the inner SELECT statement for
processing.
The SELECT query inside the brackets () is the inner query, and the SELECT query outside the
brackets is the outer query. The outer query uses the result of the inner query.
We will use the Employees and Awards table below to understand independent and co-related
nested queries. We will be using Oracle SQL syntax in our queries.
Let's create the Employees and Awards tables:
CREATE TABLE employee (id NUMBER PRIMARY KEY, name VARCHAR2(100) NOT
NULL, salary NUMBER NOT NULL, role VARCHAR2(100) NOT NULL );
CREATE TABLE awards (id NUMBER PRIMARY KEY, employee_id NUMBER NOT NULL,
award_date DATE NOT NULL);
Employees
Awards
Example 1: IN
Output
id name
2 Perice Mundford
4 Garwood Saffen
5 Faydra Beaves
Example 2: NOT IN
Output
id name
1 Augustine Hammond
3 Cassy Delafoy
Example 3: ALL
Select all Developers who earn more than all the Managers
AND salary > ALL (SELECT salary FROM employees WHERE role = 'Manager');
Output
Explanation
The developer with id 5 earns (50000) more than all the managers: 2 (10000) and 4 (40000)
Example 4: ANY
AND salary > ANY (SELECT salary FROM employees WHERE role = 'Manager');
Output
Explanation
The developer with id 3 earns (30000) more than the manager with id 2 (10000)
The developer with id 5 earns (50000) more than the managers with id 2 (10000) and 4
(40000)
Select all employees whose salary is above the average salary of employees in their role.
WHERE salary > (SELECT AVG(salary) FROM employees emp2 WHERE [Link] =
[Link]);
Output
Explanation
The manager with id 4 earns more than the average salary of all managers (25000), and the
developer with id 5 earns more than the average salary of all developers (30000). The inner query
is executed for all rows fetched by the outer query. The inner query uses the role value ([Link])
of every outer query's row ([Link] = [Link]).
We can find the average salary of managers and developers using the below query:
role avg(salary)
Developer 30000
Manager 25000
************************************************************************
SQL aggregation function is used to perform the calculations on multiple rows of a single
column of a table. It returns a single value.
The COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
Syntax
1. COUNT(*)
2. or
3. COUNT([ALL|DISTINCT] expression)
Sample table:
PRODUCT_MAST
Example: COUNT()
SELECT COUNT(*)
FROM PRODUCT_MAST;
Output:
10
SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;
Output:
7
FROM PRODUCT_MAST;
Output:
3
FROM PRODUCT_MAST
GROUP BY COMPANY;
Output:
Com1 5
Com2 3
Com3 2
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output:
Com1 5
Com2 3
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.
Syntax
SUM()
or
SUM([ALL|DISTINCT] expression)
Example: SUM()
SELECT SUM(COST)
FROM PRODUCT_MAST;
Output:
670
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
Output:
320
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3
GROUP BY COMPANY;
Output:
Com1 150
Com2 170
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING SUM(COST)>=170;
Output:
Com1 335
Com3 170
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function returns
the average of all non-Null values.
Syntax
AVG()
or
AVG([ALL|DISTINCT] expression)
Example:
SELECT AVG(COST)
FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.
Syntax
MAX()
or
MAX([ALL|DISTINCT] expression)
Example:
SELECT MAX(RATE)
FROM PRODUCT_MAST;
30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function determines the
smallest value of all selected values of a column.
Syntax
MIN()
or
MIN([ALL|DISTINCT] expression)
Example:
SELECT MIN(RATE)
FROM PRODUCT_MAST;
Output:
10
****************************************************************************
NULL values in SQL
In SQL there may be some records in a table that do not have values or data for every field and
those fields are termed as a NULL value.
NULL values could be possible because at the time of data entry information is not available. So
SQL supports a special value known as NULL which is used to represent the values of attributes
that may be unknown or not apply to a tuple. SQL places a NULL value in the field in the absence
of a user-defined value.
So, NULL values are those values in which there is no data value in the particular field in the
table.
A function in SQL is a stored program that can accept inputs, perform operations, and
return a value. It is similar to a procedure, but a function must return a value, and it can
be used within queries like any other expression.
Key Points:
Syntax
Example of Function
Usage a function
SELECT [Link]();
2. Procedures in SQL
A procedure (also called a stored procedure) is a set of SQL statements that can be
executed as a unit. Procedures can take parameters, perform operations like insert,
update, delete, and select, and return multiple results (but not directly a single value like
functions).
Key Points:
Procedures do not necessarily return a value, but they may return multiple result
sets.
They can perform multiple operations.
Syntax:
Example of Procedure
CREATE PROCEDURE GetAllStudents
AS
BEGIN
SELECT * FROM Students;
END;
Executing a Procedure::
EXEC GetAllStudents;
SQL | Triggers
Trigger is a statement that a system executes automatically when there is any
modification to the database. In a trigger, we first specify when the trigger is to
be executed and then the action to be performed when the trigger executes.
Triggers are used to specify certain integrity constraints and referential
constraints that cannot be specified using the constraint mechanism of SQL.
Example –
Suppose, we are adding a tuple to the ‘Donors’ table that is some person has
donated blood. So, we can design a trigger that will automatically add the value
of donated blood to the ‘Blood_record’ table.
Types of Triggers –
We can define 6 types of triggers for each table:
1. AFTER INSERT activated after data is inserted into the table.