0% found this document useful (0 votes)
17 views19 pages

Mid 2 (DBMS)

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

Mid 2 (DBMS)

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

IMPORTANT QUESTIONS DBMS - MID-2

UNIT-III
1. Explain relational Algebra with join conditions and examples.
Join operation combines the relation R1 and R2 with respect to a condition. It is denoted by ⋈.
The different types of join operation are as follows −
 Theta join
 Natural join
 Outer join − It is further classified into following types −
o Left outer join.
o Right outer join.
o Full outer join.
Theta join
If we join R1 and R2 other than the equal to condition then it is called theta join/ non-equi join.
Example

Consider R1 table
RegNo Branch Section

1 CSE A

2 ECE B

3 CIVIL A

4 IT B

5 IT A

Table R2
Name RegNo

Bhanu 2

Priya 4
R1 ⋈ R2 with condition R1.regno > R2.regno

RegNo Branch Section Name Regno

3 CIVIL A Bhanu 2

4 IT B Bhanu 2

5 IT A Bhanu 2

5 IT B Priya 4
In the join operation, we select those rows from the cartesian product where R1.regno>R2.regno.
Join operation = select operation + cartesian product operation
Natural join
If we join R1 and R2 on equal condition then it is called natural join or equi join. Generally, join is
referred to as natural join.
Natural join of R1 and R2 is −
{ we select those tuples from cartesian product where R1.regno=R2.regno}
R1 ⋈ R2
Regno Branch Section Name

2 - - Bhanu

4 - - priya

Outer join
It is an extension of natural join to deal with missing values of relation.
Consider R1 and R2 shown below –
Table R1
RegNo Branch Section

1 CSE A

2 ECE B

3 CIVIL A

4 IT B

5 IT A

Table R2
Name Regno

Bhanu 2

Priya 4

Hari 7
Outer join is of three types. These are explained below −
Left outer join
It is denoted by R1 ⋈ R2.

RegNo Branch Section Name Regno

2 - - Bhanu 2

4 - - Priya 4

1 - - NULL NULL

3 - - NULL NULL

5 - - NULL NULL
Here all the tuples of R1(left table) appear in output.
The mismatching values of R2 are filled with NULL.
Left outer join = natural join + mismatch / extra tuple of R1
Right outer join
It is denoted by R1 ⋈ R2
Here all the tuples of R2(right table) appear in output. The mismatching values of R1 are filled with
NULL.

RegNo Branch Section Name Regno

2 - - Bhanu 2

4 - - Priya 4

NULL NULL NULL Hari 7


Right outer join = natural join+ mismatch/extra tuple of R2.
Full outer join
It is denoted by R1 ⋈ R2.
Full outer join=left outer join U right outer join.

RegNo Branch Section Name Regno

2 - - Bhanu 2

4 - - Priya 4

1 - - NULL NULL

3 - - NULL NULL

5 - - NULL NULL

NULL NULL NULL Hari 7


Example
Given below is the picture of full outer join −
2. Explain about nested queries with examples.
A nested query is a query that has another query embedded within it. The embedded query
is called a subquery.
A subquery typically appears within the WHERE clause of a query. It can sometimes appear
in the FROM clause or HAVING clause.
Nested queries are a way to perform more complex queries by embedding one query
within another. A nested query is a query that appears inside another query, and it helps retrieve
data from multiple tables or apply conditions based on the results of another query. The result of
inner query is used in execution of outer query.
There are mainly two types of nested queries:
 Independent Nested Queries
 Co-related Nested Queries

Example
Create table class(id number(10), grade number(10), teacherID number(10),
noofstudents number(10));
insert into class values(1,8,2,20);
insert into class values(2,9,3,40);
insert into class values(3,10,1,38);
select * from class;

Example 1
Select AVG(noofstudents) from class where teacherID IN(
Select id from teacher
Where subject=’science’ OR subject=’maths’);

Output
You will get the following output −
20.0

3. Explain UNION, INTERSECT, EXCEPT with examples.


Using set operations, the results of multiple queries can be combined into a single result
set. Set operators include UNION, EXCEPT, and INTERSECT in MS SQL.
To perform such kind of various set operations on the multiple data set;
SQL mainly provides the following three operators:
 UNION, UNION ALL
 INTERSECT
 EXCEPT
UNION:
Union is used to combine the results of two queries into a single result set of all matching rows.
Both the queries must result in the same number of columns and compatible data types in order to
unite. All duplicate records are removed automatically unless UNION ALL is used.

UNION ALL:
It will not remove duplicate records. It can be faster than UNION.

INTERSECT:
It is used to take the result of two queries and returns only those rows which are common in both
result sets. It removes duplicate records from the final result set.

EXCEPT:
It is used to take the distinct records of two one query and returns only those rows which do not
appear in the second result set.

The key important things about UNION, UNION ALL, INTERSECT, EXCEPT in MS SQL.
UNION: combines results from both tables.
UNION ALL: combines two or more result sets into a single set, including all duplicate rows.
INTERSECT: takes the rows from both the result sets which are common in both.
EXCEPT: takes the rows from the first result data but does not in the second result set.

Use below SQL Script to create and populate the two tables that we are going to use in our examples.
CREATE TABLE TableA
(
ID INT,
Name VARCHAR(50),
Gender VARCHAR(10),
Department VARCHAR(50)
)
GO
INSERT INTO TableA VALUES(1, 'Pranaya', 'Male','IT')
INSERT INTO TableA VALUES(2, 'Priyanka', 'Female','IT')
INSERT INTO TableA VALUES(3, 'Preety', 'Female','HR')
INSERT INTO TableA VALUES(3, 'Preety', 'Female','HR')
GO
Fetch the records:
SELECT * FROM TableA

CREATE TABLE TableB


(
ID INT,
Name VARCHAR(50),
Gender VARCHAR(10),
Department VARCHAR(50)
)
GO
INSERT INTO TableB VALUES(2, 'Priyanka', 'Female','IT')
INSERT INTO TableB VALUES(3, 'Preety', 'Female','HR')
INSERT INTO TableB VALUES(4, 'Anurag', 'Male','IT')
GO
Fetch the records:
SELECT * FROM TableB
UNION Operator:
The Union operator will return all the unique rows from both the queries. Notice that the duplicates are
removed from the result set.
SELECT ID, Name, Gender, Department FROM TableA
UNION
SELECT ID, Name, Gender, Department FROM TableB

UNION ALL Operator:


The UNION ALL operator returns all the rows from both the queries, including the duplicates.
SELECT ID, Name, Gender, Department FROM TableA
UNION ALL
SELECT ID, Name, Gender, Department FROM TableB
Result:

INTERSECT Operator:
The INTERSECT operator retrieves the common unique rows from both the left and the right query.
Notice the duplicates are removed.
SELECT ID, Name, Gender, Department FROM TableA
INTERSECT
SELECT ID, Name, Gender, Department FROM TableB

EXCEPT Operator:
The EXCEPT operator will return unique rows from the left query that aren’t present in the right query’s
results.
SELECT ID, Name, Gender, Department FROM TableA
EXCEPT
SELECT ID, Name, Gender, Department FROM TableB

If you want the rows that are present in Table B but not in Table A, reverse the queries.
SELECT ID, Name, Gender, Department FROM TableB
EXCEPT
SELECT ID, Name, Gender, Department FROM TableA

4. What is a trigger ? explain with an example.


A Trigger in Structured Query Language is a set of procedural statements which are executed
automatically when there is any response to certain events on the particular table in the database.
Triggers are used to protect the data integrity in the database.

In SQL, this concept is the same as the trigger in real life. For example, when we pull the gun trigger, the
bullet is fired.

Example of Trigger in SQL

To understand the concept of trigger in SQL, first, we have to create the


table on which trigger is to be executed.

The following query creates the Student_Trigger table in the SQL database:

1. CREATE TABLE Student_Trigger


2. (
3. Student_RollNo INT NOT NULL PRIMARY KEY,
4. Student_FirstName Varchar (100),
5. Student_EnglishMarks INT,
6. Student_PhysicsMarks INT,
7. Student_ChemistryMarks INT,
8. Student_MathsMarks INT,
9. Student_TotalMarks INT,
10. Student_Percentage );

The following query shows the structure of theStudent_Trigger table:

1. DESC Student_Trigger;

Output:

Field Type NULL Key Default Extra

Student_RollNo INT NO PRI NULL

Student_FirstName Varchar(100) YES NULL

Student_EnglishMark INT YES NULL


s

Student_PhysicsMark INT YES NULL


s

Student_ChemistryM INT YES NULL


arks

Student_MathsMarks INT YES NULL

Student_TotalMarks INT YES NULL

Student_Percentage INT YES NULL

The following query fires a trigger before the insertion of the student record
in the table:

1. CREATE TRIGGER Student_Table_Marks


2. BEFORE INSERT
3. ON
4. Student_Trigger
5. FOR EACH ROW
6. SET new.Student_TotalMarks = new.Student_EnglishMarks + new.Student_P
hysicsMarks + new.Student_ChemistryMarks + new.Student_MathsMarks,
7. new.Student_Percentage = ( new.Student_TotalMarks / 400) * 100;

The following query inserts the record into Student_Trigger table:


1. INSERT INTO Student_Trigger (Student_RollNo, Student_FirstName, Student
_EnglishMarks, Student_PhysicsMarks, Student_ChemistryMarks, Student_Mat
hsMarks, Student_TotalMarks, Student_Percentage) VALUES ( 201, Sorya, 88
, 75, 69, 92, 0, 0);

To check the output of the above INSERT statement, you have to type the
following SELECT statement:

1. SELECT * FROM Student_Trigger;

Output:

5. Define a. Aggregate operators b. Integrity Constraints in SQL


a. Aggregate operators
The aggregation operators perform mathematical operations like Average, Aggregate,
Count, Max, Min and Sum, on the numeric property of the elements in the collection.
Aggregation operators compute a value from a collection of values.

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

UNIT-IV
1. What is Normalization ? Explain?

Normalization :
A large database defined as a single relation may result in data duplication. This repetition of
data may result in:

o Making relations very large.


o It isn't easy to maintain and update data as it would involve searching many records in relation.
o Wastage and poor utilization of disk space and resources.
o The likelihood of errors and inconsistencies increases.

So to handle these problems, we should analyze and decompose the relations with redundant
data into smaller, simpler, and well-structured relations that are satisfy desirable properties.
Normalization is a process of decomposing the relations into relations with fewer attributes.

Explaination:
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of relations. It is also
used to eliminate undesirable characteristics like Insertion, Update, and Deletion Anomalies.
o Normalization divides the larger table into smaller and links them using relationships.
o The normal form is used to reduce redundancy from the database table.

The main reason for normalizing the relations is removing these anomalies. Failure to eliminate
anomalies leads to data redundancy and can cause data integrity and other problems as the
database grows. Normalization consists of a series of guidelines that helps to guide you in
creating a good database structure.

2. Explain 1NF, 2NF, 3NF?


1NF, 2NF, and 3NF are the first three types of database normalization. They stand for first normal
form, second normal form, and third normal form, respectively.

The First Normal Form – 1NF


For a table to be in the first normal form, it must meet the following criteria:
 a single cell must not hold more than one value (atomicity)
 there must be a primary key for identification
 no duplicated rows or columns
 each column must have only one value for each row in the table

The Second Normal Form – 2NF


The 1NF only eliminates repeating groups, not redundancy. That’s why there is 2NF.

A table is said to be in 2NF if it meets the following criteria:


 it’s already in 1NF
 it has no partial dependency. That is, all non-key attributes are fully dependent on a
primary key.

The Third Normal Form – 3NF


When a table is in 2NF, it eliminates repeating groups and redundancy, but it does not eliminate
transitive partial dependency.
This means a non-prime attribute (an attribute that is not part of the candidate’s key) is dependent
on another non-prime attribute. This is what the third normal form (3NF) eliminates.
So, for a table to be in 3NF, it must:
 be in 2NF
 have no transitive partial dependency.

3. Define a. Functional Dependencies b. Multivalued dependency

a. Functional Dependencies
Functional dependency mathematically demonstrates the relation
between two attributes in a database management system (DBMS). It typically
exists with a primary key attribute and a non-key attribute within a table or data
set. Functional dependency acts as a constraint between the two sets of
attributes and is an essential factor in designing database parameters and
functions to help your business, organization or company store and manage its
data.
Often, an arrow denotes functional dependency. For example, if you mark one
attribute X and another Y and the functional dependency of X relies on Y, the
simple formula is:

X→Y
The attribute set on the left, X, is called the determinant, while the Y on the right
is called the dependent.

b. Multivalued dependency
Multivalued dependency (MVD) is having the presence of one or more rows in a table. It
implies the presence of one or more other rows in that same table. A multivalued dependency
prevents fourth normal form. A multivalued dependency involves at least three attributes of a
table.
It is represented with a symbol "->->" in DBMS.
X->Y relates one value of X to one value of Y.
X->->Y (read as X multidetermines Y) relates one value of X to many values of Y.
A Nontrivial MVD occurs when X->->Y and X->->z where Y and Z are not dependent are
independent to each other. Non-trivial MVD produces redundancy.

4. What is Boyce-Codd NF?


BCNF (Boyce Codd Normal Form) is an advanced version of the third normal form (3NF),
and often, it is also known as the 3.5 Normal Form. 3NF doesn't remove 100% redundancy in the
cases where for a functional dependency (say, A->B), A is not the candidate key of the table. To
deal with such situations, BCNF was introduced.
BCNF is based on functional dependencies, and all the candidate keys of the relation are taken into
consideration. BCNF is stricter than 3NF and has some additional constraints along with the general
definition of 3NF.
A table or relation is said to be in BCNF in DBMS if the table or the relation is already in 3NF, and
also, for every functional dependency (let's say, X->Y), X is either the super key or the candidate
key. In simple terms, for any case (let's say, X->Y), X can't be a non-prime attribute.

BCNF rules

To check if the table satisfies the conditions of BCNF, the following two conditions should exist:

 The table must be in 3NF form.


 For any dependency X → Y, X must be a candidate key or super key. In other words, for
dependency X → Y, if Y is a prime attribute, X cannot be a non-prime attribute.

3NF states that the transitive dependency must not exist. Transitive dependency is that the LHS (left-
hand side) of the functional dependency must consist of a super key/candidate key or the RHS (right-
hand side) must have a prime attribute. BCNF adds more restrictions by stating that LHS of functional
dependency must have a super key and removes the RHS condition.

5. Explain about Multivalued Dependency with 4NF and 5NF

Fourth Normal Form (4NF)


The Fourth Normal Form (4NF) is a level of database normalization where there are no non-trivial
multivalued dependencies other than a candidate key. It builds on the first three normal forms
(1NF, 2NF, and 3NF) and the Boyce-Codd Normal Form (BCNF). It states that, in addition to a
database meeting the requirements of BCNF, it must not contain more than one multivalued
dependency.
Properties
A relation R is in 4NF if and only if the following conditions are satisfied:
1. It should be in the Boyce-Codd Normal Form (BCNF).
2. The table should not have any Multi-valued Dependency.
A table with a multivalued dependency violates the normalization standard of the Fourth Normal
Form (4NF) because it creates unnecessary redundancies and can contribute to inconsistent data.
To bring this up to 4NF, it is necessary to break this information into two tables.

Fifth Normal Form / Projected Normal Form (5NF)


A relation R is in Fifth Normal Form if and only if everyone joins dependency in R is implied by the
candidate keys of R. A relation decomposed into two relations must have lossless join Property,
which ensures that no spurious or extra tuples are generated when relations are reunited through a
natural join.
Properties
A relation R is in 5NF if and only if it satisfies the following conditions:
1. R should be already in 4NF.
2. It cannot be further non loss decomposed (join dependency).

UNIT-V
1. What is transcation Management? Explain?
Transaction Management in DBMS
Transaction in DBMS refers to operations like insertion, updation, and deletion of data. This set
of logical works requires one or more database operations. A transaction means that there is a
change in the database.
Transaction States in DBMS
Transaction states in DBMS are the transaction stages from the beginning of the transaction to
its completion or rollback. The transaction states are the multiple phases of the transaction
during its lifetime. These states outline the transaction’s current situation and explain how it is
handled.

There are a total of 6 transaction states in DBMS. The transaction states are active, partially
committed state, committed, failed, aborted, and terminated state.
Active state: A transaction is called an active transaction when the operations are benign and
made in that particular transaction.
Partially Committed and Committed state: In this state, the operations made in the main
memory will be made permanent and then passed on to the committed state. And if, in the
process, some failure is experienced, it goes into a failed state.
Aborted State: In case of transaction failure, it goes from failed state to aborted state. If we
apply the rollback operation, the transaction goes to the state as it was in its beginning before
applying the operations. Also, locks are released if the tractions use a lock on the data, and now
other transactions can access the data.

2. Explain about a simple transcation Model?


Simple transaction model is a model of transaction how it must be. It has active, partially committed,
failed, aborted, and committed states. Transaction is a several operations that can change the
content of the database which is handled by a single program. Simple transaction model follows all
ACID properties while doing transactions.

Transaction States:

 Active
 Partially committed
 Failed
 Aborted
 Committed

The above figure describes in this way:

At first, when the transaction is going to operate it is the active state. When the read or write
operations occurs, it can be called partially committed states. Finally, after read or write operations,
when they use commit operations, they will be committed states meaning the transaction is stored
permanently in the database. And when after these both of active states and partially committed
states fails, they will fall under the category failed state. Without executing, failed state will rollback
which will create the aborted state. Once again, aborted state will be automatically converges into
terminated state. Also, after the committed state, the transaction terminates.

In the process of series of operations, read and write operations creates partially committed. That
will be stored in local memory or buffer. After, the use of commit statement, data will be moved into
permanent storage. This will justify the flow from active to partially committed and to committed
state.

On the other hand, in the case of power failure, it will be in failed state. Also, in the partially
committed state, in the power failure case, it will be in failed sate. After this, rollback occurs meaning
the local memory is cleared. Then aborted occurs meaning the database is unchanged and finally
terminated.
3. What is meant by ACID properties?
ACID properties are a set of fundamental principles in the context of database management
systems (DBMS) that ensure data integrity, consistency, and reliability in transaction processing.
The term "ACID" stands for Atomicity, Consistency, Isolation, and Durability. Let's look at each of
these properties in detail:

Atomicity:
Atomicity ensures that a transaction is treated as a single, indivisible unit of work. It means
that either all the operations within a transaction are executed successfully, or none of them are
executed at all. If any part of a transaction fails, the entire transaction is rolled back, and the
database is left unchanged.

Consistency:
Consistency guarantees that a transaction takes the database from one valid state to
another. It enforces all the rules and constraints defined in the database schema. In simpler terms,
the database remains in a consistent state before and after a transaction is executed.

Isolation:
Isolation ensures that the concurrent execution of multiple transactions does not lead to
data inconsistencies. Each transaction is isolated from other transactions until it is completed and
committed. This prevents interference or conflicts between transactions.

Durability:
Durability ensures that once a transaction is successfully committed, its changes to the
database are permanent and survive any subsequent system failures (e.g., power outage, crash).
The changes are stored safely in non-volatile storage, such as disk, so that they can be recovered
and restored in case of a system failure.

By adhering to the ACID properties, a DBMS ensures that data integrity and reliability are
maintained, and transactions are executed in a robust and consistent manner, even in a multi-user
and concurrent environment. However, it's essential to note that adhering to all ACID properties
might incur some performance overhead, especially in high-concurrency scenarios. To address this,
some systems may adopt weaker consistency models like BASE (Basically Available, Soft state,
Eventually consistent) for certain use cases.

4. Define a. DBMS storage b. Deadlock c. Serializability

a.DBMS storage
Storage System in DBMS
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.

In this section, we will take an overview of various types of storage devices that are used for accessing
and storing data.
Types of Data Storage

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:

o Primary Storage
o Secondary Storage
o Tertiary Storage

b. Deadlock
In a database management system (DBMS), a deadlock occurs when two or more transactions
are waiting for each other to release resources, such as locks on database objects, that they need
to complete their operations. As a result, none of the transactions can proceed, leading to a
situation where they are stuck or “deadlocked.”

c. Serializability
In computer science, serializability is a property of a system describing how different processes
operate on shared data. A system is serializable if its result is the same as if the operations were
executed in some sequential order, meaning there is no overlap in execution. A database
management system (DBMS) can be accomplished by locking data so that no other process can
access it while it is being read or written.

5. What is lock-based protocols and transaction Isolation Levels?


Lock-based protocols in DBMS (Database Management Systems) are concurrency control
mechanisms that use locks to coordinate access to shared resources (such as database tables,
records, or data items) when multiple transactions are executing concurrently. The purpose of lock-
based protocols is to prevent data inconsistencies and conflicts that can arise when multiple
transactions try to access or modify the same data simultaneously.

Lock-based protocols typically use two types of locks:

Shared Lock (Read Lock): Multiple transactions can acquire shared locks on the same data item
simultaneously. Shared locks allow read-only operations on the data and do not conflict with other
shared locks. Transactions holding shared locks can read the data but cannot modify it until all
shared locks are released.

Exclusive Lock (Write Lock): Only one transaction can acquire an exclusive lock on a data item at
a time. Exclusive locks prevent other transactions from acquiring shared or exclusive locks on the
same data item. Transactions holding exclusive locks have the exclusive right to modify the data.

There are several lock-based protocols that control the way locks are acquired and released to
ensure data consistency. Some common lock-based protocols include:
Two-Phase Locking (2PL): Transactions follow two phases: an acquisition phase, where they
acquire all the necessary locks, and a release phase, where they release all the locks. 2PL
guarantees serializability and avoids issues like dirty reads, non-repeatable reads, and lost updates.

Strict Two-Phase Locking (S2PL): A variant of 2PL where transactions hold all their locks until the
end of the transaction. This ensures strict serializability but may lead to more lock contention and
reduced concurrency.

Rigorous Two-Phase Locking (Strict 2PL with Conservative 2PL): Similar to S2PL, but the locks
are acquired before any data access in the transaction. This ensures rigorous serializability but may
result in more restrictive locking.

Shared/Exclusive Locking (Readers-Writers Locking): Allows multiple transactions to acquire


shared locks simultaneously for reading but only one transaction to acquire an exclusive lock for
writing.

Lock-based protocols ensure that transactions access shared resources in a coordinated and
controlled manner, avoiding conflicts and data inconsistencies. However, they may also lead to
issues like lock contention, deadlock, and reduced concurrency, especially in high-concurrency
environments. To strike a balance between data consistency and performance, different DBMS
implementations may use various lock-based protocols or combine them with other concurrency
control mechanisms.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy