DBMS
DBMS
PART-A
1) What is DBMS?
Ans: -DBMS is a collection of programs that facilitates users to create and maintain a database.
In other words, DBMS provides us an interface or tool for performing different operations such
as the creation of a database, inserting data into it, deleting data from it, updating the data, etc.
DBMS is a software in which data is stored in a more secure way as compared to the file-based
system. Using DBMS, we can overcome many problems such as- data redundancy, data
inconsistency, easy access, more organized and understandable, and so on.
Ans:- There are many advantages of DBMS these are the following: -
Redundancy control
Easy accessibility
Ans: -RDBMS stands for Relational Database Management Systems. It is used to maintain the
data records and indices in tables. RDBMS is the form of DBMS which uses the structure to
identify and access data concerning the other piece of data in the database. RDBMS is the
system that enables you to perform different operations such as- update, insert, delete,
manipulate and administer a relational database with minimal difficulties. Most of the time
RDBMS use SQL language because it is easily understandable and is used for often.
Ans:- Query optimization is the phase that identifies a plan for evaluation query that has the
least estimated cost. This phase comes into the picture when there are a lot of algorithms and
methods to execute the same task.
Ans:-
Aggregation Atomicity
This property states that a database
This is a feature of the E-R model which modification must either follow all the rules
allows a relationship set to participate in or nothing at all. So, if one part of the
another relationship set. transaction fails, then the entire transaction
fails.
Q.6 Do we consider NULL values the same as that of blank space or zero?
Ans: -A NULL value is not at all same as that of zero or a blank space. The NULL value
represents a value which is unavailable, unknown, assigned or not applicable whereas zero is
a number and blank space is a character.
Q.7 Define a Relation Schema and a Relation.
Ans: -A Relation Schema is specified as a set of attributes. It is also known as table schema. It
defines what the name of the table is. Relation schema is known as the blueprint with the help
of which we can explain that how the data is organized into tables. This blueprint contains no
data.
A relation is specified as a set of tuples. A relation is the set of related attributes with identifying
key attributes.
Ans: -The Relationship is defined as an association among two or more entities. There are three
type of relationships in DBMS-
One-To-One: Here one record of any object can be related to one record of another object.
One-To-Many (many-to-one): Here one record of any object can be related to many records of
other object and vice versa.
Many-to-many: Here more than one records of an object can be related to n number of records
of another object.
Q.9 What is DDL (Data Definition Language) and DML (Data Manipulation Language)?
Ans:- Data Definition Language (DDL) is a standard for commands which defines the different
structures in a database. Most commonly DDL statements are CREATE, ALTER, and DROP.
These commands are used for updating data into the database.
Data Manipulation Language (DML) is a language that enables the user to access or manipulate
data as organized by the appropriate data model. For example- SELECT, UPDATE, INSERT,
DELETE.
Q.10 What is Relational Algebra?
Ans: - Relational Algebra is a Procedural Query Language which contains a set of operations
that take one or two relations as input and produce a new relationship. Relational algebra is the
basic set of operations for the relational model. The decisive point of relational algebra is that
it is similar to the algebra which operates on the number.
Q.12 What do you understand by the terms Entity, Entity Type, and Entity Set in DBMS?
Ans :- Entity: An entity is a real-world object having attributes, which are nothing but
characteristics of that particular object. For example, an employee can be an entity. This
particular entity can have attributes such as empid, empname, etc.
Entity Type: Entity type is nothing but a collection of entities, having the same attributes.
Generally, an entity type refers to one or more related tables in a particular database. So, you
can understand, entity type as a characteristic which uniquely identifies the entity. For example,
An employee can have attributes such as empid, empname, department, etc.
Entity Set: An entity set is the collection of all the entities of a particular entity type in a
database. For example, a set of employees, a set of companies, and a set of people can come
under an entity set.
Ans :-Normalization is a process of analysing the given relation schemas according to their
functional dependencies. It is used to minimize redundancy and also used to minimize
insertion, deletion and update distractions. Normalization is considered as an essential process
as it is used to avoid data redundancy, insertion anomaly, updation anomaly, deletion anomaly.
Ans :- Functional Dependency is the starting point of normalization. It exists when a relation
between two attributes allow you to determine the corresponding attribute's value uniquely.
The functional dependency is also known as database dependency and defines as the
relationship which occurs when one attribute in a relation uniquely determines another
attribute. It is written as A->B which means B is functionally dependent on A.
Ans :- E-R model is a short name for the Entity-Relationship model. This model is based on
the real world. It contains necessary objects (known as entities) and the relationship among
these objects. Here the primary objects are the entity, attribute of that entity, relationship set,
an attribute of that relationship set can be mapped in the form of E-R diagram.
Ans :- There are different types of Keys, that can be considered in a database.
Candidate Key – This is a set of attributes which can uniquely identify a table. Each table can
have more than a candidate key.
Super Key – This is a set of attributes which can uniquely identify a tuple. So, a unique
key is a super key.
Primary Key – This is a set of attributes which are used to uniquely identify every tuple.
Foreign Key – An attribute that can only take the values present as the values of some other
attribute, is the foreign key to the attribute to which it refers.
Q.18 What do you understand by functional dependency and transitive dependency in
DBMS?
Example: Consider a relation “A1” having attributes X and Y. The functional dependency
among these two attributes will be X -> Y, this implies that Y is functionally dependent on X.
Example: Consider a relation “A1” having attributes X, Y and Z. Now, X->Z is said to hold
transitive dependency, only if the following functional dependencies holds true:
Q.20 What are the different integrity rules present in the DBMS?
Entity Integrity: This rule states that the value of the primary key can never be NULL. So, all
the tuples in the column identified as the primary key should have a value.
Referential Integrity: This rule states that either the value of the foreign key is NULL or it
should be the primary key of any other relation.
Q.21 What is 1NF?
Ans :- 1NF is the First Normal Form. It is the simplest type of normalization that you can
implement in a database. The primary objectives of 1NF are to:
Create separate tables for each group of related data and identify each row with a unique
column.
Q.22 What is the difference between a DELETE command and TRUNCATE command?
Ans :- DELETE command: DELETE command is used to delete rows from a table based on
the condition that we provide in a WHERE clause.
DELETE command delete only those rows which are specified with the WHERE
clause.
DELETE command can be rolled back.
TRUNCATE command: TRUNCATE command is used to remove all rows (complete data)
from a table. It is similar to the DELETE command with no WHERE clause.
The TRUNCATE command removes all the rows from the table.
The TRUNCATE command cannot be rolled back.
Q.23 What is the difference between a shared lock and exclusive lock?
Ans :- Shared lock: Shared lock is required for reading a data item. In the shared lock, many
transactions may hold a lock on the same data item. When more than one transaction is allowed
to read the data items then that is known as the shared lock.
Exclusive lock: When any transaction is about to perform the write operation, then the lock on
the data item is an exclusive lock. Because, if we allow more than one transaction then that
will lead to the inconsistency in the database.
Q.24 What are the differences between UNION and UNION ALL
Ans :-
Q.25 What are the differences between HAVING and WHERE clause?
Ans :-
HAVING WHERE
Used only with SELECT statement Used in a GROUP BY clause
Used with the GROUP BY function in a Applied to each row before they are a part of
query the GROUP BY function in a query
Ans :- Java Database Connectivity (JDBC) is an application programming interface i.e. (API).
JDBC was released as a part of Java development Kit (JDK) 1.1. in the year 1996 by SUN
Microsoft.
It is a standard interface between any Java application and different databases. The function of
JDBC is to help the Java-based application to access different types of databases. JDBC provide
methods to query database, and it can also be used to update the database. JDBC provide JDBC
drivers that converts the request from Java application on client side to the language that
database understands.
Ans :-Trigger is like a procedure that is automatically invoked by the DBMS in response to
specified changes to data base. Trigger is like a ‘Daemon that monitors a data base, and is
executed when the data base is modified in a way that matches the event specification. A data
base that has a set of associated triggers is called an active data base.
Q.28 Define “correlated subqueries”.
Ans:- A ‘correlated subquery’ is a sort of sub query but correlated subquery is reliant on another
query for a value that is returned. In case of execution, the sub query is executed first and then
the correlated query.
Ans :-Joins help in explaining the relation between different tables. They also enable you to
select data with relation to data in another table.
INNER JOINs: Blank rows are left in the middle while more than equal to two tables are
joined.
OUTER JOINs: Divided into Left Outer Join and Right Outer Join. Blank rows are left at
the specified side by joining tables in other side.
Ans :- A Database Transaction is a logical unit of processing in a DBMS which entails one or
more database access operation. Database transactions represent real-world events of any
enterprise.
All types of database access operation which are held between the beginning and end
transaction statements are considered as a single logical transaction in DBMS. During the
transaction the database is inconsistent. Only once the database is committed the state is
changed from one consistent state to another.
Ans :- A database is a shared resource accessed. It is used by many users and processes
concurrently. For example, the banking system, railway, and air reservations systems, stock
market monitoring, supermarket inventory, and checkouts, etc.
A transaction is an action or series of actions that are being performed by a single user or
application program, which reads or updates the contents of the database
Q.32 Explain the Committed and Aborted State in Database Transaction.
Ans :-
Committed State
If a transaction completes the execution successfully then all the changes made in the local
memory during partially committed state are permanently stored in the database. You can
also see in the above diagram that a transaction goes from partially committed state to
committed state when everything is successful.
Aborted State
if a transaction fails during execution then the transaction goes into a failed state. The
changes made into the local memory (or buffer) are rolled back to the previous consistent
state and the transaction goes into aborted state from the failed state. Refer the diagram to
see the interaction between failed and aborted state.
Read Operation
Write Operation
1. Read Operation
Read operation reads the data from the database and then stores it in the buffer in main
memory.
For example- Read(A) instruction will read the value of A from the database and will store it
in the buffer in main memory.
2. Write Operation
Write operation writes the updated data value back to the database from the buffer.
For example- Write(A) will write the updated value of A from the buffer to the database.
Q. 34 What is serializability?
2. View Serializability
1. Conflict Serializability
It deals with detecting the instructions that are conflicting in any way and specifying
the order in which the instructions should execute in case there is any conflict.
2. View Serializability
2. It can be derived by creating another schedule out of an existing schedule and involves
the same set of transactions.
Q.36 Mention the issues with traditional file-based systems that make DBMS a better
choice?
Ans:- The absence of indexing in a traditional file-based system leaves us with the only option
of scanning the full page and hence making the access of content tedious and super slow. The
other issue is redundancy and inconsistency as files have many duplicate and redundant data
and changing one of them makes all of them inconsistent. Accessing data is harder in traditional
file-based systems because data is unorganized in them.
Another issue is the lack of concurrency control, which leads to one operation locking the entire
page, as compared to DBMS where multiple operations can work on a single file
simultaneously.
Integrity check, data isolation, atomicity, security, etc. are some other issues with traditional
file-based systems for which DBMSs have provided some good solutions.
Ans :- DDL(Data Definition Language): It contains commands which are required to define
the database.
DCL(Data Control Language): It contains commands which are required to deal with the
user permissions and controls of the database system.
Ans:- Concurrency control concept comes under the Transaction in database management
system (DBMS). It is a procedure in DBMS which helps us for the management of two
simultaneous processes to execute without conflicts between each other, these conflicts
occur in multi user systems.
Lock Based Protocols in DBMS is a mechanism in which a transaction cannot Read or Write
the data until it acquires an appropriate lock. Lock based protocols help to eliminate the
concurrency problem in DBMS for simultaneous transactions by locking or isolating a
particular transaction to a single user.
A lock is a data variable which is associated with a data item. This lock signifies that operations
that can be performed on the data item. Locks in DBMS help synchronize access to the database
items by concurrent transactions.
Ans:- Growing phase: In the growing phase, a new lock on the data item may be acquired by
the transaction, but none can be released.
Shrinking phase: In the shrinking phase, existing lock held by the transaction may be
released, but no new locks can be acquired.
Part -B
Q.1 What is a Database Management System and how is it different from a File System?
Ans: -Traditionally, the file system was used for data management. Data was stored in files and
users access those files directly by going at the location of the file physically. But things
become harder when the amount of data stored in the files get increased.
How will you search a file from millions of files available to you? Will you go one by one to
each file and search for your file? No. It is very tedious and next to impossible process.
File System
The file system is a traditional way of storing data where the user saves the file manually at
any location. It is an easy way to store texts, audio, video but here security is a major concern.
The options which are given by the operating system in the name of security are very limited.
Only options which we have are locks and hidden files. Also, these files are very hard to
maintain and users manually go to these locations to access these files.
Basis DBMS Approach File System Approach
Meaning DBMS is a collection of data. In The file system is a collection of data. In this
DBMS, the user is not required to system, the user has to write the procedures
write the procedures. for managing the database.
Sharing of data Due to the centralized approach, Data is distributed in many files, and it may
data sharing is easy. be of different formats, so it isn't easy to
share data.
Data DBMS gives an abstract view of The file system provides the detail of the data
Abstraction data that hides the details. representation and storage of data.
Security and DBMS provides a good It isn't easy to protect a file under the file
Protection protection mechanism. system.
Recovery DBMS provides a crash recovery The file system doesn't have a crash
Mechanism mechanism, i.e., DBMS protects mechanism, i.e., if the system crashes while
the user from system failure. entering some data, then the content of the
file will be lost.
Manipulation DBMS contains a wide variety of The file system can't efficiently store and
Techniques sophisticated techniques to retrieve the data.
store and retrieve the data.
Ans: - A database system provides an ultimate view of the stored data. However, data in the
form of bits, bytes get stored in different storage devices.
Various types of storage devices that are used for accessing and storing data.
For storing the data, there are different types of storage options available. These storage
types differ from one another as per the speed and accessibility. There are the following types
of storage devices used for storing the data:
Primary Storage
Secondary Storage
Tertiary Storage
Primary Storage − The memory storage that is directly accessible to the CPU comes under this
category. CPU's internal memory (registers), fast memory (cache), and main memory (RAM)
are directly accessible to the CPU, as they are all placed on the motherboard or CPU chipset.
This storage is typically very small, ultra-fast, and volatile. Primary storage requires
continuous power supply in order to maintain its state. In case of a power failure, all its data
is lost.
Secondary Storage − Secondary storage devices are used to store data for future use or as
backup. Secondary storage includes memory devices that are not a part of the CPU chipset or
motherboard, for example, magnetic disks, optical disks (DVD, CD, etc.), hard disks, flash
drives, and magnetic tapes.
Tertiary Storage − Tertiary storage is used to store huge volumes of data. Since such storage
devices are external to the computer system, they are the slowest in speed. These storage
devices are mostly used to take the back up of an entire system. Optical disks and magnetic
tapes are widely used as tertiary storage.
Q.3 Explain the concept of Structure in DBMS with the help of Diagram.
Ans:- Database Management System (DBMS) is a software that allows access to data stored
in a database and provides an easy and effective method of –
End User: – They are the real users of the database. They can be developers, designers,
administrators, or the actual users of the database.
DDL: – Data Definition Language (DDL) is a query fired to create database, schema, tables,
mappings, etc in the database. These are the commands used to create objects like tables,
indexes in the database for the first time. In other words, they create the structure of the
database.
DDL Compiler: – This part of the database is responsible for processing the DDL commands.
That means this compiler actually breaks down the command into machine-understandable
codes. It is also responsible for storing the metadata information like table name, space used
by it, number of columns in it, mapping information, etc.
DML Compiler: – When the user inserts, deletes, updates or retrieves the record from the
database, he will be sending requests which he understands by pressing some buttons. But
for the database to work/understand the request, it should be broken down to object code.
Query Optimizer: – When a user fires some requests, he is least bothered how it will be fired
on the database.
Storage Manager:
Storage Manager is a program that provides an interface between the data stored in the
database and the queries received. It is also known as Database Control System.
Q.4 What is Participation? explain the types of participation with the help of diagram.
This constraint specifies the number of instances of an entity that can participate in a
relationship type.
Total Participation
Each entity in the entity set is involved in at least one relationship in a relationship set i.e. the
number of relationships in every entity is involved is greater than 0.
Consider two entities Employee and Department related via Works_For relationship. Now,
every Employee works in at least one department therefore an Employee entity exist if it has
at least one Works_For relationship with Department entity. Thus, the participation of
Employee in Works_For is total relationship.
Partial Participation
Each entity in entity set may or may not occur in at least one relationship in a relationship set.
For example: Consider two entities Employee and Department and they are related to each
other via Manages relationship. An Employee must manage a Department, he or she could be
the head of the department. But not every Employee in the company manages the
department. So, participation of employee in the Manages relationship type is partial i.e. only
a particular set of Employees will manage the Department but not all.
Q.5 Define Class Hierarchy with their types.
The ER Model has the power of expressing database entities in a conceptual hierarchical
manner. As the hierarchy goes up, it generalizes the view of entities, and as we go deep in the
hierarchy, it gives us the detail of every entity included.
Going up in this structure is called generalization, where entities are clubbed together to
represent a more generalized view. For example, a particular student named Mira can be
generalized along with all the students. The entity shall be a student, and further, the student
is a person. The reverse is called specialization where a person is a student, and that student
is Mira.
Generalization
Generalization is a bottom-up approach in which two lower level entities combine to form
a higher-level entity. In generalization, the higher-level entity can also combine with other
lower level entities to make further higher-level entity. It’s more like Superclass and
Subclass system, but the only difference is the approach, which is bottom-up. Hence,
entities are combined to form a more generalised entity, in other words, sub-classes are
combined to form a super-class.
For example, Saving and Current account types entities can be generalised and an entity with
name Account can be created, which covers both.
Specialization
Aggregation
Aggregation is a process when relation between two entities is treated as a single entity.
In the diagram above, the relationship between Centre and Course together, is acting as an
Entity, which is in relationship with another entity Visitor. Now in real world, if a Visitor or a
Student visits a Coaching Centre, he/she will never enquire about the centre only or just about
the course, rather he/she will ask enquire about both.
A strong entity is an entity type whose existence doesn't depend on any other entity. Strong
entity types have a key attribute. The key attribute helps in identifying each entity uniquely. It
is represented by a rectangle. A relationship between two strong entities is represented by a
diamond shape. It is simply called a relationship.
For Example: Suppose we have two entity type DOCTOR and PATIENT. Entity DOCTOR
has attributes Doctor_id, Name, Salary and Qualification. Similarly, entity PATIENT has
attributes as Patient_id, Age, Contact_no, Address and P_name. Both are strong entity type as
both of them have their own key attribute. Doctor_id is the key attribute for entity DOCTOR
and Patient_id is the key attribute for entity type PATIENT. Both these entities have a
relationship which is a one-to-many relationship. Each doctor many treat more than one patient.
Here, the relationship and the entity type are connected by a single line. Also, it may be possible
that all the doctors are related to some patients. It may be possible that some doctors are not
related to any patients. So, there may or may not be total participation of a strong entity in a
relationship.
Weak Entity
Weak entity type doesn’t have a key attribute. Weak entity type can’t be identified on its own.
It depends upon some other strong entity for its distinct identity. It is represented by a double
outlined rectangle. This can be understood with real-life examples. There can be children only
if the parent exits. There can be no independent existence of children. There can be a room
only if building exits. There can be no independent existence of a room.
Example: Suppose we have two entity type EMPLOYEES and DEPENDANT (children of the
employee). The EMPLOYEE entity has attributes as Emp_id, Emp_name, Job_title, Age,
Salary, Address. And, the DEPENDANT has attributes as Name, Age and Relation. So,
Emp_id is the key attribute of the EMPLOYEES entity type. So, EMPLOYEES is a strong
entity and DEPENDANT is a weak entity.
Embedded or Static SQL is those SQL statements that are fixed and can't be changed at runtime
in an application. These statements are compiled at the compile-time only. The benefit of using
this statement is that you know the path of execution of statements because you have the SQL
statements with you, so you can optimize your SQL query and can execute the query in the
best and fastest possible way. The way of accessing the data is predefined and these static SQL
statements are generally used on those databases that are uniformly distributed.
These statements are hardcoded in the application, so if you want to build some application in
which you need some dynamic or run-time SQL statements, then you should use the Dynamic
SQL statement.
Dynamic SQL
Dynamic SQL statements are those SQL statements that are created or executed at the run-
time. The users can execute their own query in some application. These statements are
compiled at the run-time. These kinds of SQL statements are used where there is a non-
uniformity in the data stored in the database. It is more flexible as compared to the static SQL
and can be used in some flexible applications.
Since the compilation is done at run-time, the system will know how to access the database at
run-time only. So, no proper planning for execution and optimization can be done previously.
This will reduce the performance of the system. Also, if you are taking the database query from
the user at run-time, then there are possibilities that the users might enter some wrong queries
and this is very dangerous because here you are dealing with lots and lots of data.
Ans:- In simple terms, we can explain query as questions or doubts. Query means finding
information, asking questions about something, especially in order to express one's doubts
about it or to check its validity or accuracy.
A Query is used to traverse over some data may be of small or large quantity to find the needed
information.
Data is the facts and figures stored digitally or physically at a location. The data provides us
with the base on which we run or perform our query. Books in a library can be considered as
data. Information on the web can be considered as data. To look for the desired information in
this heap of data is called a query.
We perform many queries in our day to day life. Asking questions is a form of query. Searching
info on the web is a form of query. Looking for places on Map is a form of query. In each of
these examples, we are looking for the required information in different ways. For asking
questions we provide query in the form of voice while searching the web we enter keywords
while looking for places on the map we enter the location. This is called a query language. A
query language is a language which is used to specify our need in the retrieval procedures of
information.
A database query can be of two types:
SELECT Query
ACTION Query.
SELECT Query: A select query is used to retrieve data from a single table of a combination
of multiple tables.
SQL uses a SELECT statement to select, or extract, specific data based on the given base
condition.
ACTION Query: The action query is used to perform operations on the database such as
insertion, deletion, modification, alteration, etc.
Subquery
A Subquery is a type of query which is written inside another query. A subquery becomes a
part of a larger query. A subquery is also called INNER QUERY OR NESTED QUERY. A
subquery provides data to the main query also called the parent query or outer query.
SELECT clause
FROM Clause
WHERE Clause
HAVING Clause
Types of Subquery
Correlated subquery
Ans:- A database is used by many users and processes at the same time as it is a shared resource.
The IRCTC website is used at the same time by millions of users. Many people are concurrently
booking tickets but how is such a large database always consistent. The transactions that we
do on such websites are concurrently managed.
There are various properties through which the transactions occurring in the database are
managed. These properties are the ACID properties.
Atomicity
This property states that the transaction should either occur completely or doesn't occur at all.
The transaction should not occur partially. Each transaction is treated as a unit and the
execution is completed else the transaction is aborted. If any transaction is aborted all the
changes made are reversed back. If the transaction occurs completely then only it is committed.
Consistency
This property ensures that the integrity of the database is maintained before and after the
transaction. It ensures that when any transaction is executed then the database should move
from one consistent state to another consistent state.
Isolation
This property tells that each transaction is executed in the system such that it is the only
transaction in the system. If more than one transaction is taking place in parallel, then the
occurrence of one transaction will not affect the other transaction. If any transaction is using a
data item then it can’t be used by other transactions until the first transaction ends.
Durability
This property ensures that once the changes are made in the database these changes persist in
the database even if any system failure occurs. These changes are saved permanently in the
non-volatile memory. It is the responsibility of the recovery manager to ensure the durability
in the database.
Q.10 What is concurrent execution and what types of problem arises in DBMS?
In a multi-user system, multiple users can access and use the same database at one time,
which is known as the concurrent execution of the database. It means that the same database
is executed simultaneously on a multi-user system by different users.
While working on the database transactions, there occurs the requirement of using the
database by multiple users for performing different operations, and in that case, concurrent
execution of the database is performed.
The thing is that the simultaneous execution that is performed should be done in an proper
manner and no operation should affect the other executing operations, thus maintaining the
consistency of the database.
Thus, on making the concurrent execution of the transaction operations, there occur several
challenging problems that need to be solved.
In a database transaction, the two main operations are READ and WRITE operations. So, there
is a need to manage these two operations in the concurrent execution of the transactions as
if these operations are not performed in an proper manner, and the data may become
inconsistent.
1. Lost update problem (Write – Write conflict)
This type of problem occurs when two transactions in database access the same data item
and have their operations in an interleaved manner (i.e., concurrent execution) that makes
the value of the items incorrect hence making the database inconsistent.
The dirty read problem occurs when one transaction updates an item of the database, and
somehow the transaction fails, and before the data gets rollback, the updated database item
is accessed by another transaction. There comes the Read-Write Conflict between both
transactions.
Also known as Inconsistent Retrievals Problem that occurs when in a transaction, two
different values are read for the same database item.
Q.12 Testing of Serializability for the following schedule and create Precedence Graph.
Ans:- Explanation:
The precedence graph for schedule S1 contains a cycle that's why Schedule S1 is non-
serializable.
The Database are prone to failures due to inconsistency, network failure, errors or any kind
of accidental damage. So, database recovery techniques are highly important to bring a
database back into a working state after a failure. There are four different recovery
techniques are available in the Database.
1. Mirroring
Mirroring:
Two complete copies of the database maintain on-line on different stable storage devices.
This method mostly uses in environments that require non-stop, fault-tolerant operations.
Immediate Backup
Archival Backup
Immediate Backup are kept in a floppy disk, hard disk or magnetic tapes. These come in handy
when a technical fault occurs in the primary database such as system failure, disk crashes,
network failure. Damage due to virus attacks repair using the immediate backup.
Archival Backup:
Archival Backups are kept in mass storage devices such as magnetic tape, CD-ROMs, Internet
Servers etc. They are very useful for recovering data after a disaster such as fire, earthquake,
flood etc. Archival Backup should be kept at a different site other than where the system is
functioning. Archival Backup at a separate place remains safe from thefts and international
destruction by user staff.
1. Shadow Paging
2. Log-Based Recovery
1. Shadow Paging- The idea is to maintain two-page tables during the life of a
transaction: the current page table and the shadow page table. When the transaction
starts, both tables are identical. The shadow page is never changed during the life of
the transaction. The current page is updated with each write operation. Each table
entry points to a page on the disk. When the transaction is committed, the shadow
page entry becomes a copy of the current page table entry and the disk block with the
old data is released.
If the shadow is stored in non-volatile memory and a system crash occurs, then the
shadow page table is copied to the current page table. This guarantees that the
shadow page table will point to the database pages corresponding to the state of the
database prior to any transaction that was active at the time of the crash, making
aborts automatic.
Q.14 Explain Time Stamp Protocol and also explain their advantage and disadvantage .
The priority of the older transaction is higher that's why it executes first. To determine the
timestamp of the transaction, this protocol uses system time or logical counter.
The lock-based protocol is used to manage the order between conflicting pairs among
transactions at the execution time. But Timestamp based protocols start working as soon as
a transaction is created.
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.
Advantages:
Disadvantages:
Q,15 What is Validation Based Protocol and also explain Characteristics of Good Concurrency
Protocol.
Validation phase is also known as optimistic concurrency control technique. In the validation-
based protocol, the transaction is executed in the following three phases:
Read phase: In this phase, the transaction T is read and executed. It is used to read the value
of various data items and stores them in temporary local variables. It can perform all the write
operations on temporary variables without an update to the actual database.
Validation phase: In this phase, the temporary variable value will be validated against the
actual data to see if it violates the serializability.
Write phase: If the validation of the transaction is validated, then the temporary results are
written to the database or system otherwise the transaction is rolled back.
Part -C
Ans: -Trigger is like a procedure that is automatically invoked by the DBMS in response to
specified changes to data base. Trigger is like a ‘Daemon that monitors a data base, and is
executed when the data base is modified in a way that matches the event specification. A data
base that has a set of associated triggers is called an active data base.
Syntax
(before | after)
on [table_name]
[trigger_body]
Parts of a Trigger
Triggering Event or Statement: - The statements due to which a trigger occurs is called
triggering event or statement. Such statements can be DDL statements, DML statements or any
database operation, executing which gives rise to a trigger.
Trigger Restriction: - The condition or any limitation applied on the trigger is called trigger
restriction. Thus, if such a condition is TRUE then trigger occurs otherwise it does not occur.
Trigger Action: The body containing the executable statements that is to be executed when
trigger occurs that is with the execution of Triggering statement and upon evaluation of Trigger
restriction as True is called Trigger Action.
Types of Triggers
1. Statement-level trigger
2. Row-level trigger
3. Before triggers
4. After triggers
5. Combination Trigger
1.Statement-level Trigger
A statement trigger is fired only for once for a DML statement irrespective of the number of
rows affected by the statement. For example, if you execute the following UPDATE command
STUDENTS table, statement trigger for UPDATE is executed only for once.
2.Row-level Trigger
A row trigger is fired once for each row that is affected by DML command. For example, if an
UPDATE command updates 100 rows then row-level trigger is fired 100 times whereas a
statement-level trigger is fired only for once.
3.Before Triggers
While defining a trigger, you can specify whether the trigger is to be fired before the command
(INSERT, DELETE, and UPDATE) is executed or after the command is executed. Before
triggers are commonly used to check the validity of the data before the action is performed.
For instance, you can use before trigger to prevent deletion of row if deletion should not be
allowed in the given case.
4.AFTER Triggers
After triggers are fired after the triggering action is completed. For example, If after trigger is
associated with INSERT command then it is fired after the row is inserted into the table.
5.Combination Trigger: Combination trigger are combination of two trigger type, Before
Statement Trigger: Trigger fire only once for each statement before the triggering DML
statement.
Before Row Trigger: Trigger fire for each and every record before the triggering DML
statement.
After Statement Trigger: Trigger fire only once for each statement after the triggering DML
statement executing. After Row Trigger: Trigger fire for each and every record after the
triggering DML statement executing.
Ans:- Functional Dependency (FD) is a constraint that determines the relation of one attribute
to another attribute in a Database Management System (DBMS). Functional Dependency helps
to maintain the quality of data in the database. It plays a vital role to find the difference between
good and bad database design.
Example:
In this example, if we know the value of Employee number, we can obtain Employee Name,
city, salary, etc. By this, we can say that the city, Employee Name, and salary are functionally
depended on Employee number.
Below are the Three most important rules for Functional Dependency in Database:
Augmentation rule: When x -> y holds, and c is attribute set, then ac -> bc also holds. That is
adding attributes which do not change the basic dependencies.
Multivalued dependency occurs in the situation where there are multiple independent
multivalued attributes in a single table. A multivalued dependency is a complete constraint
between two sets of attributes in a relation. It requires that certain tuples be present in a
relation.
In Multivalued functional dependency, entities of the dependent set are not dependent on
each other.
i.e. If a → {b, c} and there exists no functional dependency between b and c, then it is called
a multivalued functional dependency.
For example,
42 abc 17
43 pqr 18
44 xyz 18
For example,
Enrol_ building
no name dept no
42 abc CO 4
43 pqr EC 2
44 xyz IT 1
45 abc EC 2
Ans: In SQL there may be some records in a table that do not have values or data for every
field. This could be possible because at a time of data entry information is not available. So
SQL supports a special value known as NULL which is used to represent the values of attributes
that may be unknown or not apply to a tuple. SQL places a NULL value in the field in the
absence of a user-defined value.
For example, the Apartment_number attribute of an address applies only to address that are in
apartment buildings and not to other types of residences.
A NULL value is used to represent a missing value, but that it usually has one of three
different interpretations:
It is often not possible to determine which of the meanings is intended. Hence, SQL
does not distinguish between the different meanings of NULL.
TYAGI SEEMA
SINGH RAMAN 5
SHARMA AMAR
JAISWAL VICKY 6
SELECT SURNAME, NAME, MARKS FROM STUDENTS
SHARMA AMAR
TYAGI SEEMA
How to select records with no null values (in marks column)? Let's see the query to get all the
records where marks is NOT NULL
SINGH RAMAN 5
JAISWAL VICKY 6
When a relation in the relational model is not in appropriate normal form then the
decomposition of a relation is required.
In a database, it breaks the table into multiple tables. If the relation has no proper
decomposition, then it may lead to problems like loss of information. Decomposition is used
to eliminate some of the problems of bad design like anomalies, inconsistencies, and
redundancy.
Types of Decomposition
Lossless Decomposition
If the information is not lost from the relation that is decomposed, then the
decomposition will be lossless.
The lossless decomposition guarantees that the join of relations will result in the same
relation as it was decomposed.
EMPLOYEE_DEPARTMENT table:
The above relation is decomposed into two relations EMPLOYEE and DEPARTMENT
EMPLOYEE table:
DEPARTMENT table
Now, when these two relations are joined on the common column "EMP_ID", then the
resultant relation will look like:
Employee ⋈ Department
Dependency Preserving
In the dependency preservation, at least one decomposed table must satisfy every
dependency.
For example, suppose there is a relation R (A, B, C, D) with functional dependency set
(A->BC). The relational R is decomposed into R1(ABC) and R2(AD) which is dependency
preserving because FD A->BC is a part of relation R1(ABC).
A B C
1 2 1
2 5 3
3 3 3
R (A, B, C)
Consider this relation is decomposed into two sub relations as R1(A, C) and R2(B,
C )-
1 1 2 1
2 3 5 3
3 3 3 3
A B C
1 2 1
2 5 3
2 3 3
3 5 3
3 3 3
Q.6 Explain the fourth and fifth Normal Form in DBMS with example.
• A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.
• For a dependency A → B, if for a single value of A, multiple values of B exist, then the
relation will be a multi-valued dependency.
Multivalued Dependency
Multivalued dependency occurs when two attributes in a table are independent of each other
but, both depend on a third attribute.
A multivalued dependency consists of at least two attributes that are dependent on a third
attribute that's why it always requires at least three attributes.
Example: Suppose there is a bike manufacturer company which produces two colors (white
and black) of each model every year.
• BIKE_MODEL → → MANUF_YEAR
• BIKE_MODEL → → COLOR
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
• The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent
entity. Hence, there is no relationship between COURSE and HOBBY.
So to make the above table into 4NF, we can decompose it into two tables:
STUDENT_COURSE
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
Join Dependency
If the join of R1 and R2 over C is equal to relation R then we can say that a join dependency
(JD) exists, where R1 and R2 are the decomposition R1(A, B, C) and R2(C, D) of a given relations
R (A, B, C, D).
C1 mic Aman C2
C2 speaker Mohan C1
C2 speaker
Agent->->Company Company ->->Product
Table R3
AGENT PRODUCT
Aman pendrive
Aman mic
speaker
Aman
Mohan speaker
Agent->->Product
Table – R1⋈R2⋈R3
C1 mic Aman
C2 speaker speaker
C1 speaker Aman
• A relation decomposed into two relations must have loss-less join Property, which
ensures that no spurious or extra tuples are generated, when relations are reunited
through a natural join.
Example – Consider the above schema, with a case as “if a company makes a product
and an agent is an agent for that company, then he always sells that product for the
company”. Under these circumstances, the ACP table is shown as:
Table – ACP
A1 PQR Nut
A1 PQR Bolt
A1 XYZ Nut
A1 XYZ Bolt
A2 PQR Nut
The relation ACP is again decompose into 3 relations. Now, the natural Join of all the
three relations will be shown as:
Table – R1 Table- R2
Table – R3
COMPANY PRODUCT
PQR Nut
PQR Bolt
XYZ Nut
XYZ Bolt
• Result of Natural Join of R1 and R3 over ‘Company’ and then Natural Join of R13 and
R2 over ‘Agent ‘and ‘Product’ will be table ACP.
• Hence, in this example, all the redundancies are eliminated, and the decomposition of
ACP is a lossless join decomposition.
• Therefore, the relation is in 5NF as it does not violate the property of lossless join.
DBMS is the management of data that should remain integrated when any changes are done
in it. It is because if the integrity of the data is affected, whole data will get disturbed and
corrupted.
Therefore, to maintain the integrity of the data, there are four properties described in the
database management system, which are known as the ACID properties. The ACID properties
are meant for the transaction that goes through a different group of tasks.
Atomicity: -
This property states that a transaction must be treated as an atomic unit, that is, either all of
its operations are executed or none. There must be no state in a database where a transaction
is left partially completed. States should be defined either before the execution of the
transaction or after the execution/abortion/failure of the transaction.
Consistency
For example, in an application that transfers funds from one account to another, the
consistency property ensures that the total value of funds in both the accounts is the same at
the start and end of each transaction.
Isolation
In a database system where more than one transaction are being executed simultaneously
and in parallel, the property of isolation states that all the transactions will be carried out and
executed as if it is the only transaction in the system. No transaction will affect the existence
of any other transaction.
Durability
In DBMS, the term durability ensures that the data after the successful execution of the
operation becomes permanent in the database. The durability of the data should be so
perfect that even if the system fails or leads to a crash, the database still survives. However,
if gets lost, it becomes the responsibility of the recovery manager for ensuring the durability
of the database. For committing the values, the COMMIT command must be used every time
we make changes.
Therefore, the ACID property of DBMS plays a vital role in maintaining the consistency and
availability of data in the database.
Operations in Transaction-
Read Operation
Write Operation
1. Read Operation
Read operation reads the data from the database and then stores it in the buffer in main
memory.
For example- Read(A) instruction will read the value of A from the database and will store it
in the buffer in main memory.
2. Write Operation
Write operation writes the updated data value back to the database from the buffer.
For example- Write(A) will write the updated value of A from the buffer to the database.
Q.8 What is a Database Transaction? Why do you need concurrency in Transactions?
Ans :- A database is a shared resource accessed. It is used by many users and processes
concurrently. For example, the banking system, railway, and air reservations systems, stock
market monitoring, supermarket inventory, and checkouts, etc.
A transaction is an action or series of actions that are being performed by a single user or
application program, which reads or updates the contents of the database.
States of Transaction
Active State
Failed State
If a transaction is executing and a failure occurs, either a hardware failure or a software failure
then the transaction goes into failed state from the active state.
a transaction goes into “partially committed” state from the active state when there are read
and write operations present in the transaction.
A transaction contains number of read and write operations. Once the whole transaction is
successfully executed, the transaction goes into partially committed state where we have all
the read and write operations performed on the main memory (local memory) instead of the
actual database.
The reason why we have this state is because a transaction can fail during execution so if we
are making the changes in the actual database instead of local memory, database may be left
in an inconsistent state in case of any failure.
Committed State
If a transaction completes the execution successfully then all the changes made in the local
memory during partially committed state are permanently stored in the database. You can
also see in the above diagram that a transaction goes from partially committed state to
committed state when everything is successful.
Aborted State
if a transaction fails during execution then the transaction goes into a failed state. The
changes made into the local memory (or buffer) are rolled back to the previous consistent
state and the transaction goes into aborted state from the failed state. Refer the diagram to
see the interaction between failed and aborted state.
Q.9 Explain Schedule in DBMS and explain Recoverable & Non Recoverable Schedule in
DBMS Transaction.
Ans :- Schedule
1. Serial Schedule
The serial schedule is a type of schedule where one transaction is executed completely before
starting another transaction. In the serial schedule, when the first transaction completes its
cycle, then the next transaction is executed.
2. Non-serial Schedule
It contains many possible orders in which the system can execute the individual operations of
the transactions.
The serializability of schedules is used to find non-serial schedules that allow the transaction
to execute concurrently without interfering with one another. It identifies which schedules
are correct when executions of the transaction have interleaving of their operations.
A non-serial schedule will be serializable if its result is equal to the result of its transactions
executed serially.
Schedule A and Schedule B are serial schedule.
1. Conflict Serializability
It deals with detecting the instructions that are conflicting in any way and specifying
the order in which the instructions should execute in case there is any conflict.
2. View Serializability
It can be derived by creating another schedule out of an existing schedule and involves
the same set of transactions
Recoverable Schedule
If in a schedule,
And its commit operation is delayed till the uncommitted transaction either commits
or roll backs
Example
Let us consider two transaction schedules as given below −
T1 T2
Read(A)
Write(A)
- Write(A)
Commit
Commit // delayed
The above schedule is a recoverable schedule because of the reasons mentioned below-
In the above schedule transaction T2 is now allowed to commit whereas T1 is not yet
committed.
In this case transaction T1 is failed, and transaction T2 still has a chance to recover by
rollback.
Irrecoverable Schedules-
If in a schedule,
And commits before the transaction from which it has read the value
then such a schedule is known as an Irrecoverable Schedule.
Q.10 What is Deadlock in DBMS? explain classical approaches for deadlock handling
A deadlock is a condition where two or more transactions are waiting indefinitely for one
another to give up locks. Deadlock is said to be one of the most troubled complications in
DBMS as no task ever gets finished and is in waiting state forever.
For example: In the student table, transaction T1 holds a lock on some rows and needs to update
some rows in the grade table. Simultaneously, transaction T2 holds locks on some rows in the
grade table and needs to update the rows in the Student table held by Transaction T1.
Now, the main problem arises. Now Transaction T1 is waiting for T2 to release its lock and
similarly, transaction T2 is waiting for T1 to release its lock. All activities come to a halt state
and remain at a standstill. It will remain in a standstill until the DBMS detects the deadlock
and aborts one of the transactions.
Deadlock Handling in Centralized Systems
Deadlock prevention.
Deadlock avoidance.
Deadlock Prevention
The deadlock prevention approach does not allow any transaction to acquire locks that will
lead to deadlocks. The convention is that when more than one transaction request for locking
the same data item, only one of them is granted the lock.
Deadlock Avoidance
The deadlock avoidance approach handles deadlocks before they occur. It analyses the
transactions and the locks to determine whether or not waiting leads to a deadlock.
The deadlock detection and removal approach run a deadlock detection algorithm periodically
and removes deadlock in case there is one. It does not check for deadlock when a transaction
places a request for a lock. When a transaction requests a lock, the lock manager checks
whether it is available. If it is available, the transaction is allowed to lock the data item;
otherwise the transaction is allowed to wait.
During recovery, it would be very difficult for the recovery system to backtrack all the logs
and then start recovering.
Recovery with concurrent transactions can be done in the following four ways.
Transaction rollback
Checkpoints
Restart recovery
In this scheme, the recovery scheme depends greatly on the concurrency control scheme that
is used. So, to rollback a failed transaction, we must undo the updates performed by the
transaction.
Transaction rollback:
The system scans the log backward a failed transaction, for every log record found in the log
the system restores the data item.
Checkpoints:
Restart recovery:
Ans: SQL supports few Set operations which can be performed on the table data. These are
used to get meaningful results from data stored in the table, under different special conditions.
In this tutorial, we will cover 4 different types of SET operations, along with example:
1. UNION
2. UNION ALL
3. INTERSECT
4. Minus
UNION Operation
UNION is used to combine the results of two or more SELECT statements. However, it will
eliminate duplicate rows from its result set. In case of union, number of columns and data type
must be same in both the tables, on which UNION operation is being applied.
Example of UNION
ID Name
1 abhi
ID Name
2 adam
2 adam 3 Chester
ID NAME
1 abhi
2 adam
3 Chester
2. UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.
ID Name
1 abhi
ID Name
2 adam
2 adam 3 Chester
ID NAME
1 abhi
2 adam
2 adam
3 Chester
3. INTERSECT
Intersect operation is used to combine two SELECT statements, but it only retunes the records
which are common from both SELECT statements. In case of Intersect the number of columns
and data type must be same.
ID Name
ID Name
1 abhi 2 adam
2 adam 3 Chester
ID NAME
2 adam
4. Minus
It combines the result of two SELECT statements. Minus operator is used to display the rows
which are present in the first query but absent in the second query.
Syntax:
MINUS
MINUS
ID NAME
1 Jack
2 Harry
Q.13 What are Unary and Binary Relationships explain with example?
Ans:-A relationship in a DBMS, is primarily the way two or more data sets are linked. This is
so true for Relational Database Management Systems. One dataset may be then termed as the
Foreign key and the ones linked to it may be termed as the Primary Key. There may be multiple
Foreign and Primary keys linked to each other.
Relationships allow the datasets to share and store the data in separate tables. They also help
link disparate data with each other.
For example, a person has only one passport and only one passport is given to only one person
and hence unary relationship is observed
A Binary Relationship is the relationship between two different Entities i.e. it is a relationship
of role group of one entity with the role group of another entity.
For example, a teacher teaches a subject here 2 entities are teacher and subject for the
relationship teacher teaches subject
Aggregation in DBMS
Aggregation refers to the process by which entities are combined to form a single meaningful
entity. The specific entities are combined because they do not make sense on their own. To
establish a single entity, aggregation creates a relationship that combines these entities. The
resulting entity makes sense because it enables the system to function well.
When using data in the form of numerical values, the following operations can be used to
perform DBMS aggregation:
Average (AVG): This function provides the mean or average of the data values.
Sum: This provides a total value after the data values have been added.
Maximum (Max): This function provides the maximum value of a given set of data.
Minimum (Min): This provides the minimum value of a given set of data.
SQL aggregation function is used to perform the calculations on multiple rows of a single
column of a table. It returns a single value.
It is also used to summarize the data.
1. COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
PRODUCT_MAST Table: -
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Output: - 05
2. SUM Function: - Suppose we want a report that gives total amount of payments made so
far. We can use the SQL SUM function which returns the sum of all the values in the specified
column. SUM works on numeric fields only. Null values are excluded from the result returned.
Output: - 245
3.AVG function: -SQL AVG function returns the average of the values in a specified column.
Just like the SUM function, it works only on numeric data types. AVG function returns the
average of all non-Null values.
Output: - 49.00
4.MAX Function: - Just as the name suggests, the MAX function is the opposite of the MIN
function. It returns the largest value from the specified table field. Let's assume we want to get
the year that the latest movie in our database was released. We can easily use the MAX function
to achieve that.
SELECT MAX(RATE) FROM PRODUCT_MAST;
Output: - 30
5.MIN Function: - The MIN function returns the smallest value in the specified table field. As
an example, let's suppose we want to know the year in which the oldest movie in our library
was released, we can use SQL's MIN function to get the desired information.
Q.15 Explain the BCNF and Third Normal Form with example.
{Book} ->{Author} (if we know the book, we know the author name)
A relation will be in 3NF if it is in 2NF and not contain any transitive partial dependency.
3NF is used to reduce the data duplication. It is also used to achieve the data integrity.
If there is no transitive dependency for non-prime attributes, then the relation must be in
third normal form.
A relation is in third normal form if it holds at least one of the following conditions for every
non-trivial function dependency X → Y.
X is a super key.
Super Key: The set of attributes which can uniquely identify a tuple is known as Super Key.
For Example, STUD_NO, (STUD_NO, STUD_NAME) etc.
Candidate Key: The minimal set of attributes which can uniquely identify a tuple is known
as candidate key.
• The value of Candidate Key is unique and non-null for every tuple.
Example: - EMPLOYEE_DETAIL
Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new <EMPLOYEE_ZIP>
table, with EMP_ZIP as a Primary key.
EMPLOYEE Table: -
Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new <EMPLOYEE_ZIP>
table, with EMP_ZIP as a Primary key.
EMPLOYEE Table: -
EMPLOYEE_ZIP Table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
A table complies with BCNF if it is in 3NF and for every functional dependency X->Y, X
should be the super key of the table.
Functional Dependency
Functional Dependency (FD) determines the relation of one attribute to another attribute
in a database management system (DBMS) system.
Functional dependency helps you to maintain the quality of data in the database.
A functional dependency is denoted by an arrow →. The functional dependency of X on Y
is represented by X → Y.
Example: Suppose there is a company wherein employees work in more than one
department. They store the data like this:
The table is not in BCNF as neither emp_id nor emp_dept alone are keys.
To make the table comply with BCNF we can break the table in three tables like this:
emp_nationality:
emp_id emp_nationality
1001 Austrian
1002 American
emp_dept table:
emp_dept_mapping table:
emp_id emp_dept
1001 Stores
Functional dependencies:
emp_id -> emp_nationality
Candidate keys:
For first table: emp_id
This is now in BCNF as in both the functional dependencies left side part is a key.