VIEW and Relational Model
VIEW and Relational Model
Example:
CREATE VIEW employee_view AS
SELECT first_name, last_name, department
FROM employees
WHERE department = 'HR';
In this example, employee_view will display only employees from the HR department.
Using a View
You can query a view just like a table:
SELECT * FROM employee_view;
Dropping a View
You can remove a view using:
DROP VIEW view_name;
Table Student
ROLL_NO NAME ADDRESS PHONE AGE
4 SURESH DELHI 18
Important Terminologies
• Attribute: Attributes are the properties that define an entity. e.g.; ROLL_NO, NAME,
ADDRESS
• Relation Schema: A relation schema defines the structure of the relation and represents the
name of the relation with its attributes.
e.g.; STUDENT (ROLL_NO, NAME, ADDRESS, PHONE, and AGE) is the relation schema
for STUDENT.
If a schema has more than 1 relation, it is called Relational Schema.
• Tuple: Each row in the relation is known as a tuple. The above relation contains 4 tuples, one
of which is shown as:
• Relation Instance: The set of tuples of a relation at a particular instance of time is called a
relation instance. Table 1 shows the relation instance of STUDENT at a particular time. It can
change whenever there is an insertion, deletion, or update in the database.
• Degree: The number of attributes in the relation is known as the degree of the relation.
The STUDENT relation defined above has degree 5.
• Cardinality: The number of tuples in a relation is known as cardinality.
The STUDENT relation defined above has cardinality 4.
• NULL Values: The value which is not known or unavailable is called a NULL value. It is
represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
Database Languages
Query languages: a query language is a language in which a user request information from the
database. Query languages can be categorized as either procedural or non procedural.
Relational Algebra
• Relational Algebra is a procedural query language. Relational algebra mainly provides a
theoretical foundation for relational databases and SQL.
• The main purpose of using Relational Algebra is to define operators that transform one or more
input relations into an output relation.
• Relational Algebra is a set of basic operations used to manipulate the data in relational model.
• The fundamental operations of relational algebra are as follows:
σcondition(R)
Where:
• σ is the select operator.
• condition is a predicate that must be satisfied by the tuples. which may use connectors like
AND(˄), OR(˅), NOT(¬ )
these terms may use relational operators like =,<,>,<=,>= etc.
Example1:
If you want to select employees from the "Sales" department, the SELECT operation would be:
σ Department = ’Sales’(Employees)
The result would be:
Operation:
σAge>30(Employees)
Syntax
ᴨ attribute1,attribute2,…(Relation)
Where:
• ᴨ is the project operator.
• attribute1, attribute2, ... are the attributes (columns) to be projected.
• Relation is the original relation.
It selects columns (attributes) rather than rows.
Example
Consider the following Employees relation:
πName,Department(Employees)
This renames the relation Employees to Staff but keeps the same attributes.
RENAMING AN ATTRIBUTE:
The syntax
ρ(X/A)(R)
in relational algebra is used to rename an attribute.
• ρ: The rename operator.
• X/A: This part specifies that attribute A is being renamed to X.
• R: The original relation where the renaming is happening.
Example:
Let’s say you have a relation Employee(EmpID, Name, Salary), and you want to rename EmpID to
EmployeeID.
ρ(EmployeeID/EmpID) (Employee)
This operation results in a new relation where EmpID is renamed to EmployeeID.
UNION Operation:
• The result contains all tuples from both Employees1 and Employees2.
• The duplicate tuple (3, Bob, 25, Sales) appears in both relations, but is included only once in
the union result.
Example2:
Let's create an example of a UNION operation that combines two relations using the PROJECTION
operation in relational algebra. The PROJECTION operator (denoted as π) selects specific attributes
(columns) from a relation.
We have two relations, Employees1 and Employees2, with the following structure:
We can use PROJECTION to select only the Name and Age from both relations and then perform the UNION:
Syntax:
Where:
• R₁ and R₂ are two relations that must have the same number of attributes, and the
corresponding attributes must have the same domains (data types).
• ∩ represents the intersection operator.
For an INTERSECTION operation to be valid in relational algebra, the following conditions must
be met:
1. Union Compatibility:
o The two relations (R₁ and R₂) must be union-compatible.
o This means that both relations must have the same number of attributes.
o The corresponding attributes (columns) in both relations must have the same data types
or domains. For example, if the first attribute in R₁ is of type INTEGER, the first
attribute in R₂ must also be of type INTEGER.
2. Same Attribute Order:
o The attributes in both relations must be in the same order. For example, if the first
column in R₁ represents an employee's ID, the first column in R₂ should also represent
the employee's ID.
3. Distinct Tuples:
o Since relational algebra operates on sets, the resulting tuples from the intersection will
be distinct, meaning there will be no duplicate rows in the result set.
Example1:
INTERSECTION Operation:
• Only the tuple (3, Bob, 25, Sales) appears in both Employees1 and Employees2, so it is the
only result of the intersection.
Example2:
We can use PROJECTION to select only the Name and Age from both relations and then perform the INTERSECTION:
R₁ − R₂
Where:
• R₁ and R₂ are two relations that must have the same number of attributes (they are union-
compatible).
• The result will contain only the tuples that are present in R₁ but not in R₂.
Example:
Example2:
We want to find the employees (based on Name and Age) that are in Employees1 but not in Employees2.
We can use PROJECTION to select only the Name and Age from both relations and then perform the SET
DIFFERENCE:
CARTESIAN PRODUCT
The Cartesian Product (also called the Cross Product) in relational algebra is an operation that
combines two relations (tables) to form a new relation. It returns all possible combinations of rows
from both relations.
Syntax:
If R and S are two relations, the Cartesian product is written as:
R×S
Characteristics:
• The result of R × S contains every row from R paired with every row from S.
• If R has n tuples (rows) and S has m tuples, the result will have n * m tuples.
• The new relation will have all the attributes (columns) from both relations.
• If the two relations have attributes with the same names, they will be distinguished by
qualifying them with the relation names (e.g., R.A and S.A).
Example:
Let’s say we have two tables, Employees and Departments.
If we perform a Cartesian product between Employees and Departments:
Employees × Departments
JOIN operation
The JOIN operation in relational algebra combines related tuples from two relations based on a
common attribute. It is one of the most powerful and commonly used operations in relational
databases.
There are different types of JOIN operations in relational algebra, including:
1. Theta Join (θ-join)
2. Equi-Join
3. Natural Join
4. Outer Join (Left, Right, Full)
R1 ⋈θ R2
• R1 and R2 are the two relations.
• θ is the condition (e.g., R1.A = R2.B).
Example:
2. Equi-Join:
An equi-join is a special case of the theta join where the condition only involves equality (=).
Syntax:
R1 ⋈ (R1.A = R2.B) R2
The example provided above (Employees and Departments) is also an equi-join since the condition is
Employees.DeptID = Departments.DeptID.
3. Natural Join:
A natural join is a specific type of equi-join where the join is performed on attributes that have the same name in both
relations. It automatically matches the common attributes and removes duplicate columns in the result.
R1 ⋈ R2
If two relations have common attributes with the same name, the natural join will combine them based on those
attributes.
Example:
Using the same Employees and Departments relations, we can write a natural join as:
Employees ⋈ Departments
This operation will automatically join based on the DeptID attribute (because it has the same name in both relations),
and the result will be:
4. Outer Join:
An outer join keeps tuples that do not have a match in one of the relations.
• Left Outer Join: Keeps all tuples from the left relation, adding NULL for tuples that don’t
have a match in the right relation.
• Right Outer Join: Keeps all tuples from the right relation, adding NULL for tuples that
don’t have a match in the left relation.
• Full Outer Join: Keeps all tuples from both relations, adding NULL wherever there is no
match.
Syntax:
• Left Outer Join: R1 ⟕ R2
• Right Outer Join: R1 ⟖ R2
• Full Outer Join: R1 ⟗ R2
Left Outer Join:
In a Left Outer Join, all tuples from the left relation (R1) are included, and any tuples from R2 that do
not match the ID in R1 are left as NULL in the result.
R1 ⟕ R2
Explanation:
• ID 1 (Alice) does not have a matching ID in R2, so the Age is NULL.
• ID 2 (Bob) and ID 3 (Carol) have matches in R2, so the join includes both their Name and Age.
• All rows from R1 are present, even if they don't have a match in R2.
R1 ⟖ R2
Explanation:
• ID 4 (Age 35) in R2 does not have a match in R1, so the Name is NULL.
• ID 2 (Bob) and ID 3 (Carol) have matches in R1, so both their Name and Age are shown.
• All rows from R2 are present, even if they don't have a match in R1.
R1 ⟗ R2
Explanation:
• ID 1 (Alice) from R1 does not have a match in R2, so the Age is NULL.
• ID 4 (Age 35) from R2 does not have a match in R1, so the Name is NULL.
• ID 2 (Bob) and ID 3 (Carol) have matches in both R1 and R2, so both their Name and Age are
shown.
• All rows from both R1 and R2 are present in the result.
Summary:
• Left Outer Join (R1 ⟕ R2): Includes all rows from R1, even if there is no match in R2.
• Right Outer Join (R1 ⟖ R2): Includes all rows from R2, even if there is no match in R1.
• Full Outer Join (R1 ⟗ R2): Includes all rows from both R1 and R2, filling missing values with
NULL where there is no match.
RELATIONAL CALCULUS IN DBMS:
Relational Calculus in a Database Management System (DBMS) is a non-procedural query language that
allows users to describe what they want from the database, without specifying how to compute it. It focuses
on defining the desired results rather than detailing the step-by-step procedure to retrieve them, in contrast
to procedural languages like relational algebra.
There are two types of relational calculus:
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus (DRC)
1. Tuple Relational Calculus (TRC):
• In Tuple Relational Calculus (TRC), queries are expressed by selecting tuples (rows) that satisfy a
certain condition.
• TRC uses variables that represent tuples from a relation.
• The result is a set of tuples that match the given condition.
TRC Syntax:
Where:
• T is a tuple variable.
• P(T) is a predicate (a condition or formula) that is applied to the tuple T.
COMMON NOTATIONS USED IN RELATIONAL CALCULUS:
Example
{ T | T ∈ Employee }
• { T | ... }: This is the set notation in relational calculus, which defines a set of tuples T. The
result of this expression will be a set of tuples.
• T: This is a tuple variable that represents a row or tuple from the relation (table) Employee.
• T ∈ Employee: This means that the tuple T belongs to or is a member of the Employee relation
(table). In other words, the tuple T is taken from the set of all rows in the Employee table.
The expression { T | T ∈ Employee } simply means:
• Retrieve all tuples T from the Employee relation.
This query returns every row (tuple) in the Employee table.
Example:
Assume the Employee table looks like this:
Let’s consider a relation Employee with attributes (ID, Name, Age, Department):
Syntax: