0% found this document useful (0 votes)
38 views60 pages

22MCA21 - DBMS - Simplified Notes

Uploaded by

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

22MCA21 - DBMS - Simplified Notes

Uploaded by

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

ACHARYA INSTITUTE OF TECHNOLOGY

Soladevanahalli, Bengaluru – 560107

DEPARTMENT OF MASTER OF COMPUTER APPLICATION

Simplified Notes
Based on
Previous Year VTU Questions and answers

Database Management System


(22MCA21)
Simplified Notes

Ms.SHAHEENA K V
Assistant Professor, Department of MCA
Acharya Institute of Technology
Soladevanahalli, Bengaluru – 560107

2023-24
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

COs, POs, PEOs, PSOs


I. COURSE OUTCOMES (COS)
 To Identify, analyze and define database objects, enforce integrity constraints on a database
using RDBMS
 Use Structured Query Language (SQL) for database manipulation and also demonstrate the
basic of query evaluation.
 Design and build simple database systems and relate the concept of transaction, concurrency
control and recovery in database
 Develop application to interact with databases, relational algebra expression.
 Develop applications using tuple and domain relation expression from queries.
II. PROGRAM OUTCOMES (POS)
 PO 1: Technological knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and computer science and business systems to the solution of complex
engineering and societal problems.

 PO 2: Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering and business problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.

 PO 3: Design/development of solutions: Design solutions for complex engineering problems


and design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

 PO 4:Conduct investigations of complex problems: Use research-based knowledge and


research methods including design of experiments, analysis and interpretation of data, and
synthesisof the information to provide valid conclusions.

 PO 5:Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations
III. PROGRAM EDUCATIONAL OBJECTIVES (PEOS)
 PEO 1: be skilled software engineers who can adopt lifelong learning philosophy for
continuous improvement and acquire higher education.

 PEO 2: Drive graduates in inter-disciplinary field to understand cross cultural, societal,


professional, legal and ethical issues prevailing in industry.

 PEO 3: Expertise to use research, experiment, contemporary issues to solve real time industrial

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
2|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

problems.
IV. PROGRAM SPECIFIC OUTCOME (PSOS)
 PSO 1: Develop self-motivational ability to use current technologies, skills and models in
computing discipline.

 PSO 2: Drive graduates in inter-disciplinary field to understand cross cultural, societal,


professional, legal and ethical issues prevailing in industry.

 PSO 3: Expertise to use research, experiment, contemporary issues to solve real time industrial
problemscomplete zealand commitment.

V. CO-PO -PSO Mapping

Program Outcomes PSO’s


COs
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3

CO1    

CO2   

CO3    

CO4   
CO5    

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
3|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Database Management System


Course Code 22MCA22 Semester II
MODULE – 01
SYLLABUS CONTENT
Introduction to Databases: Introduction, Characteristics of database approach, Advantages of
using the DBMS approach, History of database applications. Overview of Database Languages
and Architectures: Data Models, Schemas, and Instances. Three schema architecture and data
independence, database languages, and interfaces, The Database System environment.
Conceptual Data Modelling using Entities and Relationships: Entity types, Entity sets, attributes,
roles, and structural constraints, Weak entity types, ER diagrams, Examples Textbook 1: Ch 1.1
to 1.8, 2.1 to 2.6, 3.1 to 3.7
Reference:Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th
Edition, 2017, Pearson.

1. What is DBMS ? Explain the actors on the scene and workers behind the scene or
Discus on the different types of people who works in database environment
A DBMS is software that helps define, store, manipulate, and share databases. Its key functions include:
Defining the database: Specifying data types, structures, and constraints.
Constructing the database: Storing data on a controlled storage medium.
Manipulating the database: Retrieving and updating data, and generating reports.
Sharing the database: Allowing multiple users and applications to access it simultaneously.
Additional functions:
Protection: Safeguarding against hardware failures and unauthorized access.
Maintenance: Evolving the system as requirements change over time, since databases can
have a long life cycle.
Actors on the Scene in Database Systems
Database Administrators (DBAs): Responsible for managing and overseeing the database. Tasks
include: Authorizing user access. ,Managing database resources.,Addressing security issues,Monitoring
system performance. In large organizations, DBAs often work with a team for support.
Database Designers: Focus on designing the structure of the database. Decide what data needs to

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
4|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

be stored and how it should be structured. Collaborate with users to understand their requirements and
create a database that fulfills their needs.
End Users:
Casual Users: Use the database occasionally with advanced tools for queries.
Naive/Parametric Users: Perform routine tasks using predefined queries and updates.
Sophisticated Users: Advanced users, like engineers, who handle complex tasks using DBMS
features.
Standalone Users: Manage personal databases using pre-built software.
System Analysts and Application Programmers:
System Analysts: Gather user requirements and specify the needs for the database.
Application Programmers: Write programs that implement standard transactions and applications
based on these requirements.
Workers Behind the Scene:
1. DBMS System Designers and Implementers: Develop the software that powers the DBMS.
Create modules for query processing, data access, concurrency control, and data recovery.
2. Tool Developers: Develop specialized software tools for database design, performance monitoring,
and other tasks. These tools are often developed by independent software vendors.
3. Operators and Maintenance Personnel: Ensure the smooth operation of the database hardware
and software. They do not interact with the database content directly but manage the infrastructure.

2. Explain characteristics and advantages of Database Approach


Characteristics of the Database Approach
In traditional file processing, each user keeps separate files, leading to redundant data and wasted space.
The database approach centralizes data in one location, accessible to multiple users. Key features include:
1. Self-Describing Nature of a Database System: A database system stores data along with its
structure (metadata) in a catalog, making it easier to manage multiple databases. For example, a student
database will have a catalog describing fields like student names and IDs.
2. Insulation Between Programs and Data, and Data Abstraction: In traditional systems,
changes to the data structure require program modifications. With a DBMS, the structure is separate, so

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
5|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

changes (like adding a new field) are made in the catalog, not in the programs. This allows flexible updates
without altering applications.
3. Support for Multiple Views of the Data: A database can provide different views to different
users based on their needs. For example, an admin might need access to student grades, while a teacher
might only need attendance records.
4. Sharing of Data and Multi-User Transaction Processing: Multiple users can access the
database simultaneously. The DBMS ensures that transactions (such as booking a flight seat) are handled
without conflicts, maintaining data accuracy through concurrency control.
Advantages of Using the DBMS Approach
1. Controlling Redundancy:In traditional systems, different departments maintain their own files,
leading to duplicate data. A DBMS centralizes data storage, reducing redundancy and ensuring consistency,
though controlled redundancy can be used for performance improvement.
2. Restricting Unauthorized Access :A DBMS allows restricting access, so only authorized users
can view or modify data. For instance, salary information might be accessible only to HR staff.
3. Providing Persistent Storage for Program Objects: Databases can permanently store complex
program objects, helping maintain object integrity across programs without requiring manual conversion to
and from files.
4. Efficient Query Processing: A DBMS optimizes queries using indexes and specialized data
structures, speeding up data retrieval. It also manages buffering and caching for frequently accessed data.
5. Backup and Recovery: A DBMS provides mechanisms for recovering from hardware or
software failures, ensuring that the database can be restored to its previous state in case of errors.
6. Providing Multiple User Interfaces: A DBMS supports various user interfaces like web-based
GUIs, query languages, and programming interfaces for different types of users.
7. Representing Complex Relationships:A DBMS handles complex relationships efficiently. For
example, it can manage relationships between students and courses and ensure consistent updates.
8. Enforcing Integrity Constraints: A DBMS enforces rules (like data types or unique keys) to
maintain data accuracy and prevent errors.
9. Permitting Inference and Actions: Modern databases allow for automated inference (deductive
rules) and actions (triggers or stored procedures) based on changes in the data.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
6|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

3. Explain advantages of using database system approach over traditional file system
approach
Advantages of Using the DBMS Approach
1. Controlling Redundancy:In traditional systems, different departments maintain their own files,
leading to duplicate data. A DBMS centralizes data storage, reducing redundancy and ensuring consistency,
though controlled redundancy can be used for performance improvement.
2. Restricting Unauthorized Access :A DBMS allows restricting access, so only authorized users
can view or modify data. For instance, salary information might be accessible only to HR staff.
3. Providing Persistent Storage for Program Objects: Databases can permanently store complex
program objects, helping maintain object integrity across programs without requiring manual conversion to
and from files.
4. Efficient Query Processing: A DBMS optimizes queries using indexes and specialized data
structures, speeding up data retrieval. It also manages buffering and caching for frequently accessed data.
5. Backup and Recovery: A DBMS provides mechanisms for recovering from hardware or
software failures, ensuring that the database can be restored to its previous state in case of errors.
6. Providing Multiple User Interfaces: A DBMS supports various user interfaces like web-based
GUIs, query languages, and programming interfaces for different types of users.
7. Representing Complex Relationships:A DBMS handles complex relationships efficiently. For
example, it can manage relationships between students and courses and ensure consistent updates.
8. Enforcing Integrity Constraints: A DBMS enforces rules (like data types or unique keys) to
maintain data accuracy and prevent errors.
9. Permitting Inference and Actions: Modern databases allow for automated inference (deductive
rules) and actions (triggers or stored procedures) based on changes in the data.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
7|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

4. Explain Database system environment with a diagram


A simplified database system environment

A database environment is made up of various components that work together to manage, store, and use
data efficiently. These components include:
Hardware: The physical equipment such as computers and devices used to run the database system.
Software: Includes the operating system and database management software like MS Access or
SQL Server, which helps in managing the data.
People: Those who manage and use the database system, including database administrators and
end-users.
Techniques: The rules, concepts, and procedures used to manage the database and guide how
people and software interact with the data.
Data: The actual information or facts stored in the database that are organized and accessed
through the system.
In short, the database environment is a combination of all these components working together to handle
data efficiently.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
8|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

5. Explain three schema architecture of DBMS with neat diagram


The three schema architecture is used to describe the structure of a specific database system. The goal of
the three-schema architecture is to separate the user applications and the physical database

In this architecture, schemas can be defined at the following three levels:


1. The internal level (physical schema) has an internal schema, which describes the physical storage
structure of the database.
 The physical level is used to describe complex low level data structures in detail.
 The internal schema uses a physical data model and describes the complete details of data storage
and access paths for the database.
2. The Conceptual level (conceptual schema) has a conceptual schema, which describes the structure of the
whole database for a community of users.
 The conceptual schema hides the details of physical storage structures and concentrates on
describing entities, data types, relationships, user operation and constraints.
 In the conceptual level, internal details such as an implementation of the data structure are hidden.
Programmers and database administrators work at this level.
3. The External Level or view level includes a number of external schemas or user views.
 Each external schema describes the part of the database that a particular user group is interested
in and hides the rest of the database from that user group.
 The view schema describes the end user interaction with database systems.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
9|Page
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

6. What are the different types of attributes? Explain With example


Attribute An attribute describes the property of an entity. An attribute is represented as Oval in an ER
diagram. There are four types of attributes:
1. Key attribute
2. Composite attribute
3. Multivalued attribute
4. Derived attribute
1. Key attribute: A key attribute can uniquely identify an entity from an entity set. For example, student
roll number can uniquely identify a student from a set of students. Key attribute is represented by oval same
as other attributes however the text of key attribute is underlined.
2. Composite attribute: An attribute that is a combination of other attributes is known as composite
attribute. For example, In student entity, the student address is a composite attribute as an address is
composed of other attributes such as pin code, state, country.
3. Multivalued attribute: An attribute that can hold multiple values is known as multivalued attribute. It is
represented with double ovals in an ER Diagram. For example – A person can have more than one phone
numbers so the phone number attribute is multivalued.
4. Derived attribute: A derived attribute is one whose value is dynamic and derived from another attribute.
It is represented by dashed oval in an ER Diagram. For example – Person age is a derived attribute as it
changes over time and can be derived from another attribute (Date of birth)

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
10 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

7. What are the notations used in ER diagram with example


ER Model Entity Relationship (ER) model is a theoretical/conceptual model which provides a means of
identifying entities, their attributes to be represented in the database and how these entities are related to
each other.
ER Diagram Entity Relationship Diagram (ERD) is a pictorial representation of data that describes how
data is communicated and related to each other. Entities, attributes and relationship among entities can be
characterised with the help of the ER diagram. ERD shows the logical structure of the database which can
be used to convert into relational database.

Entity Entities are represented by rectangles in ERD. Rectangles are named with the entity set they
represent.
For Example

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
11 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Weak Entity Weak entities are represented by double-lined rectangles in ERD.

Attribute Attributes are represented by ellipses. Ellipses are name with attribute name and connected with
rectangle (entity).

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
12 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Relationship A relationship describes how entities interact. For example, the entity “Carpenter” may be
related to the entity “table” by the relationship “builds” or “makes”. Relationships are represented by
diamond shapes and are labelled using verbs.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
13 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Database Management System


Course Code 22MCA22 Semester II
MODULE – 02
SYLLABUS CONTENT
Relational Model: Relational Model Concepts, Relational Model Constraints and relational database
schemas, Update operations, transactions, and dealing with constraint violations. Relational Algebra:
Unary and Binary relational operations, additional relational operations (aggregate, grouping, etc.)
Examples of Queries in relational algebra. Mapping Conceptual Design into a Logical Design:
Relational Database Design using ER-to-Relational mapping. Textbook 1:, Ch 5.1 to 5.3, 8.1 to 8.5,
9.1;
Reference:Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th
Edition, 2017, Pearson.

1. Discus the characteristics of Relations


Relational Model: The relational model represents a database as a collection of relations, which are
essentially tables of values. Each row in the table represents a set of related data values, corresponding to a
real-world entity or relationship. The table name and column names help interpret the meaning of the
values in each row. In this model, data is stored logically in tables, but the physical storage is independent
of how the data is organized.
Key Concepts in the Relational Model
Attribute: A column in a table that defines properties of a relation (e.g., Student_Rollno, Name).
Table: A collection of rows and columns where each row represents a record and each column represents
an attribute.
Tuple: A single row in a table, representing a single record.
Relation Schema: The structure of a relation, including its name and attributes.
Degree: The number of attributes (columns) in a relation.
Cardinality: The number of tuples (rows) in a relation.
Column: A vertical set of values for a specific attribute.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
14 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Relation Instance: A set of tuples at any given point in time. A relation instance doesn't have duplicate
tuples.
Relation Key: One or more attributes that uniquely identify a tuple in a relation.
Attribute Domain: The predefined set of values and data types for an attribute.

2. Describe the entity integrity and referential integrity in details with necessary
example
Integrity Constraints: Integrity constraints are a set of rules. It is used to maintain the quality of
information. Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected. Thus, integrity constraint is used to guard
against accidental damage to the database.
Types of Integrity Constraint

Entity integrity constraints


 The entity integrity constraint states that primary key value can't be null.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
15 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

 This is because the primary key value is used to identify individual rows in relation and if the primary
key has a null value, then we can't identify those rows.
 A table can contain a null value other than the primary key field.
For example:

Referential Integrity Constraints


 A referential integrity constraint is specified between two tables.
 In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2,
then every value of the Foreign Key in Table 1 must be null or be available in Table 2.
Example:

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
16 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

3. Explain primary key and foreign key with an example


A primary key is used to ensure that data in the specific column is unique. A column cannot have NULL
values. It is either an existing table column or a column that is specifically generated by the database
according to a defined sequence.
Example: STUD_NO, as well as STUD_PHONE, are candidate keys for relation STUDENT but
STUD_NO can be chosen as the primary key (only one out of many candidate keys).
Table Student

A foreign key is a column or group of columns in a relational database table that provides a link between
data in two tables. It is a column (or columns) that references a column (most often the primary key) of
another table.
Example: STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation.
Table Student_Course

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
17 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

4. How does the various update operations deal with Constraint violation's
In a database, there are four main update operations:
Insert: Adds new data to a table.
Delete: Removes data from a table.
Update: Changes existing data in a table.
Select: Retrieves specific data from the database (this doesn't modify data).
Insert Operation :When adding new data (inserting), certain rules (called constraints) must be followed:
Domain: The data must be in the correct format (like numbers for age).
Primary Key: The key (ID) must be unique and cannot be empty (NULL).
Foreign Key: If a column refers to another table, that data must exist in the other table.
Delete Operation:When deleting data, there could be issues if the data is linked to another table:
Restrict: You can't delete the data if it's linked.
Cascade: Automatically delete the linked data too.
Set Null: Set the linked data to NULL.
Update Operation: When changing data (updating), you must ensure: The new data fits the rules (like the
correct format). Changing a key (ID) doesn't create duplicates or break links with other tables.
In case of any rule violations, the database rejects the operation to maintain data integrity.

5. Describe about different relational Algebra operations


Relational Algebra:Relational Algebra is a way to query and retrieve data from databases. It uses a set of
operations that help work with tables and get the information we need. The results of each operation form a
new table (or relation).
Main Types of Operations
Unary Relational Operations (work on one table):
SELECT: Get rows that match certain conditions.
PROJECT: Get specific columns.
RENAME: Change the name of a table or its columns.
Set Operations (work on two tables):
UNION: Combines rows from two tables, removing duplicates.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
18 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

INTERSECTION: Gets rows that are in both tables.


MINUS (Set Difference): Gets rows that are in the first table but not the second.
Other Binary Operations (work on two tables):
JOIN: Combines rows from two tables based on a condition.
DIVISION: Retrieves rows that match all values in a related table.
Aggregate Functions: These perform calculations (like sum, average) on groups of rows.
Unary Relational Operations
SELECT (σ): Used to get rows that meet a specific condition.
Syntax: σ<condition>(Table)
Example: σDno=4(EMPLOYEE) gets all employees from department 4.
PROJECT (π): Used to get specific columns.
Syntax: π<columns>(Table)
Example: πLname, Fname, Salary(EMPLOYEE) gets only the last name, first name, and salary of
employees.
RENAME (ρ): Changes the name of the table or columns.
Syntax: ρNewName(Table) or ρ(NewColumn1, NewColumn2)(Table).
Example: ρE(Fname, Lname, Salary)(EMPLOYEE) renames the columns.
Set Operations
UNION (∪): Combines rows from two tables, removing duplicates.
Example: π(Student_Name)FRENCH ∪ π(Student_Name)GERMAN gets students studying French or
German.
INTERSECTION (∩): Gets rows present in both tables.
Example: π(Student_Name)FRENCH ∩ π(Student_Name)GERMAN gets students studying both French
and German.
MINUS (−):Gets rows that are in the first table but not the second.
Example: π(Student_Name)FRENCH − π(Student_Name)GERMAN gets students studying French but not
German.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
19 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Other Operations
JOIN:Combines rows from two tables based on a matching condition. Example: Combine employees
with their department details based on Dno.
Cartesian Product (Cross Product):
Operation: Combines every tuple from one relation with every tuple from another relation.
Resulting Relation: Includes all attributes from both relations.
Tuple Count: If relation A has 'n' tuples and relation B has 'm' tuples, then the Cartesian product
A×B will have n×mn \times mn×m tuples.
Example:
If A = {(1, 2), (3, 4)} and B = {(5, 6)}, then A×BA \times BA×B = {(1, 2, 5, 6), (3, 4, 5, 6)}.
2. JOIN Operations:
JOIN: Combines tuples from two relations based on a related condition.
Difference from Cartesian Product: JOIN is more efficient as it only returns rows where the join condition
is met.

General Form: R ⋈ <condition>S

Types of JOIN:
Theta Join: Uses comparison operators (e.g., <, >, =).

Example: A⋈ A.S<B.T B joins where A.S is less than B.T.


Equijoin: Uses the equality operator (=).
Example: Joining A and B on A.column = B.column.
Natural Join: A simplified equijoin where duplicate attributes are eliminated.

Example: If A and B share common attributes, A⋈ B eliminates the duplicate columns.

Cross Join (Cartesian Product): When there is no join condition, it results in a Cartesian Product.
Outer Joins:
Left Outer Join: Returns all tuples from the left table (A), and matching tuples from the right table (B).
Non-matching tuples are filled with NULL.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
20 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Right Outer Join: Returns all tuples from the right table (B), and matching tuples from the left table (A).
Non-matching tuples are filled with NULL.

Full Outer Join: Combines both Left and Right Outer Joins. It returns all tuples from both tables, filling
non-matching tuples with NULL.
3. DIVISION Operation: Used for queries that require identifying tuples in one relation (R) associated
with all tuples in another relation (S).
Operation:
Given R(Z) and S(X), where X⊆Z and Y=Z−X the result of R÷S is a relation T(Y) containing tuples that
are associated with all tuples in S.
Example: If relation R contains student-course combinations and S contains a list of courses, division
would find students enrolled in all courses in S.
Aggregate Functions and Grouping in Relational Algebra: Aggregate functions like SUM, COUNT,
AVERAGE, MAX, and MIN are used to perform calculations on data. Grouping is used to organize data

based on one or more attributes before applying these functions. Special Operator: ℑ

The general form is: <grouping attributes> ℑ <function list>(R)


Grouping attributes: Columns used to group data (e.g., department number Dno).

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
21 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Function list: Aggregate functions applied to grouped data (e.g., COUNT(Ssn), AVERAGE(Salary)).
R: The relation (table) on which the operation is performed.
Example
Suppose we want to retrieve each department's number (Dno), the number of employees in the department
(COUNT(Ssn)), and their average salary (AVERAGE(Salary)). The query is written as:

ρR(Dno,No_of_employees,Average_sal)(Dno ℑ COUNT(Ssn),AVERAGE(Salary)(EMPLOYEE))
Here, ρR renames the resulting relation's attributes to Dno, No_of_employees, and Average_salary.
Without Grouping: If no grouping is specified, the aggregate functions apply to the entire relation.
For example, to compute the total number of employees and the average salary for all employees, the query

is: ℑCOUNT(Ssn),AVERAGE(Salary)(EMPLOYEE))
This would result in a single tuple relation with the values of the aggregate functions.
Common Aggregate Functions:
COUNT(X): Counts the number of tuples.
SUM(X): Adds up values in a column.
AVERAGE(X): Calculates the average value.
MAX(X): Finds the highest value.
MIN(X): Finds the lowest value.

6. Describe the steps involved in ER diagram to relational mapping with suitable


example
ER-to-Relational Mapping steps: The ER-to-Relational Mapping Algorithm converts an Entity-
Relationship (ER) schema into a relational database schema. Below are the main steps:
Below are the key steps in the algorithm with the COMPANY database used as an example:
Step 1: Mapping Strong Entity Types
 For each strong entity type, create a corresponding relation.
 Include all simple attributes of the entity.
 If there are composite attributes, only include their simple components.
 The primary key of the relation is the entity's key attribute.
Example: EMPLOYEE → Relation includes attributes like Fname, Lname, Ssn (primary key).

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
22 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

DEPARTMENT → Relation includes attributes like Dname, Dnumber (primary key).

Step 2: Mapping Weak Entity Types


 For each weak entity type, create a new relation.
 Include all attributes of the weak entity and the foreign key from the strong entity.
 The primary key is a combination of the foreign key and the weak entity’s partial key.
Example: DEPENDENT (weak entity) → Includes Essn (foreign key from EMPLOYEE) and
Dependent_name. The primary key is {Essn, Dependent_name}.
Step 3: Mapping 1:1 Binary Relationship Types
Three approaches:
 Foreign key: Add the primary key of one entity as a foreign key in the other.
 Merged relation: Combine both entities into a single relation if both participate fully.
 Cross-reference: Create a separate relation storing the primary keys of both entities.
Example: MANAGES relationship (DEPARTMENT and EMPLOYEE) → Add Mgr_ssn (foreign key) and
Mgr_start_date in DEPARTMENT.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
23 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Step 4: Mapping 1Binary Relationship Types


 Use the foreign key approach: Add the primary key from the "1-side" to the "N-side"
relation.
Example:WORKS_FOR relationship → Add Dno (foreign key) to EMPLOYEE.
CONTROLS relationship → Add Dnum (foreign key) to PROJECT.
Step 5: Mapping MBinary Relationship Types
 Create a new relation to store the relationship.
 Include foreign keys from both entities as the primary key of the new relation.
Example:
WORKS_ON relationship → Create a new relation WORKS_ON with Essn (EMPLOYEE) and Pno
(PROJECT) as foreign keys, plus the Hours attribute. The primary key is {Essn, Pno}.
Step 6: Mapping Multivalued Attributes
 Create a separate relation for multivalued attributes.
 Include the multivalued attribute and a foreign key referencing the entity’s primary key.
Example:
DLOCATION (multivalued attribute of DEPARTMENT) → Create DEPT_LOCATIONS relation with
Dnumber (foreign key) and Dlocation. The primary key is {Dnumber, Dlocation}.
Step 7: Mapping N-ary Relationship Types
 For N-ary relationships (where N > 2), create a new relation.
 Include foreign keys from all participating entities.
 The primary key is a combination of these foreign keys.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
24 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Database Management System


Course Code 22MCA22 Semester II
MODULE – 03
SYLLABUS CONTENT
SQL: SQL data definition and data types, specifying constraints in SQL, retrieval queries in SQL,
INSERT, DELETE, and UPDATE statements in SQL, Additional features of SQL. Advances Queries:
More complex SQL retrieval queries, Specifying constraints as assertions and action triggers, Views in
SQL, Schema change statements in SQL. Database Application Development: Accessing databases
from applications, An introduction to JDBC, JDBC classes and interfaces, SQLJ, Stored procedures,
Case study: The internet Bookshop. Textbook 1: Ch 6.1 to 6.5, 7.1 to 7.4; Textbook
2: 6.1 to 6.6;
Reference:Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th
Edition, 2017, Pearson.
1. What is datatype and explain different datatypes in sql
Data types in SQL define the kind of data a column can hold in a database table, such as numbers, text, or
dates. They help ensure that data is stored correctly and efficiently.
Common SQL Data Types:
Numeric Data Types:
 INT: Stores whole numbers (e.g., 5, -100).
 DECIMAL(p, s): Stores exact decimal numbers, where p is the precision (total digits) and s is the
scale (digits after the decimal point) (e.g., 123.45).
 FLOAT: Stores approximate floating-point numbers (e.g., 3.14).
Character Data Types:
 CHAR(n): Fixed-length string, pads with spaces if necessary (e.g., CHAR(5) stores 'abc ').
 VARCHAR(n): Variable-length string, stores only the actual input (e.g., VARCHAR(10) stores
'abc' as 'abc').
Date and Time Data Types:
 DATE: Stores date values (e.g., '2024-10-16').

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
25 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

 TIMESTAMP: Stores date and time, including fractions of a second (e.g., '2024-10-16
10:30:45.123').
Binary Data Types:
 BLOB: Stores large binary data like images or videos (e.g., up to 4 GB).
Boolean (Emulated in SQL):
 BOOLEAN: Represents true/false values (often emulated with 1 for true and 0 for false).
Data Type Description Range/Storage

Stores integers and real numbers with precision Precision: up to 38 digits. Scale: -84
NUMBER(p,s)
p and scale s. to 127.

INTEGER Stores integer values. -10^38 +1 to 10^38 -1

Stores floating-point numbers with


FLOAT Up to 126 binary digits
approximate precision.

CHAR(n) Fixed-length character data. Maximum size: 2000 bytes

VARCHAR2(n) Variable-length character data. Maximum size: 4000 bytes

January 1, 4712 BC to December 31,


DATE Stores date and time data.
9999 AD

TIMESTAMP Stores date and time with fractional seconds. Up to 9 fractional seconds precision

Stores large binary data such as images and


BLOB Up to 4 GB
multimedia.

BOOLEAN Emulates BOOLEAN by storing 1 for TRUE


1 byte
(Emulated) and 0 for FALSE.

2. Explain basic structure of SQL queries with all clauses


A basic SQL query is composed of several key clauses that perform different functions. Below is an
explanation of the structure of SQL queries and the commonly used clauses.
1. SELECT Clause:The SELECT clause is used to specify which columns you want to retrieve from a
database table.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
26 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Syntax: SELECT column1, column2, ... FROM table_name;


Example: SELECT name, age FROM employees;
2. FROM Clause:The FROM clause specifies the table(s) from which to retrieve the data.
Syntax:SELECT column1, column2 FROM table_name;
Example:SELECT name, salary FROM employees;
3. WHERE Clause: The WHERE clause is used to filter records based on specified conditions. It restricts
the rows returned by the query.
Syntax:SELECT column1, column2 FROM table_name WHERE condition;
Example:SELECT name, salary FROM employees WHERE salary > 50000;
4. GROUP BY Clause:The GROUP BY clause is used to group rows that have the same values in specified
columns. It is often used with aggregate functions like COUNT, SUM, AVG, MAX, MIN.
Syntax:SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
Example:SELECT department, AVG(salary) FROM employees GROUP BY department;
5. HAVING Clause: The HAVING clause is used to filter records after the GROUP BY operation. It works
similarly to the WHERE clause but is applied to groups, not individual rows.
Syntax:SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1
HAVING condition;
Example:SELECT department, AVG(salary) FROM employees GROUP BY department HAVING
AVG(salary) > 60000;
6. ORDER BY Clause:The ORDER BY clause is used to sort the result set in ascending or descending
order. The default order is ascending.
Syntax: SELECT column1, column2 FROM table_name ORDER BY column1 [ASC|DESC];
Example:SELECT name, salary FROM employees ORDER BY salary DESC;
SQL Clauses:
SELECT: Columns to display.
FROM: Table to fetch data from.
WHERE: Filter rows based on conditions.
GROUP BY: Group rows based on column values.
HAVING: Filter groups.
ORDER BY: Sort the result set.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
27 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

3. Explain about data definition Commands with example


Data Definition Commands (DDL) in SQL:Data Definition Language (DDL) consists of commands that
define, modify, and delete database structures like tables, indexes, and schemas. These commands impact
the schema of the database and are essential for creating and managing its structure.
Key DDL Commands:
 CREATE: Used to create new database objects such as tables, indexes, views, or schemas.
Example:
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
Department VARCHAR(30),
Salary DECIMAL(10, 2)
);
This command creates a table called Employees with four columns: EmpID, EmpName, Department, and
Salary.
 ALTER:Used to modify an existing database structure, such as adding, modifying, or dropping
columns in a table.
Example: ALTER TABLE Employees ADD Email VARCHAR(100);
This command adds a new column Email to the Employees table.
 DROP: Deletes an existing database object like a table, view, or index. Once deleted, the data is lost.
Example: DROP TABLE Employees;
This command deletes the Employees table and all its data permanently.
 TRUNCATE:Removes all records from a table but keeps the table structure intact. It’s faster than
DELETE as it does not log individual row deletions.
Example: TRUNCATE TABLE Employees;
This command deletes all records from the Employees table, but the table itself remains.
 RENAME:Used to rename a database object such as a table.
Example: RENAME Employees TO Staff;
This command renames the Employees table to Staff.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
28 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

4. Briefly explain about Data Manipulation Commands with syntax and examples.
Data Manipulation Commands (DML) in SQL: Data Manipulation Language (DML) includes commands
that manage data stored in the database. These commands allow users to retrieve, insert, update, and delete
data.
Key DML Commands:
 SELECT:Retrieves data from one or more tables.
Syntax: SELECT column1, column2, ... FROM table_name WHERE condition;
Example: SELECT EmpName, Salary FROM Employees WHERE Department = 'HR';
Retrieves the names and salaries of employees from the HR department.
 INSERT: Adds new data (rows) to a table.
Syntax: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
Example:
INSERT INTO Employees (EmpID, EmpName, Department, Salary) VALUES (101, 'John Doe', 'HR',
50000);
Inserts a new employee into the Employees table.
 UPDATE:Modifies existing data in a table.
Syntax: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example:UPDATE Employees SET Salary = 55000 WHERE EmpID = 101;
Updates the salary of the employee with ID 101.
 DELETE:Removes rows from a table based on a condition.
Syntax:DELETE FROM table_name WHERE condition;
Example:DELETE FROM Employees WHERE EmpID = 101;
Deletes the employee with ID 101 from the Employees table.

5. Explain aggregate function in SQL with example


Aggregate functions in SQL are used to perform calculations on a set of values and return a single value.
These functions are often used with the GROUP BY clause to summarize data. Common aggregate
functions include:

1. COUNT(): Returns the number of rows that match a specified criterion.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
29 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

2. SUM(): Returns the total sum of a numeric column.


3. AVG(): Returns the average value of a numeric column.
4. MAX(): Returns the maximum value in a set.
5. MIN(): Returns the minimum value in a set.

Example of Aggregate Functions

Let’s consider a sample table named Employees:

EmployeeID Name Department Salary


1 Alice HR 60000
2 Bob IT 80000
3 Charlie IT 75000
4 David Finance 70000
5 Eve HR 62000
1. COUNT() Function: To count the total number of employees:
Example : SELECT COUNT(*) AS TotalEmployees FROM Employees;
Output:
TotalEmployees
5

2. SUM() Function: To find the total salary of all employees:


Example : SELECT SUM(Salary) AS TotalSalaryFROM Employees;
Output:
TotalSalary
347000

3. AVG() Function:To calculate the average salary of employees:


Example : SELECT AVG(Salary) AS AverageSalaryFROM Employees;
Output:
AverageSalary
69400

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
30 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

4. MAX() Function: To find the highest salary among employees:


Example: SELECT MAX(Salary) AS HighestSalaryFROM Employees;
Output:
HighestSalary
80000

5. MIN() Function: To find the lowest salary among employees:


Example: SELECT MIN(Salary) AS LowestSalaryFROM Employees;
Output:
LowestSalary
60000

6. Explain group by and having clause with example


7. In SQL how to handle aggregate functions with group by and having clause?with
example
GROUP BY and HAVING Clauses in SQL:GROUP BY and HAVING clauses are used to group rows that
have the same values in specified columns and then filter the grouped data based on certain conditions.
GROUP BY groups rows that share common values.
HAVING filters groups based on aggregate conditions.
In SQL, aggregate functions are used to perform calculations on multiple rows of data and return a single
value. When combined with the GROUP BY and HAVING clauses, you can group data and filter the
results based on aggregate calculations. Here’s how to handle aggregate functions with GROUP BY and
HAVING
1. GROUP BY Clause:The GROUP BY clause groups rows with the same values in one or more columns
into summary rows. It is often used with aggregate functions like COUNT(), SUM(), AVG(), MAX(), and
MIN() to perform operations on each group.
Syntax: SELECT column1, aggregate_function(column2)FROM table_nameGROUP BY column1;
Example: SELECT Department, COUNT(EmpID) AS TotalEmployeesFROM EmployeesGROUP BY
Department;

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
31 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

This query groups employees by their department and counts the number of employees in each department.
2. HAVING Clause: The HAVING clause is used to filter groups created by the GROUP BY clause.
It is similar to the WHERE clause but works after the grouping is done, allowing you to filter groups based
on aggregate functions.
Syntax: SELECT column1, aggregate_function(column2)FROM table_name GROUP BY
column1HAVING condition;
Example: SELECT Department, COUNT(EmpID) AS TotalEmployeesFROM EmployeesGROUP BY
Department HAVING COUNT(EmpID) > 5;
This query groups employees by department, then filters those groups to show only departments with more
than 5 employees.

8. Explain about views in SQL with Example


In SQL, a view is a virtual table that is based on the result of a SELECT query. It does not store the data
itself but provides a way to present and manage data from one or more tables in a structured manner.
Views can simplify complex queries, enhance security by restricting access to specific data, and provide a
way to encapsulate complex logic.
Creating a View :You can create a view using the CREATE VIEW statement.
Here’s the syntax:
CREATE VIEW view_name AS SELECT column1, column2, ...FROM table_name WHERE condition;
Example
Let’s consider a table named Employees:
EmployeeID Name Department Salary
1 Alice HR 60000
2 Bob IT 80000
3 Charlie IT 75000
4 David Finance 70000
5 Eve HR 62000

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
32 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Step 1: Create a View: Suppose we want to create a view that displays the names and salaries of employees
in the IT department:
CREATE VIEW IT_Employees ASSELECT Name, Salary FROM Employees
WHERE Department = 'IT';
Step 2: Query the View:Now you can query the view just like a table:
SELECT * FROM IT_Employees;
Output:
Name Salary
Bob 80000
Charlie 75000
Step 3: Updating a View:If the view is updatable and you want to change Bob's salary, you can do so:
UPDATE IT_EmployeesSET Salary = 85000WHERE Name = 'Bob';
This update will reflect in the underlying Employees table.
Step 4: Dropping a View: If you no longer need the view, you can drop it using:
DROP VIEW IT_Employees;

9. Explain queries using Sub queries and correlated queries.


 Subqueries are nested queries that can return results to be used by the outer query and can exist
independently.
 Correlated subqueries depend on the outer query and are executed multiple times, once for each
row of the outer query.
 Both types of queries enable complex data retrieval operations, enhancing the flexibility and power
of SQL in database management.
1. Subqueries: A subquery (or inner query) is a query nested within another SQL query (the outer query).
It is used to retrieve data that will be used in the main query. Subqueries can be used in various clauses,
including SELECT, FROM, WHERE, and HAVING.
Example of a Subquery: Suppose we have two tables: Employees and Departments.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
33 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Employees Table:
EmployeeID Name DepartmentID Salary Departments Table:
1 Alice 1 60000
2 Bob 2 80000 DepartmentID DepartmentName

3 Charlie 2 75000 1 HR

4 David 3 70000 2 IT

5 Eve 1 62000 3 Finance

To find the names of employees who earn more than the average salary in the Employees table, you can
use a subquery:
SELECT Name FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);
The inner query (SELECT AVG(Salary) FROM Employees) calculates the average salary, and the outer
query retrieves the names of employees whose salaries exceed this average.
2. Correlated Subqueries: A correlated subquery is a type of subquery that references columns from the
outer query. Unlike a regular subquery, which can be executed independently, a correlated subquery cannot.
It is executed once for each row processed by the outer query.
Example of a Correlated Subquery
Using the same Employees and Departments tables, let’s find employees who earn more than the average
salary in their respective departments.
SELECT Name, Salary FROM Employees e WHERE Salary >
(SELECT AVG(Salary) FROM Employees WHERE DepartmentID = e.DepartmentID);
The inner query calculates the average salary for the department of the current employee from the outer
query (e.DepartmentID). The outer query selects the name and salary of employees whose salaries are
greater than the average salary of their respective departments.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
34 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

10. What is trigger explain with example


A trigger in Oracle is a stored procedure that automatically executes (or "fires") in response to specific
events on a particular table or view. These events can be INSERT, UPDATE, or DELETE operations.
Triggers are often used to enforce business rules, validate data, or update other tables when data changes.
Types of Triggers:
1. Row-level triggers: Executes once for each row affected by the triggering event.
2. Statement-level triggers: Executes once for the entire triggering statement, regardless of how
many rows are affected.
Trigger Timing:
 BEFORE Trigger: Executes before the triggering event (e.g., before an INSERT).
 AFTER Trigger: Executes after the triggering event (e.g., after an UPDATE).
 INSTEAD OF Trigger: Used with views to perform the INSERT, UPDATE, or DELETE
operation on the underlying tables.
Example : Trigger to Update Voter Count
A trigger is a database mechanism that automatically executes a specified action when certain events occur,
such as inserts, updates, or deletes. In this case, we create a trigger to update the count of voters in the
Constituency table whenever a new voter is added to the Voter table.
CREATE OR REPLACE TRIGGER UpdateVoterCount
AFTER INSERT ON Voter
FOR EACH ROW
BEGIN
UPDATE Constituency
SET Number_of_voters = Number_of_voters + 1
WHERE Constituency_id = :NEW.Constituency_id;
END;
After the trigger is created, whenever a new row is inserted into the Voter table, the trigger will
automatically update the Number_of_voters in the Constituency table.
For example, inserting a new voter:
INSERT INTO Voter (Voter_id, Name, Age, Constituency_id) VALUES (101, 'John Doe', 30, 5);

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
35 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

The trigger ensures that the Constituency table is updated automatically whenever a new voter is added.
Triggers are event-driven, so once created, you don't need to call them manually—they execute whenever
the defined event occurs.

11. Define Database stored procedure. Explain creating and calling stored procedure
with example.
Database Stored Procedure :A stored procedure is a precompiled group of SQL statements stored in the
database. It allows you to execute multiple SQL commands as a single unit.
 Reusable: Can be called multiple times, reducing repetition.
 Takes Parameters: Can accept input and output parameters.
 Efficient: Runs faster as it’s precompiled.
Syntax to Create a Stored Procedure:
CREATE OR REPLACE PROCEDURE procedure_name
( param1 IN datatype, param2 OUT datatype )
AS
BEGIN
-- SQL statements
END procedure_name;
Example:Creating a procedure to add a new employee to the Employees table:
CREATE OR REPLACE PROCEDURE AddNewEmployee (
emp_id IN NUMBER,
emp_name IN VARCHAR2,
emp_salary IN NUMBER
)
AS
BEGIN
INSERT INTO Employees (EmployeeID, EmployeeName, Salary)
VALUES (emp_id, emp_name, emp_salary);
END

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
36 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Calling the Procedure:: EXEC AddNewEmployee(101, 'John Doe', 50000);


Advantages of stored procedure
 Improves performance by reducing query parsing time.
 Enhances security by controlling access through stored procedures.
 Increases maintainability since changes are made at the procedure level.

12. Discuss i) Dynamic SQL ii) Embedded SQL with example


i) Embedded SQL: Embedded SQL allows SQL statements to be embedded within a general-purpose
programming language such as C, Java, or COBOL. The SQL queries are written inside the host language
code, allowing seamless interaction with the database.
 SQL statements are enclosed within EXEC SQL and END-EXEC.
 Shared variables (used in both SQL and the host language) are prefixed with a colon (:).
 Allows retrieval and manipulation of data within the host program.
Example (Embedded SQL in C):
#include <stdio.h>
/* Main function */
int main() {
char ssn[10];
char fname[20], lname[20];
/* Prompt user to enter SSN */
printf("Enter SSN: ");
scanf("%s", ssn);
/* Embedded SQL to select employee data */
EXEC SQL SELECT FNAME, LNAME
INTO :fname, :lname
FROM EMPLOYEE
WHERE SSN = :ssn;
/* Check if the query was successful */
if (SQLCODE == 0) {

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
37 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

/* Print the employee name */


printf("Employee Name: %s %s\n", fname, lname);
} else {
/* If SSN does not exist, show error */
printf("SSN %s does not exist.\n", ssn);
}
return 0;
}
ii) Dynamic SQL: Dynamic SQL refers to SQL statements that are constructed and executed at runtime,
rather than being precompiled. This technique is useful when the exact structure of the SQL query is
unknown until runtime.
 SQL statements are composed and executed dynamically during program execution.
 Offers flexibility as the query can change based on the input.
 Commonly used in situations where the SQL query needs to be constructed conditionally based
on user input or other factors.
Example (Dynamic SQL in PL/SQL):
DECLARE
sql_stmt VARCHAR2(1000);
student_id NUMBER := 1;
BEGIN
sql_stmt := 'SELECT Name, City FROM Student WHERE Id = ' || student_id;
EXECUTE IMMEDIATE sql_stmt;
END;
This example shows how a SQL query is dynamically generated to select student information from a table
based on the student_id. The query is built and executed at runtime using EXECUTE IMMEDIATE.
In short Embedded SQL is static, with SQL queries written inside a host language and executed directly.
Dynamic SQL is flexible, with SQL queries generated and executed at runtime, making it adaptable to
varying conditions.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
38 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

13. Explain Architecture of JDBC main components and Types of drivers


Architecture of JDBC (Java Database Connectivity): JDBC is an API (Application Programming
Interface) used to connect Java applications to databases. It provides a set of classes and interfaces that
allow applications to interact with databases, execute queries, and retrieve results.

Major Components of JDBC Architecture:


 Application: Java applications (such as applets or servlets) that communicate with the database using
JDBC.
 JDBC API: A set of classes and interfaces in the java.sql and javax.sql packages for database
interaction. Key classes include:
 DriverManager: Manages database drivers and establishes connections.
 Connection: Represents a session with the database.
 Statement: Used to execute SQL queries.
 ResultSet: Stores the result of a query.
 DriverManager: The DriverManager class loads the appropriate database driver to establish a
connection between the Java application and the database. It manages a list of database drivers and
handles connection requests.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
39 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

 JDBC Drivers:The drivers are used to translate generic JDBC calls into database-specific calls.
 Data Sources: Databases or data storage systems that the Java application interacts with.
Types of JDBC Drivers: JDBC drivers are classified into four types based on how they communicate with
the database.
Type I: JDBC-ODBC Bridge Driver: Translates JDBC calls into ODBC (Open Database Connectivity)
calls, and then uses ODBC to connect to the database.
 Type I driver provides mapping between JDBC and access API of a database
 A common Type I driver defines a JDBC to ODBC bridge

Native API
Driver (Type I)

Client
ODBC
API
Data
Application
base

Type II: Native-API Driver (Partially Java): Converts JDBC calls into database-specific calls using
native database libraries.
 Type II driver communicates directly with native API
 Type II makes calls directly to the native API calls
 More efficient since there is one less layer to contend with (i.e. no ODBC)
 It is dependent on the existence of a native API for a database

Type III: Network Protocol Driver:Converts JDBC calls into a database-independent protocol that is sent to
a server. The server then translates the requests into database-specific calls.
 This communication uses a database independent net protocol
 Middleware server then makes calls to the database using database-specific protocol

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
40 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

 The program sends JDBC call through the JDBC driver to the middle tier
 Middle-tier may use Type I or II JDBC driver to communicate with the database.

