0% found this document useful (0 votes)
12 views

VIEW and Relational Model

This is a note of DBMS subject it includes all notes and information for DBMS and its based on Kannur university THIRD semester

Uploaded by

freefire5667778
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

VIEW and Relational Model

This is a note of DBMS subject it includes all notes and information for DBMS and its based on Kannur university THIRD semester

Uploaded by

freefire5667778
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

SQL VIEWS

• In SQL, a view is a virtual table based on the result-set of an SQL statement.


• A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.
• We can create a view by selecting fields from one or more table present in the database.
• A view can either have all the rows of a table or specific row based on certain condition.
• A view is created with the CREATE VIEW statement.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

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;

Relational Model in DBMS


• E.F. Codd proposed the relational Model to model data in the form of relations or tables.
• The relational model uses a collection of tables to represent both data and the relationships among
those data.
• Each table has multiple columns, and each column has a unique name.
• Tables are also known as relations.
• The relational model is an example of a record-based model.
• Record-based models are so named because the database is structured in fixed-format records of
several types. Each table contains records of a particular type. Each record type defines a fixed
number of fields, or attributes.
• The columns of the table correspond to the attributes of the record type. The relational data model
is the most widely used data model, and a vast majority of current database systems are based on
the relational model.
What is the Relational Model?
• The relational model represents how data is stored in Relational Databases. A relational database
consists of a collection of tables, each of which is assigned a unique name.
• Consider a relation STUDENT with attributes ROLL_NO, NAME, ADDRESS, PHONE, and
AGE shown in the table.

Table Student
ROLL_NO NAME ADDRESS PHONE AGE

1 RAM DELHI 9455123451 18

2 RAMESH GURGAON 9652431543 18

3 SUJIT ROHTAK 9156253131 20

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:

1 RAM DELHI 9455123451 18

• 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:

The select operation(σ):


The select operation selects tuples that satisfy a given predicate. Use the lowercase Greek letter
sigma(σ) to denote the selection. The predicate appears as a subscript to σ.
The argument relation is in parentheses after the σ.

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

• R is the relation from which the tuples are selected.

Example1:

Consider the following relation Employees:

If you want to select employees from the "Sales" department, the SELECT operation would be:

σ Department = ’Sales’(Employees)
The result would be:

EmpID Name Age Department


1 John 28 Sales
3 Bob 25 Sales

2. Select Employees with Age Greater than 30

Operation:

σAge>30(Employees)

3. Select Employees from the HR Department


Operation:
σDepartment=′HR′(Employees)
4. Select Employees with Age Less than 30 and in the Sales Department
Operation:
σ Age<30 ∧ Department=′Sales′(Employees)

The Project operation (π)


The project operation is a unary operation. The PROJECT operation (denoted by π, pi) in relational algebra is used to
retrieve specific columns (or attributes) from a relation, discarding the other columns and eliminating duplicates in the
result.

Projection is denoted by the upper case Greek letter Pi.

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:

1. Project Employee Names


Operation:
πName(Employees)

2. Project Employee Names and Departments


Operation:

πName,Department(Employees)

The RENAME operation


The RENAME operation (denoted by ρ, rho) in relational algebra is used to change the name of a
relation or its attributes. It allows you to give a new name to a relation or assign new names to the
attributes of a relation.
Where:
• ρ is the rename operator.
• new_relation_name is the new name for the relation.
• new_attribute1, new_attribute2, ... are the new names for the attributes.
• Relation is the original relation to be renamed.
Examples:
1. Renaming a Relation
Given the Employees relation:

This renames the relation Employees to Staff but keeps the same attributes.

Now the relation is referred to as Staff instead of Employees.


2. Renaming Attributes
Suppose you want to rename the attributes EmpID to ID, and Name to EmployeeName.

3. Renaming Both Relation and Attributes


You can rename both the relation and its attributes simultaneously.
Now the relation is referred to as Staff, and its attributes are renamed.

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.

The UNION operation

The UNION operation in relational algebra (denoted by ∪) combines the tuples


(rows) of two relations to produce a new relation that includes all tuples that
appear in either of the input relations, eliminating duplicates.
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 union operator.
Example1:

Consider the following two relations Employees1 and Employees2:

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:

π (Name, Age) (Employees1) ∪ π (Name, Age) (Employees2)


The INTERSECTION operation
The INTERSECTION operation in relational algebra (denoted by ∩) retrieves the common tuples
(rows) from two relations. It is used to return only those tuples that exist in both relations.

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:

Consider the following two relations Employees1 and Employees2:

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:

π(Name, Age)(Employees1) ∩ π(Name, Age)(Employees2)

The SET DIFFERENCE operation


The SET DIFFERENCE operation in relational algebra (denoted by −) returns the tuples (rows) that are
present in the first relation but not in the second relation. It is also known as the MINUS operation
in SQL. This operation allows you to find the difference between two sets of data.
Syntax:

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:

Consider two relations Employees1 and Employees2:


Set Difference Operation (Employees1 − Employees2):
This operation returns all tuples in Employees1 that are not present in Employees2.

Set Difference Operation (Employees2 − Employees1):


Similarly, if we perform Employees2 − Employees1, it would return all tuples in Employees2 that are not
present in Employees1:

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:

π(Name, Age)(Employees1) − π(Name, Age)(Employees2)

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)

1. Theta Join (θ-Join):


A theta join is a general join where a condition (θ) is used to combine tuples from two relations. The
condition can be any comparison operator such as =, >, <, >=, etc.

R1 ⋈θ R2
• R1 and R2 are the two relations.
• θ is the condition (e.g., R1.A = R2.B).
Example:

Assume we have two relations Employees and Departments:


We want to find the employees along with their department names. We can use a theta join where the
condition is Employees.DeptID = Departments.DeptID:

Employees ⋈ (Employees.DeptID = Departments.DeptID) Departments

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.

Right Outer Join (R1 ⟖ R2):


In a Right Outer Join, we keep all the tuples from the right relation (R2), and for the tuples that don’t
have a match in the left relation (R1), we fill in the missing columns with NULL.
Example:

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.

Full Outer Join: R1 ⟗ R2


In a Full Outer Join, all tuples from both relations (R1 and R2) are included. Where there is no match,
the missing attributes are filled with NULL.

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:

The query { T | T ∈ Employee } will return:


• Tuple 1: (1, Alice, 30, HR)
• Tuple 2: (2, Bob, 25, Sales)
• Tuple 3: (3, Charlie, 35, IT)

Let’s consider a relation Employee with attributes (ID, Name, Age, Department):

To find employees who work in the "HR" department:


{ T | T ∈ Employee ∧ T.Department = "HR" }
This query returns all tuples T from the relation Employee where the Department attribute is "HR".
Key Points:
• TRC focuses on retrieving tuples based on a predicate or condition.
• TRC is declarative: It specifies what to retrieve, not how to retrieve it.
2. Domain Relational Calculus (DRC):
• Domain Relational Calculus (DRC) uses variables that represent the values of individual attributes
(domains), rather than entire tuples.
• DRC also retrieves the result based on conditions applied to the domains (attributes) of a relation.

Syntax:

{ <x₁, x₂, ..., xn> | P(x₁, x₂, ..., xn) }


Where:
• <x₁, x₂, ..., xn> are domain variables (representing attributes).
• P(x₁, x₂, ..., xn) is a predicate (condition or formula) applied to these variables.
EXAMPLE:
Employee:

Retrieve names of employees who are older than 28:


{ <Name> | Employee(ID, Name, Age, Department) AND Age > 28 }
Result:
• Alice
• Charlie

You might also like