Type IV: Thin Driver (Pure Java): Directly converts JDBC calls into database-specific protocol using Java.
It communicates directly with the database.
 It issues requests directly to the database using its native protocol
 It can be used directly on platform with a JVM
 Most efficient since requests only go through one layer
 Simplest to deploy since no additional libraries or middle-ware

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
41 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Database Management System


Course Code 22MCA22 Semester II
MODULE – 04
SYLLABUS CONTENT
Normalization: Database Design Theory – Introduction to Normalization using Functional and
Multivalued Dependencies: Informal design guidelines for relation schema, Functional Dependencies,
Normal Forms based on Primary Keys, Second and Third Normal Forms, Boyce-Codd Normal Form,
Multivalued Dependency and Fourth Normal Form, Join Dependencies and Fifth Normal Form.
Examples on normal forms. Normalization Algorithms: Inference Rules, Equivalence, and Minimal
Cover, Properties of Relational Decompositions, Algorithms for Relational Database Schema Design,
Nulls, Dangling tuples, and alternate Relational Designs, Further discussion of Multivalued
dependencies and 4NF, Other dependencies and Normal Forms Textbook 1: Ch 14.1 to -14.7, 15.1 to
15.6
Reference:Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th
Edition, 2017, Pearson.
1. Give all four informal design guidelines for relation schema.
Informal Design Guidelines for Relation Schemas:These guidelines help in assessing the quality of a
relational schema design:
 Clear Semantics of Attributes:Ensure each attribute in a relation has a clear meaning related to the
others. This enables easy interpretation of attribute values in tuples.
Example: In the COMPANY database schema: EMPLOYEE table: Each row represents an employee with
attributes like Ename, Ssn, Bdate, and Dnumber (Department Number).
DEPARTMENT table: Each row represents a department, with attributes like Dmgr_ssn (Manager's SSN),
linking it to the manager.
Guideline: Each tuple in a relation should represent a single entity or relationship.
 Avoid Redundant Information: Redundant Information: Storing the same data in multiple places
wastes storage space and can lead to anomalies.
Example: If the EMP_DEPT table stores Dname (Department Name) and Dmgr_ssn (Manager’s SSN) for

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
42 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

each employee, it’s redundant since these could be stored once in the DEPARTMENT table.
Anomalies:
Insertion: Adding new data might require unnecessary or inconsistent information.
Deletion: Deleting data could unintentionally remove other data.
Modification: Updating one attribute could require changes across multiple rows.
Guideline: Design the schema to avoid insertion, deletion, and update anomalies.
 Minimize NULL Values: NULL Values: Attributes that frequently hold NULLs could be split into
separate relations.
Reasons for NULLs:
Attribute is not applicable.
Attribute value is unknown.
Value exists but is currently unavailable.
Guideline: Design relations to have as few NULL values as possible.
 Prevent Spurious Tuples:
Spurious Tuples: Poor designs can lead to erroneous results when performing JOIN operations.
Lossless Join: Ensures no incorrect data appears when relations are joined.
Guideline: Ensure the schema satisfies the lossless join property to avoid generating spurious tuples.

2. What are the problems caused by Redundancy? Explain about Normalization and

need for normalization.


Problems Caused by Redundancy: Redundancy in a database occurs when the same data is stored in
multiple places, which can cause several issues:
 Storage Wastage: Redundant data consumes unnecessary storage space, as the same information is
repeated in multiple rows or tables.
 Update Anomalies: When data is duplicated, updating one instance requires updating all other
instances to maintain consistency.
Example: If a department's manager changes, this change must be updated in every row where the
department is listed. Failure to do so causes inconsistencies.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
43 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

 Insertion Anomalies: Adding new data may require unnecessary or repetitive information.
Example: To add a new employee, you might also have to insert department details if stored redundantly in
the same table.
 Deletion Anomalies: Removing data can unintentionally delete other important information.
Example: Deleting the last employee in a department might result in the deletion of department details if
they are stored in the same table.
 Modification Anomalies: Updating data in one place but missing it elsewhere leads to inconsistencies
across the database.
Normalization:Normalization is a systematic process of organizing data in a database to reduce
redundancy and improve data integrity. It involves decomposing tables to ensure that each piece of
information is stored only once.
Need for Normalization
 Reduce Redundancy: By eliminating duplicate data, normalization ensures that each fact is
stored in only one place.
 Improve Data Integrity: Since data is not repeated, updates, insertions, and deletions are easier
to manage and less prone to errors.
 Prevent Anomalies:
Update Anomalies are minimized since data changes only need to happen in one place.
Insertion Anomalies are reduced by separating data into logical tables.
Deletion Anomalies are avoided because data relationships are maintained properly.
Efficient Data Organization: Breaking down data into related tables helps streamline queries
and optimizes database performance.

3. Define Functional Dependency. State and prove Arm Strong's inference rules.
4. Discuss Different inference rules of Functional Dependencies

Functional Dependency (FD)


Definition: A Functional Dependency (FD) is a relationship between two attributes, typically between a
primary key and non-key attributes within a relational database. It states that for a given set of attributes, if

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
44 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

two tuples (rows) have the same values for certain attributes, then they must have the same values for
another set of attributes.

Notation: If X and Y are sets of attributes in a relation R, then the functional dependency is represented as:
X→Y
This means that if two tuples have the same value for X, they must also have the same value for Y. In other
words, X uniquely determines Y.
Example: In a table of students, if StudentID determines StudentName, we write it as:
StudentID→StudentName
This means that each unique StudentID corresponds to one StudentName.
Armstrong's Axioms (Inference Rules for Functional Dependencies):Armstrong's Axioms are a set of
sound and complete inference rules used to derive all possible functional dependencies logically implied by
a given set of functional dependencies. These axioms were introduced by William W. Armstrong in 1974.
Armstrong's Axioms
 Reflexivity Rule: If Y is a subset of X, then X → Y.
Example: If X = {A, B} and Y = {A}, then X → Y is valid.
 Augmentation Rule: If X → Y, then XZ → YZ for any Z (where Z is an additional set of attributes).
Example: If StudentID → StudentName, then {StudentID, CourseID} → {StudentName, CourseID} is
valid.
 Transitivity Rule: If X → Y and Y → Z, then X → Z.
Example: If StudentID → StudentName and StudentName → StudentAddress, then StudentID →
StudentAddress.
Additional Inference Rules (Derived from Armstrong's Axioms):
These additional rules can be derived from the three basic axioms (reflexivity, augmentation, and
transitivity):
 Union Rule (Decomposition and Union): If X → Y and X → Z, then X → YZ.
Proof:
From X → Y and using augmentation, we get X → YZ.
From X → Z and using augmentation, we get X → YZ.
 Decomposition Rule (Projectivity): If X → YZ, then X → Y and X → Z.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
45 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Proof:
Since X → YZ, it implies X → Y and X → Z as individual functional dependencies.

 Pseudo-Transitivity Rule: If X → Y and YZ → W, then XZ → W.


Proof:
From X → Y, by augmentation with Z, we get XZ → YZ.
Then, from YZ → W and XZ → YZ, transitivity gives XZ → W.

5. What is Normalization? Explain Advantages? Define 1, 2nd ,3 rd with example


Normalization is a process used in database design to organize data in a way that reduces data
redundancy and improves data integrity. It involves breaking down large tables into smaller, more
manageable tables connected by relationships. The goal is to ensure that the data is stored in a structured,
consistent, and efficient manner.
Importance of Normalization:
Reduced Data Redundancy: Eliminates duplicate data by storing information only once.
Enhanced Data Integrity: Ensures consistent changes across the database, maintaining accuracy.
Improved Data Consistency: Allows efficient querying and analysis by organizing data logically.
Flexibility and Scalability: Makes the database easier to modify and expand as needs change.

Normal forms: Condition using keys and FDs of a relation to certify whether a relation schema is in
a particular normal form
 INF
 2NF, 3NF, BCNF - based on keys and FDs of a relation schema
 4NF-based on keys, multi-valued dependencies : MVDs;
 5NF - based on keys, join dependencies : JDs

First Normal Form :


 First Normal Form (1NF) is the most basic level of normalization.
 It requires that each column in a table must contain atomic values, meaning each cell should
hold a single, indivisible piece of data.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
46 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

 Data should not be stored in repeating groups.


 It Disallows composite attributes,multivalued attributes, nested relations
Data in a table must be atomic (indivisible). No repeating groups or arrays are allowed.
Requirement: Each column should contain only one value.

Example (Not in 1NF):

EmpID EmpName Skills


101 Alice Java, Python
102 Bob C++, SQL

1NF (Atomic Values):

EmpID EmpName Skill


101 Alice Java
101 Alice Python
102 Bob C++
102 Bob SQL
Second Normal Form : it Uses the concepts of FDs, primary key
Second Normal form definition: A given relation is called in Second Normal Form (2NF) if and only if
1. Relation already exists in 1NF.
2. 2. No partial dependency exists in the relation.
Ie; A relation schema R is in second normal form (2NF) if every non-prime attribute A in R is fully
functionally dependent on the primary key
R can be decomposed into 2NF relations via the process of 2NF normalization

Example (Not in 2NF):

EmpID DeptID DeptName


101 10 HR
102 20 IT

Here, DeptName depends only on DeptID and not the whole key (partial dependency).

2NF (Breaking Partial Dependencies):

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
47 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

EMPLOYEE Table: DEPARTMENT Table:


DeptID DeptName
EmpID DeptID 10 HR
101 10 20 IT
102 20

Third Normal Form: A given relation is called in Third Normal Form (3NF) if and only if
1. Relation already exists in 2NF.

2. No transitive dependency exists for non-prime attributes


As we knoe that Transitive functional dependency is a FD X -> Z that can be derived from two FDs X ->
Y and Y -> Z

Example (Not in 3NF):

EmpID DeptID DeptLocation


101 10 New York
102 20 San Francisco

Here, DeptLocation depends on DeptID, not directly on EmpID, leading to a transitive dependency.

3NF (Breaking Transitive Dependencies):

EMPLOYEE Table: DEPARTMENT Table:


EmpID DeptID DeptID DeptLocation
101 10 10 New York
102 20 20 San Francisco

6. Explain with example of Boyce-Codd Normal form ?


A relation is in Boyce-Codd Normal Form (BCNF) if it is in Third Normal Form (3NF) and, for every
functional dependency, the left side is a candidate key.
In simpler terms, BCNF ensures that each non-trivial functional dependency in a relation uses a unique
identifier or key to determine the remaining attributes in a table.
BCNF is a stricter form of 3NF, eliminating anomalies that may still exist in 3NF.
Example:Suppose we have a table that tracks students' enrollment in courses:

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
48 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

StudentID Course Instructor


101 Math Dr. Smith
102 Math Dr. Smith
103 Science Dr. Brown
104 Science Dr. Brown
Analysis of Functional Dependencies:
Functional Dependency 1: (StudentID, Course) → Instructor (each student-course combination has a
specific instructor).
Functional Dependency 2: Course → Instructor (a course is always taught by the same instructor).
Primary Key: Here, (StudentID, Course) could serve as the primary key because it uniquely identifies each
row.
BCNF Violation: The dependency Course → Instructor violates BCNF because:
Course is a determinant (it determines Instructor).
But Course is not a candidate key for the table (it doesn’t uniquely identify each row).
Solution: Decompose into BCNF
To remove the violation, we decompose the table into two tables:
COURSE_INSTRUCTOR Table:
Course Instructor
Math Dr. Smith
Science Dr. Brown

STUDENT_COURSE Table:

StudentID Course
101 Math
102 Math
103 Science
104 Science

Explanation: Now, each table conforms to BCNF:

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
49 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

In COURSE_INSTRUCTOR, Course uniquely determines Instructor, and Course is the primary key.
In STUDENT_COURSE, (StudentID, Course) is the primary key, and there are no non-trivial
dependencies other than the primary key itself.
Benefits of BCNF: Reduces redundancy by removing anomalies. And Makes updates, deletions, and
insertions more efficient and consistent.

7. Explain the following terms: a. Fully functional Dependencies b. Transitive


Dependencies
1. Fully Functional Dependency: A fully functional dependency occurs when an attribute in a table is
functionally dependent on the entire primary key and not just part of it.
This means that if a table has a composite primary key (a primary key made up of two or more attributes),
a fully functional dependency requires that the non-key attribute depends on all parts of that key, not just a
subset. If a non-key attribute is functionally dependent on only a part of the composite primary key, it is
known as a partial dependency, which violates the requirements for Second Normal Form (2NF).
Example:Consider a table StudentCourse with attributes (StudentID, CourseID, Instructor, Grade):

 FD 1: (StudentID, CourseID) → Grade (fully dependent on the whole key)


 FD 2: StudentID → Instructor (partial dependency, as Instructor only depends on StudentID)

In this example, Grade has a fully functional dependency on (StudentID, CourseID), as it depends on both
attributes together, while Instructor only depends on StudentID (a partial dependency). To meet 2NF, we
should remove partial dependencies by decomposing the table.
2. Transitive Dependency:A transitive dependency occurs when a non-key attribute depends on another
non-key attribute rather than directly on the primary key. In other words, if attribute A depends on attribute
B, and B depends on attribute C, then A is transitively dependent on C.
Transitive dependencies often lead to redundancy and anomalies and prevent a table from meeting Third
Normal Form (3NF).
Example: Consider a table EmployeeDept with attributes (EmployeeID, DepartmentID,
DepartmentLocation, DepartmentHead):
FD 1: EmployeeID → DepartmentID

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
50 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

FD2: DepartmentID → DepartmentLocation, DepartmentHead

In this case:DepartmentLocation and DepartmentHead depend on DepartmentID, which in turn depends on


EmployeeID.

This creates a transitive dependency, as DepartmentLocation and DepartmentHead rely on EmployeeID


through DepartmentID.To resolve this, we would split the table to remove transitive dependencies,
ensuring each non-key attribute depends only on the primary key directly. This would result in two tables:
EmployeeDept: (EmployeeID, DepartmentID) DepartmentInfo: (DepartmentID, DepartmentLocation,
DepartmentHead) .This decomposition brings the data into 3NF by eliminating transitive dependencies.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
51 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Database Management System


Course Code 22MCA22 Semester II
MODULE – 05
SYLLABUS CONTENT
Transaction Processing: Introduction to Transaction Processing, Transaction and System concepts,
Desirable properties of Transactions, Characterizing schedules based on recoverability, Characterizing
schedules based on Serializability, Transaction support in SQL. Concurrency Control in Databases:
Two-phase locking techniques for Concurrency control, Concurrency control based on Timestamp
ordering, Multiversion Concurrency control techniques, Validation Concurrency control techniques,
Granularity of Data items and Multiple Granularity Locking. Textbook 1: Ch 20.1 to
20.6, 21.1 to 21.7;
Reference:Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th
Edition, 2017, Pearson.
1. Define a transaction in the context of database systems. Explain the ACID
properties that characterize a transaction.
Definition of a Transaction: In the context of database systems, a transaction is defined as a set of
logically related operations that are treated as a single unit of work. Transactions are essential for ensuring
the integrity of a database, especially when multiple operations need to be executed together to achieve a
desired outcome. For example, when transferring money from one bank account to another, a transaction
might include the following steps:
Read the balance of the sender's account.
Deduct the amount from the sender's account.
Write the updated balance back to the sender's account.
Read the balance of the recipient's account.
Add the amount to the recipient's account.
Write the new balance back to the recipient's account.
This entire sequence of operations is considered a single transaction because it must either complete all

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
52 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

steps successfully or none at all. If any step fails, the transaction must be rolled back to maintain database
consistency.
ACID Properties of Transactions: The ACID properties are crucial for maintaining the reliability,
consistency, and integrity of transactions in a database system. They ensure that even in the presence of
failures, concurrent executions, or other unexpected events, the database remains in a consistent state.
To ensure that transactions are processed reliably, they are characterized by four key properties known as
ACID:
Atomicity: This property states that a transaction must be treated as an atomic unit; that is, either all of its
operations are executed successfully, or none of them are executed at all. In other words, a transaction is
"all or nothing."
Example: If the money transfer transaction described above fails after deducting the amount from the
sender's account but before crediting it to the recipient's account, atomicity ensures that the deduction is
rolled back. As a result, the sender's balance remains unchanged, preventing any partial completion of the
transaction.
Consistency: The consistency property ensures that a transaction takes the database from one consistent
state to another consistent state. It mandates that all data integrity constraints must be maintained.
Example: In the money transfer example, if the sender’s account has sufficient funds before the transaction,
the database should remain consistent after the transaction. The total amount of money in the system before
and after the transaction should be the same.
Isolation: Isolation ensures that transactions are executed independently of one another. Even if multiple
transactions are running concurrently, the results of each transaction should not be visible to others until
they are committed.
Example: If two users are transferring money simultaneously, isolation ensures that the operations of one
transaction do not affect the operations of another. Each transaction appears to run in isolation, which
prevents any interference that could lead to inconsistent results.
Durability:The durability property guarantees that once a transaction has been committed, its changes are
permanent, even in the event of a system failure. Once the database confirms that a transaction has been
completed, the changes it made are stored in a way that survives crashes and restarts.
Example: After the money transfer transaction is successfully committed, even if a power failure occurs
immediately afterward, the changes (i.e., updated balances of both accounts) will be saved in the database.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
53 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

The system will retain these updates when it restarts.

2. Discuss the characterizing schedule based on recoverablility


Recoverability in Concurrent Transaction Execution: Recoverability is a crucial aspect of database
transactions, particularly in multi-user environments where concurrent execution is common. A schedule is
considered recoverable if, whenever a transaction reads a data item written by another transaction, that
transaction must either be committed before the reading transaction commits or must be rolled back if it
fails. This ensures that the database can return to a consistent state if a transaction fails.
the characterizing schedule based on recoverability is critical for maintaining database consistency during
concurrent transaction execution. By ensuring that transactions respect the rules of recoverability—whether
through recoverable, non-recoverable, cascading, or strict schedules—databases can achieve high
throughput while avoiding the pitfalls of concurrency issues. Proper scheduling and concurrency control
mechanisms are essential for the reliability and integrity of database systems in multi-user environments.
Types of Schedules Based on Recoverability:
 Recoverable Schedule

 Non-recoverable Schedule

Recoverable Schedule: A schedule is recoverable if it ensures that no transaction reads data written by
another uncommitted transaction.
Example: Consider two transactions, T1 and T2.
If T1 writes to a data item A and T2 reads A, T2 must not commit until T1 has committed.
If T1 fails and is rolled back, T2 should also roll back to maintain consistency.
Non-Recoverable Schedule: In a non-recoverable schedule, a transaction may read data written by another
uncommitted transaction.
Example: If T1 writes to data item A, and T2 reads A before T1 commits, then if T1 fails and rolls back,
T2 may have committed using an invalid value, resulting in inconsistencies.
Such schedules can lead to the "lost update" or "dirty read" problems discussed earlier.
Cascading Schedule: A cascading schedule is one where the rollback of one transaction may cause other
transactions to also rollback.
Example: If T1 writes to A and T2 reads A before T1 commits, if T1 fails, T2 must also be rolled back.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
54 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

This can lead to performance issues and should be avoided in practice, as it creates a chain reaction of
rollbacks.
Strict Schedule: A strict schedule prohibits any transaction from reading or writing data written by another
transaction until that transaction has been committed.
This is the safest type of scheduling for ensuring recoverability, as it eliminates the possibility of cascading
rollbacks.
Importance of Recoverability
Data Integrity: Ensuring that transactions do not interfere with each other's uncommitted changes helps
maintain the integrity and consistency of the database.
System Stability: Recoverability helps the system recover from failures without compromising data
correctness. It minimizes the potential for inconsistencies that can arise from concurrent transaction
execution.
Conflict Management: By managing read and write operations carefully, recoverability mechanisms help
prevent common concurrency problems like lost updates and dirty reads.

3. Describe the two-phase locking technique for concurrency control in databases. Explain
how it ensures serializability.
Two-Phase Locking (2PL) Technique for Concurrency Control in Databases:Two-phase locking (2PL)
is a concurrency control protocol used in database management systems to ensure that transactions are
executed in a manner that maintains database consistency and ensures serializability. The protocol divides
the execution of a transaction into two distinct phases: the growing phase and the shrinking phase.
Phases of Two-Phase Locking
 Growing Phase:
1. In this phase, a transaction may acquire any number of locks on data items it needs to access, but it
cannot release any locks.
2. As the transaction progresses, it requests and obtains the necessary locks to perform read or write
operations on the data.
3. The goal is to gather all the required locks before proceeding to the next phase.
 Shrinking Phase:

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
55 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

1. Once a transaction releases its first lock, it enters the shrinking phase. During this phase, the
transaction can only release locks; it cannot acquire any new locks.
2. This restriction ensures that once a transaction starts releasing locks, it is moving toward completion.
Lock Conversion: In 2PL, lock conversion is allowed:
 Upgrading Lock: In the growing phase, a transaction can upgrade a shared lock (S) to an exclusive
lock (X) on a data item. For example, if a transaction holds a shared lock S(a) on a data item and
later requires an exclusive lock, it can upgrade it to X(a).
 Downgrading Lock: In the shrinking phase, if a transaction holds an exclusive lock (X) on a data
item and wants to downgrade it to a shared lock (S), this must occur during the shrinking phase.

Example of Two-Phase Locking


Transaction T1:
 Growing Phase: Steps 1-3 (acquiring locks)
 Lock Point: Step 3 (the point where the transaction has acquired all necessary locks)
 Shrinking Phase: Steps 5-7 (releasing locks)
Transaction T2:
 Growing Phase: Steps 2-6 (acquiring locks)
 Lock Point: Step 6
 Shrinking Phase: Steps 8-9 (releasing locks)
Strict Two-Phase Locking (Strict-2PL): Strict-2PL is a variant of the standard 2PL protocol. The primary
distinction lies in how and when locks are released:
 First Phase: Similar to 2PL, where all required locks are acquired during the growing phase.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
56 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

 Lock Release: In Strict-2PL, a transaction does not release any locks until the transaction is fully
committed. This means that all locks are held until the end of the transaction, eliminating the
shrinking phase.
This approach guarantees that no other transaction can access the data items that are locked by the
transaction until it has completed, ensuring a higher level of consistency.
Ensuring Serializability: Two-phase locking ensures serializability, which means that the outcome of
executing transactions concurrently is equivalent to some serial execution of those transactions. Here’s
how 2PL achieves this:
 No New Locks in Shrinking Phase: Since no new locks can be acquired in the shrinking phase, once a
transaction starts releasing locks, it is guaranteed that the operations can no longer interfere with other
transactions.
 Lock Point: The concept of a lock point (the moment when a transaction acquires its last lock) helps
establish a clear boundary between the growing and shrinking phases, ensuring that any locks held by
other transactions are respected.
 Conflict Serializability: Transactions are structured in such a way that if two transactions access the
same data items, they will do so in a consistent manner, preventing anomalies such as lost updates or
dirty reads.
 Strict-2PL Advantage: By delaying lock releases until a transaction is completely finished, Strict-2PL
further enhances the guarantee of serializability. Since locks are not released until commit, it prevents
cascading rollbacks, where the failure of one transaction might necessitate rolling back others.

4. Explain concurrency control based on Timestamp ordering techniques in databases.


Timestamp ordering is a concurrency control technique used in databases to ensure that transactions are
executed in a serializable manner. Here’s a simple overview of how it works:
Timestamp Assignment: Each transaction is assigned a unique timestamp when it starts. This timestamp
is used to determine the order of execution for the transactions.
Read and Write Operations:When a transaction wants to read a data item, it checks the timestamp of the
last write operation on that item. If the timestamp of the transaction is earlier than the write operation, the
read is allowed; otherwise, it is aborted.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
57 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

When a transaction wants to write to a data item, it checks the timestamps of any existing transactions that
have read or written that item. If there are conflicts (i.e., another transaction with an earlier timestamp has
written to the item), the transaction is aborted.
Let's assume there are two transactions T1 and T2. Suppose the transaction T1 has entered the system at
007 times and transaction T2 has entered the system at 009 times. T1 has the higher priority, so it executes
first as it is entered the system first.
The timestamp ordering protocol also maintains the timestamp of last 'read' and 'write'
operation on a data.
Basic Timestamp ordering protocol works as follows:
1. Check the following condition whenever a transaction Ti issues a Read (X) operation:
If W_TS(X) >TS(Ti) then the operation is rejected.
If W_TS(X) <= TS(Ti) then the operation is executed.
Timestamps of all the data items are updated.
2. Check the following condition whenever a transaction Ti issues a Write(X) operation:
If TS(Ti) < R_TS(X) then the operation is rejected.
If TS(Ti) < W_TS(X) then the operation is rejected and Ti is rolled back otherwise the operation is
executed.
Where,
TS(TI) denotes the timestamp of the transaction Ti.
R_TS(X) denotes the Read time-stamp of data-item X.
W_TS(X) denotes the Write time-stamp of data-item X.

5. Explain the concept of granularity of data items in the context of concurrency


control. How does multiple granularity locking improve concurrency in databases?
Granularity of Data Items in Concurrency Control: Granularity refers to the size of the data item that
can be locked in a database during concurrent operations. The granularity can range from fine-grained
locks (locking individual records) to coarse-grained locks (locking entire tables or databases).
Multiple Granularity Locking: Multiple Granularity Locking is a technique that organizes the database
into a hierarchical structure, allowing locks to be applied at various levels of granularity.Multiple

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
58 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Granularity Locking allows a more efficient concurrency control mechanism by providing flexibility in the
size of locks. It reduces contention for resources, improves concurrency by allowing multiple transactions
to access different parts of the database simultaneously, and ultimately enhances the overall performance of
database operations.
Here’s how it works and improves concurrency:
Hierarchical Structure: The database is divided into different levels (or granularity levels), which can be
visualized as a tree. For example:
Root Level: Entire database
Second Level: Areas (logical divisions of the database)
Third Level: Files (collections of records)
Fourth Level: Records (individual data entries)
Locking Mechanism: In this structure, you can lock at different levels. For instance, locking an entire area
would also implicitly lock all files and records within that area.
Conversely, if you lock a specific record, you do not lock the entire file or area, allowing other transactions
to access other records within the same file or area concurrently.
Improved Concurrency:
 Reduced Lock Overhead: By allowing locks at different levels, Multiple Granularity Locking
reduces the overhead of managing locks, as fewer locks need to be held simultaneously.
 Increased Flexibility: It provides flexibility in how locks are acquired and released, enabling
transactions to work with only the specific data they need without locking larger sections unnecessarily.
 Better Resource Utilization: Multiple transactions can work on different levels of the database
simultaneously, improving resource utilization and overall throughput.

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
59 | P a g e
ACHARYA INSTITUTE OF TECHNOLOGY
DEPARTMENT OF MCA

Reference books:
1. Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th Edition, 2017,
Pearson.
2. Database management systems, Ramakrishnan, and Gehrke, 3rd Edition, 2014, McGraw Hill.
3. Abraham Silberschatz, Henry F. Korth and S. Sudarshan‟s Database System Concepts 6th EditionTata
Mcgraw Hill

Ms.SHAHEENA K V – Database Management System – Department of MCA


Intended for internal circulation only
60 | P a g e

You might also like