Professional Documents
Culture Documents
DBMS 3rd IT FAQ
DBMS 3rd IT FAQ
Degree Engineering
Database Users:
Users are differentiated by the way they expect to interact with the system:
● Application programmers:
○ Application programmers are computer professionals who write
application programs. Application programmers can choose from many
tools to develop user interfaces.
○ Rapid application development (RAD) tools are tools that enable an
application programmer to construct forms and reports without writing a
program.
● Sophisticated users:
○ Sophisticated users interact with the system without writing programs.
Instead, they form their requests in a database query language.
○ They submit each such query to a query processor, whose function is to
break down DML statements into instructions that the storage manager
understands.
● Specialized users :
○ Specialized users are sophisticated users who write specialized database
applications that do not fit into the traditional data-processing framework.
○ Among these applications are computer-aided design systems, knowledge
base and expert systems, systems that store data with complex data types
(for example, graphics data and audio data), and environment-modeling
systems.
● Naïve users :
○ Naive users are unsophisticated users who interact with the system by
invoking one of the application programs that have been written
previously.
○ For example, a bank teller who needs to transfer $50 from account A to
account B invokes a program called transfer. This program asks the teller
for the amount of money to be transferred, the account from which the
money is to be transferred, and the account to which the money is to be
transferred.
Database Administrator:
● Coordinates all the activities of the database system. The database administrator
has a good understanding of the enterprise’s information resources and needs.
● Database administrator's duties include:
○ Schema definition: The DBA creates the original database schema by
executing a set of data definition statements in the DDL.
○ Storage structure and access method definition.
○ Schema and physical organization modification: The DBA carries out
changes to the schema and physical organization to reflect the changing
needs of the organization, or to alter the physical organization to improve
performance.
○ Granting user authority to access the database: By granting different types
of authorization, the database administrator can regulate which parts of
the database various users can access.
○ Specifying integrity constraints.
○ Monitoring performance and responding to changes in requirements.
Query Processor:
The query processor will accept query from user and solves it by accessing the
database.
Parts of Query processor:
DDL interpreter
This will interprets DDL statements and fetch the definitions in the data dictionary.
DML compiler
a. This will translates DML statements in a query language into low level instructions
that the query evaluation engine understands.
b. A query can usually be translated into any of a number of alternative evaluation plans
for same query result DML compiler will select best plan for query optimization.
Query evaluation engine
This engine will execute low-level instructions generated by the DML compiler on
DBMS.
Storage Manager/Storage Management:
A storage manager is a program module which acts like interface between the data
stored in a database and the application programs and queries submitted to the system.
Thus, the storage manager is responsible for storing, retrieving and updating data in the
database.
The storage manager components include:
Authorization and integrity manager: Checks for integrity constraints and authority of
users to access data.
Transaction manager:
Ensures that the database remains in a consistent state although there are system
failures.
File manager:
Manages the allocation of space on disk storage and the data structures used to
represent information stored on disk.
Buffer manager:
It is responsible for retrieving data from disk storage into main memory. It enables the
database to handle data sizes that are much larger than the size of main memory.
Data structures implemented by storage manager.
Data files:
Stored in the database itself.
Data dictionary:
Stores metadata about the structure of the database.
Indices:
Provide fast access to data items.
A:2 schema is the skeleton structure that represents the logical view of the entire
database. It defines how the data is organized and how the relations among them are
associated. It formulates all the constraints that are to be applied on the data.
Database Management System: A database management system (or DBMS) is
essentially nothing more than a computerized data-keeping system. Users of the
system are given facilities to perform several kinds of operations on such a system for
either manipulation of the data in the database or the management of the database
structure itself.
Physical Data Independence: Physical Data Independence is defined as the ability to
make changes in the structure of the lowest level of the Database Management System
(DBMS) without affecting the higher-level schemas. Hence, modification in the Physical
level should not result in any changes in the Logical or View levels.
Q:3 Explain three levels of data abstraction. [winter - 2021(3 marks)]
A: 3
The ANSI SPARC architecture divided into three levels:
1) External level
2) Conceptual level
3) Internal level
Internal Level
∙ This is the lowest level of the data abstraction.
∙ It describes how the data are actually stored on storage devices.
∙ It is also known as a physical level.
∙ The internal view is described by internal schema.
∙ Internal schema consists of definition of stored record, method of representing the data
field and access method used.
Conceptual Level
∙ This is the next higher level of the data abstraction.
∙ It describes what data are stored in the database and what relationships exist among
those data.
∙ It is also known as a logical level.
∙ Conceptual view is defined by conceptual schema. It describes all records and
relationship.
External Level
∙ This is the highest level of data abstraction.
∙ It is also known as view level.
∙ It describes only part of the entire database that a particular end user requires. ∙
External view is describes by external schema.
∙ External schema consists of definition of logical records, relationship in the external
view and method of deriving the objects from the conceptual view.
∙ This object includes entities, attributes and relationship.
Q:4 What are the main functions of a database administrator? [summer - 2020(3
marks)]
A:4 Main functions are as follow -
● working with database software to find ways to store, organise and manage data.
● troubleshooting.
● keeping databases up to date.
● helping with database design and development.
● managing database access.
● designing maintenance procedures and putting them into operation.
Q:5 Describe tasks performed by the Database Administrator. [winter - 2020(4
marks)]
A:5
1. Software Installation and Maintenance
A DBA often collaborates on the initial installation and configuration of a new Oracle,
SQL Server etc database. The system administrator sets up hardware and deploys the
operating system for the database server, and then the DBA installs the database
software and configures it for use. As updates and patches are required, the DBA
handles this ongoing maintenance.
And if a new server is needed, the DBA handles the transfer of data from the existing
system to the new platform.
2. Data Extraction, Transformation, and Loading
Known as ETL, data extraction, transformation, and loading refers to efficiently
importing large volumes of data that have been extracted from multiple systems into a
data warehouse environment.
This external data is cleaned up and transformed to fit the desired format so that it can
be imported into a central repository.
3. Specialised Data Handling
Today’s databases can be massive and may contain unstructured data types such as
images, documents, or sound and video files. Managing a very large database (VLDB)
may require higher-level skills and additional monitoring and tuning to maintain
efficiency.
4. Database Backup and Recovery
DBAs create backup and recovery plans and procedures based on industry best
practices, then make sure that the necessary steps are followed. Backups cost time and
money, so the DBA may have to persuade management to take necessary precautions
to preserve data.
System admins or other personnel may actually create the backups, but it is the DBA’s
responsibility to make sure that everything is done on schedule.
5. Security
A DBA needs to know potential weaknesses of the database software and the
company’s overall system and work to minimise risks. No system is one hundred per
cent immune to attacks, but implementing best practices can minimise risks.
In the case of a security breach or irregularity, the DBA can consult audit logs to see
who has done what to the data. Audit trails are also important when working with
regulated data.
6. Authentication
Setting up employee access is an important aspect of database security. DBAs control
who has access and what type of access they are allowed.
7. Capacity Planning
The DBA needs to know how large the database currently is and how fast it is growing
in order to make predictions about future needs. Storage refers to how much room the
database takes up in server and backup space. Capacity refers to usage level.
8. Performance Monitoring
Monitoring databases for performance issues is part of the ongoing system
maintenance a DBA performs. If some part of the system is slowing down processing,
the DBA may need to make configuration changes to the software or add additional
hardware capacity.
9. Database Tuning
Performance monitoring shows where the database should be tweaked to operate as
efficiently as possible. The physical configuration, the way the database is indexed, and
how queries are handled can all have a dramatic effect on database performance.
With effective monitoring, it is possible to proactively tune a system based on
application and usage instead of waiting until a problem develops.
10. Troubleshooting
DBAs are on call for troubleshooting in case of any problems. Whether they need to
quickly restore lost data or correct an issue to minimise damage, a DBA needs to
quickly understand and respond to problems when they occur.
Q:6 Explain the difference between physical and logical data
independence.[summer - 2020(4 marks)]
A: 6
Here is the differences between Physical and Logical Data Independence.
Parameters Physical Data Independence Logical Data Independence
Degree of The changes made at the Any changes made at the physical
Changes physical level need not be made level need to be made at the
Required at the application level. application level as well.
Internal We may or may not need the Making modifications at the logical
Modification modifications at the internal level is a prerequisite whether we
level for improving the want to change the database
performance of a system’s structure or not.
structure.
A: 7
Q.7 Enlist and explain the advantages of DBMS over traditional file
system.[Winter-2019(7 marks)]
A:7
File System: A File Management system is a DBMS that allows access to single files or
tables at a time. In a File System, data is directly stored in a set of files. It contains flat
files that have no relation to other files (when only one table is stored in a single file,
then this file is known as a flat file).
A:8
The overall design of the database is called database schema. Schema will not be
changed frequently. It is the logical structure of a database. It does not show the data in
the database.
The schema is pictorially represented as follows −
Types of Schema
The different types of schemas are as follows −
● Physical schema − It is a database design at the physical level.It is hidden
below the logical schema and can be changed easily without affecting the
application programs.
● Logical schema − It is a database design at the logical level. Programmers
construct applications using logical schema.
● External − It is schema at view level. It is the highest level of a schema
which defines the views for end users.
Generally the Database Management System (DBMS) assists one physical schema,
one logical schema and several sub or external schemas.
Database schema refers to the format and layout of the database in which the data will
be stored. It is the one thing that remains the same throughout unless otherwise
modified. It defines the Instance
Instance or extension or database state is a collection of information that stored in a
database at a particular moment is called an instance of the database. The Database
instance refers to the information stored in the database at a given point of time. Thus,
it is a dynamic value which keeps on changing.
Unit - 2: Data models
Q:1 Differentiate strong entity set and weak entity set. Demonstrate the concept
of both using real-time example using E-R diagram.[winter - 2020(7 marks)]
A:1
Primary key Strong entity has primary Weak entity does not have any
key. primary key. But, it has a partial
discriminator key.
Key attribute Strong entity has a key Weak entity does not have any
attribute. key attribute.
Participation Strong entity can either Weak entity always have total
have no participation or participation.
total participation.
Weak Entity
Q:2 What is integrity constraint? Explain primary key, reference key and check
constraint with SQL syntax. [winter - 2021(7 marks)]
A:2
● 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.
1. Check
This constraint defines a business rule on a column. All the rows in that column
must satisfy this rule.
Limits the data values of variables to a specific set, range, or list of values.
The constraint can be applied for a single column or a group of columns.
E.g. value of SPI should be between 0 to 10.
2. Primary key
This constraint defines a column or combination of columns which uniquely
identifies each row in the table.
Primary key = Unique key + Not null
E.g enrollment no column should have unique value as well as can’t be null.
3. Foreign key
A referential integrity constraint (foreign key) is specified between two tables.
In the referential integrity constraints, if a foreign key column in table 1 refers to
the primary key column of table 2, then every value of the foreign key column in
table 1 must be null or be available in primary key column of table 2.
An entity set that does not have a primary key is called weak entity set. ∙ The existence
of a weak entity set depends on the existence of a strong entity set. ∙ Weak entity set is
indicated by double rectangle.
❖ Weak entity relationship set is indicated by double diamond.
❖ The discriminator (partial key) of a weak entity set is the set of attributes that
distinguishes between all the entities of a weak entity set.
❖ The primary key of a weak entity set is created by combining the primary key of
the strong entity set on which the weak entity set is existence dependent and the
weak entity set’s discriminator.
❖ We underline the discriminator attribute of a weak entity set with a dashed line. ∙
E.g. in below fig. there are two entities loan and payment in which loan is strong
entity set and payment is weak entity set.
❖ Payment entity has payment-no which is discriminator.
❖ Loan entity has loan-no as primary key.
❖ So primary key for payment is (loan-no, payment-no).
Q:5 Why do we require E-R model? Explain the term
‘Generalization’,‘Specialization’ and ‘Aggregation’. [winter - 2021(7 marks)]
A:5
Entity-relationship (ER) model/diagram is a graphical representation of entities and their
relationships to each other with their attributes.
We can have three sub entities as Car, Truck, Motorcycle and these three entities can
be generalized into one general super class as Vehicle.
It is a form of abstraction that specifies two or more entities (sub class) having common
characters that can be generalized into one single entity (super class) at higher level
hiding all the differences.
Specialization
Specialization is a process of identifying subsets of an entity that shares different
characteristics. It breaks an entity into multiple entities from higher level (super class) to
lower level (sub class). The breaking of higher level entity is based on some
distinguishing characteristics of the entities in super class.
It is a top down approach in which we first define the super class and then sub class
and then their attributes and relationships.
Aggregation
Aggregation represents relationship between a whole object and its component. Using
aggregation we can express relationship among relationships. Aggregation shows
‘has-a’ or ‘is-part-of’ relationship between entities where one represents the ‘whole’ and
other ‘part’.
Consider a ternary relationship Works_On between Employee, Branch and Manager.
Now the best way to model this situation is to use aggregation, So, the relationship-set,
Works_On is a higher level entity-set. Such an entity-set is treated in the same manner
as any other entity-set. We can create a binary relationship, Manager, between
Works_On and Manager to represent who manages what tasks.
Q:6 What is the similarity between relational model and E-R model? How the
entity, attributes, primary key and relationship are shown in the relational model.
[winter - 2021(7 marks)]
1. Attribute:
Each column in a Table. Attributes are the properties which define a relation. e.g.,
Student_Rollno, NAME,etc.
2. Entity:
For example: In the following ER diagram we have two entities Student and College and
these two entities have many to one relationship as many students study in a single
college. We will read more about relationships later, for now focus on entities.
3. Relationship
Cardinality: Defines the numerical attributes of the relationship between two entities or
entity sets.
4. Primary Key:
The primary key is an attribute or a set of attributes that uniquely identify a specific
instance of an entity. Every entity in the data model must have a primary key whose
values uniquely identify instances of the entity.
Q:7 Describe the differences in meaning between the terms relation and relation
schema.[summer - 2020(3 marks)]
A:7 Relation schema: A set of attributes is called a relation schema (or relation
scheme). A relation schema is also known as table schema (or table scheme). A
relation schema can be thought of as the basic information describing a table or
relation. It is the logical definition of a table. Relation schema defines what the name of
the table is. This includes a set of column names, the data types associated with each
column.
Relational schema may also refer to as database schema. A database schema is the
collection of relation schemas for a whole database. Relational or Database schema is
a collection of meta-data. Database schema describes the structure and constraints of
data representing in a particular domain. A Relational schema can be described a
blueprint of a database that outlines the way data is organized into tables. This blueprint
will not contain any type of data. In a relational schema, each tuple is divided into fields
called Domains.
A:8
Since there is not subscript under the joins, the joins are natural joins, i.e., the
The latter version can be refined by pushing the projection through the union:
πsid(σcolour=0red0 (Part) ✶ Catalog) ∪ πsid(σcolour=0green0 (Part) ✶ Catalog).
A:9
a. If a pair of entity sets are connected by a path in an E-R diagram, the entity sets are
related, though perhaps indirectly. A disconnected graph implies that there are pairs of
entity sets that are unrelated to each other. If we split the graph into connected
components, we have, in effect, a separate database corresponding to each connected
component.
b. As indicated in the answer to the previous part, a path in the graph between a pair of
entity sets indicates a (possibly indirect) relationship between the two entity sets. If
there is a cycle in the graph then every pair of entity sets on the cycle are related to
each other in at least two distinct ways. If the E-R diagram is acyclic then there is a
unique path between every pair of entity sets and, thus, a unique relationship between
every pair of entity sets.
Q: 10 Draw ER diagram for university database consisting four entities Student,
Department, Class and Faculty.
Student has a unique id, the student can enroll for multiple classes and has a
most one major. Faculty must belong to department and faculty can teach
multiple classes. Each class is taught by only faculty. Every student will get grade
for the class he/she has enrolled.
A:10
Q:11 Draw an E-R diagram of following scenario. Make necessary assumptions
and clearly note down the same. We would like to make our College’s manually
operated Library to fully computerized .[winter 2021(7 marks)]
A:11
Q:12 Draw E-R diagram for student management system with the necessary
assumption. [summer - 2021(7 marks)]
A:12
It is expressed as the number of entities to which another entity can be associated via a
relationship set.
For binary relationship set there are entity set A and B then the mapping cardinality can
be one of the following −
● One-to-one
● One-to-many
● Many-to-one
● Many-to-many
One-to-one relationship
Example
One-to-many relationship
An entity set A is associated with any number of entities in B with a possibility of zero
and entity in B is associated with at most one entity in A.
Example
Many-to-one relationship
An entity set A is associated with at most one entity in B and an entity set in B can be
associated with any number of entities in A with a possibility of zero.
Example
Many-to-many relationship
Example
A:14
A:15 Generalization, Specialization and Aggregation in ER model are used for data
abstraction in which abstraction mechanism is used to hide details of a set of objects.
Generalization –
Generalization is the process of extracting common properties from a set of entities and
create a generalized entity from it. It is a bottom-up approach in which two or more
entities can be generalized to a higher level entity if they have some attributes in
common. For Example, STUDENT and FACULTY can be generalized to a higher level
entity called PERSON as shown in Figure 1. In this case, common attributes like
P_NAME, P_ADD become part of higher entity (PERSON) and specialized attributes
like S_FEE become part of specialized entity (STUDENT).
Specialization –
In specialization, an entity is divided into sub-entities based on their characteristics. It is
a top-down approach where higher level entity is specialized into two or more lower
level entities. For Example, EMPLOYEE entity in an Employee management system
can be specialized into DEVELOPER, TESTER etc. as shown in Figure 2. In this case,
common attributes like E_NAME, E_SAL etc. become part of higher entity
(EMPLOYEE) and specialized attributes like TES_TYPE become part of specialized
entity (TESTER).
Q:16 What do you mean by integrity constraints? Discuss various
A:16 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.
A:3
All those Operators which operate on a single operand are known as unary operators.
There are various types of unary operators in relational algebra.
Types of Unary operators
Unary operators are of three types
1. Projection Operator
2. Selection Operator
3. Rename Operator
Projection Operator
● Projection Operator (π) displays the columns of a table based on the specified
attributes.
● It just works on Columns
Syntax
π<attribute list>(R)
Selection Operator
● Selection Operator (σ) performs a selection operation.
● It selects those rows or tuples from the table which satisfies the selection
condition.
● It works with rows(tuples)
Syntax
σ<selection_condition>(R)
A) Get the details about all flights from Chennai to New Delhi.
A:6
Q.7 List the relational algebra operators. Discuss any two such algebra operator
with suitable example. [Winter- 2019(4 marks)]
A:7
Relational algebra is a procedural query language. It gives a step by step process to
obtain the result of the query. It uses operators to perform queries.
1. Select Operation:
Where:
p is used as a propositional logic formula which may use connectors like: AND OR and
NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
Input:
1. σ BRANCH_NAME="perryride" (LOAN)
Output:
2. Project Operation:
● This operation shows the list of those attributes that we wish to appear in the
result. Rest of the attributes are eliminated from the table.
● It is denoted by ∏.
1. Notation: ∏ A1, A2, An (r)
Where
Input:
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
1.Find the names of all employees who work for First Bank Corporation.
2.Find the names and cities of residence of all employees who work for First Bank
Corporation.
3.. Find the names, street address, and cities of residence of all employees who
work for First Bank Corporation and earn more than $10,000 per annum.
4. Find the names of all employees in this database who do not work for First
A:8
1.Find the names of all employees who work for First Bank Corporation.
2.Find the names and cities of residence of all employees who work for First Bank
Corporation.
3.. Find the names, street address, and cities of residence of all employees who
work for First Bank Corporation and earn more than $10,000 per annum.
4. Find the names of all employees in this database who do not work for First
Bank Corporation.
A:1
A:2
It is possible that the same information may be duplicated in different files. This leads to
data redundancy.
● Data redundancy results in memory wastage.
● For example, consider that some customers have both kinds of accounts - saving
and current. In this case, data about customers such as name, address, e-mail
and contact number will be duplicated in both files, saving accounts file and
current account file.
● In other words, same information will be stored in two different locations (files).
And, it wastes memory.
Various types of anomalies can occur in a DB. For instance, redundancy anomalies are
a very significant issue for tests if you’re a student, and for job interviews if you’re
searching for a job. But these can be easily identified and fixed. The following are
actually the ones about which we should be worried:
1. Update
2. Insert
3. Delete
Anomalies in databases can be, thus, divided into three major categories:
Update Anomaly
Employee David has two rows in the table given above since he works in two different
departments. If we want to change David’s address, we must do so in two rows, else
the data would become inconsistent.
If the proper address is updated in one of the departments but not in another, David will
have two different addresses in the database, which is incorrect and leads to
inconsistent data.
Insert Anomaly
If a new worker joins the firm and is currently unassigned to any department, we will be
unable to put the data into the table because the w_dept field does not allow nulls.
Delete Anomaly
If the corporation closes the department F890 at some point in the future, deleting the
rows with w_dept as F890 will also erase the information of employee Mike, who is
solely assigned to this department.
A:3
using Transitivity on (i) and (ii) we get
{E, F} → {G, I, J} .......(vi) ·
Also {E, F, H} → {E, F, H} is trivial, combine this with (viii) using union, we get:
{E, F, H} → {E, F, G, H, I, J, K, L, M, N}
⇒ {E, F, H} → R
(c) {E, F, H, K, L} is a super key but not a key since it is not minimal (i.e. contains extra
attributes)
functional dependencies hold: {A–>B, BC–> D, E–>C, D–>A}. What are the
candidate keys of R?( Any 1 in case of more than one candidate key) [winter -
2020(4 marks)]
A:4
A → B, BC → D, E → C, D → A
We start form set of all the attributes and reduce them using given functional
dependencies
ABCDEH ABCDEH
ABCEH {BC- → D}
ABEH{E → C} BCDEH {D → A}
AEH {A → B} BEH {E → C}
ABCDEH ACDEH {A → B} ADEH{E → C} DEH{D → A}
So candidate keys are AEH, BEH & DEH .
Q:5 Define the terms: a) Primary Key b) Super Key [winter - 2020(3 marks)]
A:5
A primary key, also called a primary keyword, is a column in a relational database table
that's distinctive for each record. It's a unique identifier, such as a driver's license
number, telephone number with area code or vehicle identification number (VIN). A
relational database must have only one primary key.
Super key is a single key or a group of multiple keys that can uniquely identify tuples in
a table. Super keys can contain redundant attributes that might not be important for
identifying tuples. Candidate keys are a subset of Super keys
Q:7 Define the terms : a) foreign key b) candidate key.[winter - 2021(3 marks)]
A:7
Foreign Key – is a column that creates a relationship between two tables. The purpose
of Foreign keys is to maintain data integrity and allow navigation between two different
instances of an entity.
Candidate Key – is a set of attributes that uniquely identify tuples in a table. Candidate
Key is a super key with no repeated attributes.
Q:8 What is normalization? Explain 2NF. [summer - 2020(3 marks)]
A:8
● Normalization is the process of organizing the data in the database.
● 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.
● Normalization divides the larger table into smaller and links them using
relationships.
● The normal form is used to reduce redundancy from the database table.
Second Normal Form (2NF):
Second Normal Form (2NF) is based on the concept of full functional dependency.
Second Normal Form applies to relations with composite keys, that is, relations with a
primary key composed of two or more attributes. A relation with a single-attribute
primary key is automatically in at least 2NF. A relation that is not in 2NF may suffer from
the update anomalies.
To be in second normal form, a relation must be in first normal form and relation must
not contain any partial dependency. A relation is in 2NF if it has No Partial Dependency,
i.e., no non-prime attribute (attributes which are not part of any candidate key) is
dependent on any proper subset of any candidate key of the table.
Q:9 Compute the closure of the following set F of functional dependencies for
relation schema R = (A, B, C, D, E).
A->BC
CD-> E
B -> D
E -> A
List the candidate keys for R.
A:9
A -> BC, B -> D so A -> D so A -> DC -> E
therefore A -> ABCDE
E -> A, A -> ABCDE, so E -> ABCDE
CD -> E, so CD -> ABCDE
B -> D, BC -> CD, so BC -> ABCDE
Attribute closure:
A -> ABCDE
B -> BD
C -> C
D -> D
E -> ABCDE
AB -> ABCDE
AC -> ABCDE
AD -> ABCDE
AE -> ABCDE
BC -> ABCDE
BD -> BD
BE -> ABCDE
CD -> ABCDE
CE -> ABCDE
DE -> ABCDE
ABC -> ABCDE
ABD -> ABCDE
ABE -> ABCDE
ACD -> ABCDE
ACE -> ABCDE
ADE -> ABCDE
BCD -> ABCDE
BDE -> ABCDE
CDE -> ABCDE
ABCD -> ABCDE
ABCE -> ABCDE
ABDE -> ABCDE
ACDE -> ABCDE
BCDE -> ABCDE
We have again divided our tables and created a new table which stores Salutations.
There are no transitive functional dependencies, and hence our table is in 3NF
RAID technology
There are 7 levels of RAID schemes. These schemas are as RAID 0, RAID 1, ...., RAID
6.
● In this technology, the operating system views these separate disks as a single
logical disk.
● In this technology, data is distributed across the physical drives of the array.
● In case of disk failure, the parity information can be helped to recover the data.
A:12
A:13
1NF
2. Lecturer Number (FK), Subject Code (PK), Subject Name, Subject Leve
l 2NF
2. Lecturer Number (FK), Subject Code (PK) 3. Subject Code (PK), Subject Name,
Subject Level
3NF
A relation is said to be in 1NF (first normal form), if it doesn’t contain any multi-valued
attribute. In other words you can say that a relation is in 1NF if each attribute contains
only atomic(single) value only.
As per the rule of first normal form, an attribute (column) of a table cannot hold multiple
values. It should hold only atomic values.
Example: Let’s say a company wants to store the names and contact details of its
employees. It creates a table in the database that looks like this:
Two employees (Jon & Lester) have two mobile numbers that caused the Emp_Mobile
field to have multiple values for these two employees.
This table is not in 1NF as the rule says “each attribute of a table must have atomic
(single) values”, the Emp_Mobile values for employees Jon & Lester violates that rule.
To make the table complies with 1NF we need to create separate rows for the each
mobile number in such a way so that none of the attributes contains multiple values.
Example: Let’s say a school wants to store the data of teachers and the subjects they
teach. They create a table Teacher that looks like this: Since a teacher can teach more
than one subjects, the table can have multiple rows for a same teacher.
This table is in 1 NF because each attribute has atomic values. However, it is not in 2NF
because non prime attribute Teacher_Age is dependent on Teacher_Id alone which is a
proper subset of candidate key. This violates the rule for 2NF as the rule says “no
non-prime attribute is dependent on the proper subset of any candidate key of the
table”.
To make the table complies with 2NF we can disintegrate it in two tables like this:
Teacher_Details table:
Third Normal form (3NF)
An attribute that is not part of any candidate key is known as non-prime attribute.
In other words 3NF can be explained like this: A table is in 3NF if it is in 2NF and for
each functional dependency X-> Y at least one of the following conditions hold:
An attribute that is a part of one of the candidate keys is known as prime attribute.
Example: Let’s say a company wants to store the complete address of each employee,
they create a table named Employee_Details that looks like this:
To make this table complies with 3NF we have to disintegrate the table into two tables to
remove the transitive dependency:
Unit - 5: Query processing and optimization
Q:1 What is Query processing? Explain why ‘Parsing and translation’ and
‘Optimization’ steps are required for query processing. [winter - 2021(7 marks)]
A:1
Query Processing is the activity performed in extracting data from the database. In
query processing, it takes various steps for fetching the data from the database. The
steps involved are:
● Parsing and translation
● Optimization
● Evaluation
As query processing includes certain activities for data retrieval. Initially, the given user
queries get translated in high-level database languages such as SQL. It gets translated
into expressions that can be further used at the physical level of the file system. After
this, the actual evaluation of the queries and a variety of query -optimizing
transformations and takes place. Thus before processing a query, a computer system
needs to translate the query into a human-readable and understandable language.
Consequently, SQL or Structured Query Language is the best suitable choice for
humans. But, it is not perfectly suitable for the internal representation of the query to the
system. Relational algebra is well suited for the internal representation of a query. The
translation process in query processing is similar to the parser of a query. When a user
executes any query, for generating the internal form of the query, the parser in the
system checks the syntax of the query, verifies the name of the relation in the database,
the tuple, and finally the required attribute value. The parser creates a tree of the query,
known as 'parse-tree.' Further, translate it into the form of relational algebra. With this, it
evenly replaces all the use of the views when used in the query.
Q:2 List the type of joins in relational algebra. Explain with example. [winte2020(4
marks)]
A:2
Join in DBMS is a binary operation which allows you to combine join product and
selection in one single statement. The goal of creating a join condition is that it helps
you to combine the data from two or more DBMS tables. The tables in DBMS are
associated using the primary key and foreign keys.
Types of Join
Inner Join
Inner Join is used to return rows from both tables which satisfy the given condition. It is
the most widely used join operation and can be considered as a default join-type
● Theta join
● Natural join
● EQUI join
Theta Join
Theta Join allows you to merge two tables based on the condition represented by
theta. Theta joins work for all comparison operators. It is denoted by symbol θ. The
general case of JOIN operation is called a Theta join.
Syntax: A ⋈θ B
EQUI Join
EQUI Join is done when a Theta join uses only the equivalence condition. EQUI join is
the most difficult operation to implement efficiently in an RDBMS, and one reason why
RDBMS have essential performance problems.
Natural Join does not utilize any of the comparison operators. In this type of join, the
attributes should have the same name and domain. In Natural Join, there should be at
least one common attribute between two relations.
It performs selection forming equality on those attributes which appear in both relations
and eliminates the duplicate attributes.
Syntax:C ⋈ D
Outer Join
An Outer Join doesn’t require each record in the two join tables to have a matching
record. In this type of join, the table retains each record even if no other matching
record exists.
Left Outer Join returns all the rows from the table on the left even if no matching rows
have been found in the table on the right. When no matching record is found in the table
on the right, NULL is returned.
Syntax: A B
Right Outer Join ( A B)
Right Outer Join returns all the columns from the table on the right even if no matching
rows have been found in the table on the left. Where no matches have been found in
the table on the left, NULL is returned. RIGHT outer JOIN is the opposite of LEFT JOIN.
Syntax: A B
In a Full Outer Join , all tuples from both relations are included in the result, irrespective
of the matching condition.
Syntax: A B
Q:3 A:3
Query Processing is the activity performed in extracting data from the database.
In query processing, it takes various steps for fetching the data from the database.
The steps involved are:
As query processing includes certain activities for data retrieval. Initially, the given user
queries get translated in high-level database languages such as SQL.
It gets translated into expressions that can be further used at the physical levelof the file
system. After this, the actual evaluation of the queries and a variety of query -optimizing
transformations and takes place. Thus before processing a query, a computer system
needs to translate the query into a human-readable and understandable language.
Consequently, SQL or Structured Query Language is the best suitable choice for
humans. But, it is not perfectly suitable for the internal representation of the query to the
system. Relationalalgebra is well suited for the internal representation of a query. The
translation process in query processing is similar to the parser of a query. When a user
executes any query, for generating the internal form of the query, the parser in
thesystem checks the syntax of the query, verifies the name of the relation in the
database, the tuple, and finally the required attribute value. The parser creates a tree of
the query, known as 'parse-tree.' Further, translate it into the formof relational algebra.
With this, it evenly replaces all the use of the views when used in the query.
-In order to fully evaluate a query, the system needs to construct a query evaluation
plan.
-The annotations in the evaluation plan may refer to the algorithms to be used for the
particular index or the specific operations.
-Such relational algebra with annotations is referred to as Evaluation Primitives. The
evaluation primitives carry the instructions needed for the evaluation of the operation.
-Thus, a query evaluation plan defines a sequence of primitive operations used for
evaluating a query. The query evaluation plan is also referred to as the query execution
plan.
Optimization
The cost of the query evaluation can vary for different types of queries. Although the
system is responsible for constructing the evaluation plan, the user does need not to
write their query efficiently.
For optimizing a query, the query optimizer should have an estimated cost analysis of
each operation. It is because the overall operation cost depends on the memory
allocations to several operations, execution costs, and so on.
A:4
Block Nested Loop Join:
In block nested loop join, for a block of outer relation, all the tuples in that block are
compared with all the tuples of the inner relation, then only the next block of outer
relation is considered. All pairs of tuples which satisfy the condition are added in the
result of the join.
for each block bR in BR do
for each block bs in BS do
for each tuple tR in TR do
for each tuple ts in Ts do
compare (tR, ts) if they satisfies the condition
add them in the result of the join
end
end
end
end
Let’s look at some similar cases as nested loop join,
Case-1: Assume only two blocks of main memory are available to store blocks from R
and S relation.
For each block of relation R, we have to transfer all blocks of relation S and each block
of relation R should be transferred only once.
So, the total block transfers needed = BR+ BR * BS
Case-2: Assume one relation fits entirely in the main memory and there is at least space
for one extra block.
In this case, total block transfers needed are similar to nested loop join.
Block nested loop join algorithm reduces the access cost compared to nested loop join
if the main memory space allocated for join is limited.
Q.5 List the techniques to obtain the query cost. Explain any one.[winter - 2020(7
marks)]
A:6
Though a system can create multiple plans for a query, the chosen method should be
the best of all. It can be done by comparing each possible plan in terms of their
estimated cost. For calculating the net estimated cost of any plan, the cost of each
operation within a plan should be determined and combined to get the net estimated
cost of the query evaluation plan.
To estimate the cost of a query evaluation plan, we use the number of blocks
transferred from the disk, and the number of disks seeks. Suppose the disk has an
average block access time of ts seconds and takes an average of tT seconds to transfer
x data blocks. The block access time is the sum of disk seeks time and rotational
latency. It performs S seeks than the time taken will be b*tT + S*tS seconds. If tT=0.1 ms,
tS =4 ms, the block size is 4 KB, and its transfer rate is 40 MB per second. With this, we
can easily calculate the estimated cost of the given query evaluation plan.
Generally, for estimating the cost, we consider the worst case that could happen. The
users assume that initially, the data is read from the disk only. But there must be a
chance that the information is already present in the main memory. However, the users
usually ignore this effect, and due to this, the actual cost of execution comes out less
than the estimated value.
The response time, i.e., the time required to execute the plan, could be used for
estimating the cost of the query evaluation plan. But due to the following reasons, it
becomes difficult to calculate the response time without actually executing the query
evaluation plan:
● When the query begins its execution, the response time becomes dependent on
the contents stored in the buffer. But this information is difficult to retrieve when
the query is in optimized mode, or it is not available also.
● When a system with multiple disks is present, the response time depends on an
interrogation that in "what way accesses are distributed among the disks?". It is
difficult to estimate without having detailed knowledge of the data layout present
over the disk.
● Consequently, instead of minimizing the response time for any query evaluation
plan, the optimizers finds it better to reduce the total resource consumption of the
query plan. Thus to estimate the cost of a query evaluation plan, it is good to
minimize the resources used for accessing the disk or use of the extra resources.
Unit - 6: Storage strategies
Q:1 Differentiate dynamic hashing and static hashing. [winter - 2021(3 marks)]
Q: 2 What is index in the database? Explain sparse indices and Dense indices
with proper example. [winter - 2021(7 marks)]
A:2
● Indexes are special lookup tables that the database search engine can use to
speed up data retrieval.
● A database index is a data structure that improves the speed of data retrieval
operations
● on a database table.
● An index in a database is very similar to an index in the back of a book.
● Indexes are used to retrieve data from the database very fast.
Dense Index
● In dense index, there is an index record for every search key value in the
database.
● This makes searching faster but requires more space to store index records.
● In this, the number of records in the index table is same as the number of records
in the
● main table.
● Index records contain search key value and a pointer to the actual record on the
disk.
Sparse Index
● In sparse index, index records are not created for every search key.
● The index record appears only for a few items in the data file.
● It requires less space, less maintenance overhead for insertion, and deletions but
is slower
● compared to the dense index for locating records.
● To search a record in sparse index we search for a value that is less than or
equal to value
● in index for which we are looking.
● After getting the first record, linear search is performed to retrieve the desired
record.
● In the sparse indexing, as the size of the main table grows, the size of index table
also
● grows.
● Indexed sequential access file requires unique keys and periodic reorganization.
● Indexed sequential access file takes longer time to search the index for the data
access or retrieval.
● It requires more storage space.
● It is expensive because it requires special software.
● It is less efficient in the use of storage space as compared to other file
organizations.
B+ Tree
B+ Tree uses a tree-like structure to store records in file, as the name implies. It
employs the key indexing idea, in which the primary key is used to sort the records. An
index value is generated for each primary key and mapped to the record. The address
of a record in the file is the index of that record.
Unlike a binary search tree (BST), the B+ tree can contain more than two children. All
records are stored solely at the leaf node in this method. The leaf nodes are pointed to
by intermediate nodes. There are no records in them.
The above B+ tree shows this:
● The tree has only one root node, which is number 25.
● There is a node-based intermediary layer. They don’t keep the original record.
The only thing they have are pointers to the leaf node.
● The prior value of the root is stored in the nodes to the left of the root node, while
the next value is stored in the nodes to the right, i.e. 15 and 30.
● Only one leaf node contains only values, namely 10, 12, 17, 20, 24, 27, and 29.
● Because all of the leaf nodes are balanced, finding any record is much easier.
● This method allows you to search any record by following a single path and
accessing it quickly.
● Because all records are stored solely in the leaf nodes and ordered in a
sequential linked list, searching becomes very simple using this method.
● It’s easier and faster to navigate the tree structure.
● The size of the B+ tree is unrestricted; therefore, the number of records and the
structure of the B+ tree can both expand and shrink.
● It is a very balanced tree structure. Here, each insert, update, or deletion has no
effect on the tree’s performance.
● The B+ tree file organization method is very inefficient for the static method.
A:4 Hashing:
Hashing is a popular technique for storing and retrieving data as fast as possible. The
main reason behind using hashing is that it gives optimal results as it performs optimal
searches.
Why to use Hashing?
If you observe carefully, in a balanced binary search tree, if we try to search , insert or
delete any element then the time complexity for the same is O(logn). Now there might
be a situation when our applications want to do the same operations in a faster way i.e.
in a more optimized way and here hashing comes into play. In hashing, all the above
operations can be performed in O(1) i.e. constant time. It is important to understand that
the worst case time complexity for hashing remains O(n) but the average case time
complexity is O(1).
Now let us understand a few basic operations of hashing.
Basic Operations:
HashTable: This operation is used in order to create a new hash table.
Delete: This operation is used in order to delete a particular key-value pair from the
hash table.
Get: This operation is used in order to search a key inside the hash table and return the
value that is associated with that key.
Put: This operation is used in order to insert a new key-value pair inside the hash table.
A:5
B-tree in DBMS is an m-way tree which self balances itself. Due to their balanced
structure, such trees are frequently used to manage and organise enormous databases
and facilitate searches. In a B-tree, each node can have a maximum of n child nodes. In
DBMS, B-tree is an example of multilevel indexing. Leaf nodes and internal nodes will
both have record references. B Tree is called Balanced stored trees as all the leaf
nodes are at same levels.
Properties of B-Tree
● A non-leaf node's number of keys is one less than the number of its children.
● The number of keys in the root ranges from one to (m-1) maximum. Therefore,
root has a minimum of two and a maximum of m children.
● The keys range from min([m/2]-1) to max(m-1) for all nodes (non-leaf nodes)
besides the root. Thus, they can have between m and [m/2] children.
● The level of each leaf node is the same.
Unit - 7: Transaction processing
Q:1 How does two phase locking protocol differ from timestamp based protocol?
Explain timestamp-ordering protocol. [winter - 2021(7 marks)]
A:1
This locking protocol divides the execution phase of a transaction into three parts. In the
first part, when the transaction starts executing, it seeks permission for the locks it
requires. The second part is where the transaction acquires all the locks. As soon as the
transaction releases its first lock, the third phase starts. In this phase, the transaction
cannot demand any new locks; it only releases the acquired locks.
Two-phase locking has two phases, one is growing, where all the locks are being
acquired by the transaction; and the second phase is shrinking, where the locks held by
the transaction are being released.
To claim an exclusive (write) lock, a transaction must first acquire a shared (read) lock
and then upgrade it to an exclusive lock.
The first phase of Strict-2PL is same as 2PL. After acquiring all the locks in the first
phase, the transaction continues to execute normally. But in contrast to 2PL, Strict-2PL
does not release a lock after using it. Strict-2PL holds all the locks until the commit point
and releases all the locks at a time.
Operation rejected.
Operation rejected.
Q:2 Enlist and explain user authorization to modify the database schema. [winter
- 2021(4 marks)]
A:2
Grant
● It is a DCL command.
● It grants permissions to users on database objects.
● It can also be used to assign access rights to users.
● For every user, the permissions need to be specified.
● When the access is decentralized, permission granting is easier.
Syntax:
grant privilege_name on object_name
to {user_name | public | role_name}
Revoke
● It is a DCL command.
● It removes permissions if they are granted to users on database objects.
● It takes away/revokes the rights of the users.
● If access for a user is removed, all specific permissions provided by that user to
others will be removed.
● If decentralized access is used, it would be difficult to remove granted
permissions.
Syntax
revoke privilege_name on object_name
from {user_name | public | role_name}
Q:3 How does ‘partial commit’ state differ from ‘commit’ state of the transaction?
[winter - 2021(3 marks)]
A:3
1. Partially Committed –
After completion of all the read and write operation the changes are made in main
memory or local buffer. If the changes are made permanent on the DataBase then the
state will change to “committed state” and in case of failure it will go to the “failed state”.
2. Committed State –
It is the state when the changes are made permanent on the Data Base and the
transaction is complete and therefore terminated in the “terminated state”.
A:4
In the context of transaction processing, the acronym ACID refers to the four key
properties of a transaction: atomicity, consistency, isolation, and durability.
Atomicity
All changes to data are performed as if they are a single operation. That is, all the
changes are performed, or none of them are.
For example, in an application that transfers funds from one account to another, the
atomicity property ensures that, if a debit is made successfully from one account, the
corresponding credit is made to the other account.
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
For example, in an application that transfers funds from one account to another, the
isolation property ensures that another transaction sees the transferred funds in one
account or the other, but not in both, nor in neither.
Durability
After a transaction successfully completes, changes to data persist and are not undone,
even in the event of a system failure.
For example, in an application that transfers funds from one account to another, the
durability property ensures that the changes made to each account will not be reversed.
A:5
● A transaction consists of many steps. Some involve I/O activity; others involve
CPU activity. The CPU and the disks in a computer system can operate in
parallel. Therefore, I/O activity can be done in parallel with processing at the
CPU.
● The parallelism of the CPU and the I/O system can therefore be exploited to run
multiple transactions in parallel.
● While a read or write on behalf of one transaction is in progress on one disk,
another transaction can be running in the CPU, while another disk may be
executing a read or write on behalf of a third transaction.
● All of this increases the throughput of the system—that is, the number of
transactions executed in a given amount of time.
● Correspondingly, the processor and disk utilization also increase; in other words,
the processor and disk spend less time idle, or not performing any useful work.
● There may be a mix of transactions running on a system, some short and some
long.
● If transactions run serially, a short transaction may have to wait for a preceding
long transaction to complete, which can lead to unpredictable delays in running a
transaction.
● If the transactions are operating on different parts of the database, it is better to
let them run concurrently, sharing the CPU cycles and disk accesses among
them.
● Concurrent execution reduces the unpredictable delays in running transactions.
● Moreover, it also reduces the average response time: the average time for a
transaction to be completed after it has been submitted.
Q:6 What is transaction? What are the functions of commit and rollback?[summer
- 2020(4 marks)]
A:6
We can define a transaction as a group of tasks in DBMS. Here a single task refers to a
minimum processing unit, and we cannot divide it further. Now let us take the example
of a certain simple transaction. Suppose any worker transfers Rs 1000 from X’s account
to Y’s account. This given small and simple transaction involves various low-level tasks.
X’s Account
Open_Account(X)
Old_Bank_Balance = X.balance
New_Bank_Balance = Old_Bank_Balance – 1000
A.balance = New_Bank_Balance
Close_Bank_Account(X)
Y’s Account
Open_Account(Y)
Old_Bank_Balance = Y.balance
New_Bank_Balance = Old_Bank_Balance + 1000
B.balance = New_Bank_Balance
Close_Bank_Account(Y)
1. COMMIT-
COMMIT in SQL is a transaction control language that is used to permanently save the
changes done in the transaction in tables/databases. The database cannot regain its
previous state after its execution of commit.
STAFF
sql>
SELECT *
FROM Staff
WHERE Allowance = 400;
sql> COMMIT;
Output:
So, the SELECT statement produced the output consisting of three rows.
2. ROLLBACK
STAFF
sql>
SELECT *
FROM EMPLOYEES
WHERE ALLOWANCE = 400;
sql> ROLLBACK;
Output:
So, the SELECT statement produced the same output with the ROLLBACK command.
Q:7 List and explain ACID properties with respect to Database transaction.
[winter - 2020(3 marks)]
A:7 A transaction is a single logical unit of work that accesses and possibly modifies the
contents of a database. Transactions access data using read and write operations.
In order to maintain consistency in a database, before and after the transaction, certain
properties are followed. These are called ACID properties.
Atomicity:
By this, we mean that either the entire transaction takes place at once or doesn’t
happen at all. There is no midway i.e. transactions do not occur partially. Each
transaction is considered as one unit and either runs to completion or is not executed at
all. It involves the following two operations.
—Abort: If a transaction aborts, changes made to the database are not visible.
Consistency:
This means that integrity constraints must be maintained so that the database is
consistent before and after the transaction. It refers to the correctness of a database.
Isolation:
This property ensures that multiple transactions can occur concurrently without leading
to the inconsistency of the database state. Transactions occur independently without
interference. Changes occurring in a particular transaction will not be visible to any
other transaction until that particular change in that transaction is written to memory or
has been committed. This property ensures that the execution of transactions
concurrently will result in a state that is equivalent to a state achieved these were
executed serially in some order.
Durability:
This property ensures that once the transaction has completed execution, the updates
and modifications to the database are stored in and written to disk and they persist even
if a system failure occurs. These updates now become permanent and are stored in
non-volatile memory. The effects of the transaction, thus, are never lost.
A:8
Conflict serializability:
Instructions Ii and Ij, of transactions Ti and Tj respectively, conflict if and only if there
exists some item P accessed by both Ii and Ij, and atleast one of these instructions
wrote P.
Consider the below operations-
i. Ii = read(P), Ij = read(P). Ii and Ij don’t conflict.
ii. Ii = read(P), Ij = write(P). They conflict.
iii. Ii = write(P), Ij = read(P). They conflict.
iv. Ii = write(P), Ij = write(P). They conflict.
• A conflict between Ii and Ij forces a temporal order between them.
• If Ii and Ij are consecutive in a schedule and they do not conflict, their results would
remain the same even if they had been interchanged in the schedule.
• If a schedule Scan be transformed in to a schedule S by a series of swaps of
non-conflicting instructions, then S and S` are conflict equivalent.
• In other words a schedule S is conflict serializable if it is conflict equivalent to a serial
schedule.
• Example of a schedule that is not conflict serializable:
T3 T4
Read(P)
Write(P)
Write(P)
• View serializability:
o S and S` are view equivalent if the following three conditions are met:
i. For each data item P, if transaction Ti reads the initial value of P in schedule S, then
transaction Ti must, in schedule S`, also read the initial value of P.
ii. For each data item P, if transaction Ti executes read (P)in schedule S, and that value
was produced by transaction Tj, then transaction Ti must in schedule S` also read the
value of P that was produced by transaction Tj.
iii. For each data item P, the transaction that performs the final write(P) operation in
schedule S must perform the final write(P) operation in schedule S`.
o Every view serializable schedule which is not conflict serializable has blind writes.
T3 T4 T6
Read(P)
Write(P)
Write(P)
Write(P)
Example:
Conflict Equivalent
T1 T2
Read(A)
Write(A)
Read(B)
Write(B)
Read(A)
Write(A)
Read(B)
Write(B)
A:10 In this type of protocol, any transaction cannot read or write data until it acquires
an appropriate lock on it. There are two types of lock:
1. Shared lock:
● It is also known as a Read-only lock. In a shared lock, the data item can only
read by the transaction.
● It can be shared between the transactions because when the transaction holds a
lock, then it can't update the data on the data item.
2. Exclusive lock:
● In the exclusive lock, the data item can be both reads as well as written by the
transaction.
● This lock is exclusive, and in this lock, multiple transactions do not modify the
same data simultaneously.
Q.11 With neat diagram explain data storage hierarchy so far..[winter - 2020(4
marks)]
A:11
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.
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
It is the primary area that offers quick access to the stored data. We also know the
primary storage as volatile storage. It is because this type of memory does not
permanently store the data.
Main Memory: It is the one that is responsible for operating the data that is available by
the storage medium. The main memory handles each instruction of a computer
machine. This type of memory can store gigabytes of data on a system but is small
enough to carry the entire database.
Cache: It is one of the costly storage media. On the other hand, it is the fastest one. A
cache is a tiny storage media which is maintained by the computer hardware usually.
Secondary Storage
Secondary storage is also called as Online storage. It is the storage area that allows the
user to save and store data permanently. This type of memory does not lose the data
due to any power failure or system crash. That's why we also call it non-volatile storage.
Flash Memory: A flash memory stores data in USB (Universal Serial Bus) keys which
are further plugged into the USB slots of a computer system. These USB keys help
transfer data to a computer system, but it varies in size limits.
Magnetic Disk Storage: This type of storage media is also known as online storage
media. A magnetic disk is used for storing the data for a long time. It is capable of
storing an entire database. It is the responsibility of the computer system to make
availability of the data from a disk to the main memory for further accessing.
Tertiary Storage
It is the storage type that is external from the computer system. It has the slowest
speed. But it is capable of storing a large amount of data. It is also known as Offline
storage. Tertiary storage is generally used for data backup. There are following tertiary
storage devices available:
● Tape Storage: It is the cheapest storage medium than disks. Generally, tapes
are used for archiving or backing up the data. It provides slow access to data as
it accesses data sequentially from the start. Thus, tape storage is also known as
sequential-access storage.
A:12
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 feared
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 Avoidance
● When a database is stuck in a deadlock state, then it is better to avoid the
database rather than aborting or restating the database. This is a waste of time
and resource.
Deadlock Detection
In a database, when a transaction waits indefinitely to obtain a lock, then the DBMS
should detect whether the transaction is involved in a deadlock or not. The lock
manager maintains a Wait for the graph to detect the deadlock cycle in the database.
● This is the suitable method for deadlock detection. In this method, a graph is
created based on the transaction and their lock. If the created graph has a cycle
or closed loop, then there is a deadlock.
● The wait for the graph is maintained by the system for every transaction which is
waiting for some data held by the others. The system keeps checking the graph if
there is any cycle in the graph.
The wait for a graph for the above scenario is shown below:
Deadlock Prevention
● Deadlock prevention method is suitable for a large database. If the resources are
allocated in such a way that deadlock never occurs, then the deadlock can be
prevented.
Wait-Die scheme
In this scheme, if a transaction requests for a resource which is already held with a
conflicting lock by another transaction then the DBMS simply checks the timestamp of
both transactions. It allows the older transaction to wait until the resource is available for
execution.
Let's assume there are two transactions Ti and Tj and let TS(T) is a timestamp of any
transaction T. If T2 holds a lock by some other transaction and T1 is requesting for
resources held by T2 then the following actions are performed by DBMS:
1. Check if TS(Ti) < TS(Tj) - If Ti is the older transaction and Tj has held some
resource, then Ti is allowed to wait until the data-item is available for execution.
That means if the older transaction is waiting for a resource which is locked by
the younger transaction, then the older transaction is allowed to wait for resource
until it is available.
2. Check if TS(Ti) < TS(Tj) - If Ti is older transaction and has held some resource
and if Tj is waiting for it, then Tj is killed and restarted later with the random delay
but with the same timestamp.
● In wound wait scheme, if the older transaction requests for a resource which is
held by the younger transaction, then older transaction forces younger one to kill
the transaction and release the resource. After the minute delay, the younger
transaction is restarted but with the same timestamp.
● If the older transaction has held a resource which is requested by the Younger
transaction, then the younger transaction is asked to wait until older releases it.
A:13
Atomicity
Atomicity refers to the fact that a transaction succeeds or it fails. It is an all-or-nothing
operation. Despite being composed of multiple steps, those steps are treated as a
single operation or a unit. In the example above, where a system crash stopped the
database mid-transaction, the transaction fails, rolling the database back to the previous
state and re-instating Alice’s money.
Consistency
Consistency refers to the characteristic that requires data updated via transactions to
respect the other constraints or rules within the database systems to keep data in a
consistent state
For example, you set in place SQL triggers or integrity constraints that check personal
balances and prevent an account from withdrawing more money than they have - your
app offers no credit. So if Alice started with $50, she would not be allowed to send 100
dollars to Bob.
Isolation
Modern DBMSs allow users to access data concurrently and in parallel. Isolation is the
characteristic that allows concurrency control so modifications from one transaction are
not affecting operations in another transaction. Two parallel transactions are in reality
isolated and seem to be performed sequentially.
Durability
The last ACID property, durability, refers to the persistence of committed transactions.
Transactions and database modifications are not kept in volatile memory but are saved
to permanent storage, such as disks.
1. Active State –
When the instructions of the transaction are running then the transaction is in active
state. If all the ‘read and write’ operations are performed without any error then it
goes to the “partially committed state”; if any instruction fails, it goes to the “failed
state”.
2. Partially Committed –
After completion of all the read and write operation the changes are made in main
memory or local buffer. If the changes are made permanent on the DataBase then
the state will change to “committed state” and in case of failure it will go to the “failed
state”.
3. Failed State –
When any instruction of the transaction fails, it goes to the “failed state” or if failure
occurs in making a permanent change of data on Data Base.
4. Aborted State –
After having any type of failure the transaction goes from “failed state” to “aborted
state” and since in previous states, the changes are only made to local buffer or
main memory and hence these changes are deleted or rolled-back.
5. Committed State –
It is the state when the changes are made permanent on the Data Base and the
transaction is complete and therefore terminated in the “terminated state”.
6. Terminated State –
If there isn’t any roll-back or the transaction comes from the “committed state”, then
the system is consistent and ready for new transaction and the old transaction is
terminated.
Q:15 What is dirty write in the transaction? Explain with example. [summer -
2021(3 marks)]
A:15
Dirty write is that a transaction updates or deletes (overwrites) the uncommitted data
which other transactions insert, update or delete.
Deadlock Avoidance –
When a database is stuck in a deadlock, It is always better to avoid the deadlock rather
than restarting or aborting the database. The deadlock avoidance method is suitable for
smaller databases whereas the deadlock prevention method is suitable for larger
databases.
One method of avoiding deadlock is using application-consistent logic. In the
above-given example, Transactions that access Students and Grades should always
access the tables in the same order. In this way, in the scenario described above,
Transaction T1 simply waits for transaction T2 to release the lock on Grades before it
begins. When transaction T2 releases the lock, Transaction T1 can proceed freely.
Another method for avoiding deadlock is to apply both row-level locking mechanism and
READ COMMITTED isolation level. However, It does not guarantee to remove
deadlocks completely.
Deadlock Detection –
When a transaction waits indefinitely to obtain a lock, The database management
system should detect whether the transaction is involved in a deadlock or not.
Wait-for-graph is one of the methods for detecting the deadlock situation. This method is
suitable for smaller databases. In this method, a graph is drawn based on the
transaction and their lock on the resource. If the graph created has a closed-loop or a
cycle, then there is a deadlock.
For the above-mentioned scenario, the Wait-For graph is drawn below
Deadlock prevention –
For a large database, the deadlock prevention method is suitable. A deadlock can be
prevented if the resources are allocated in such a way that deadlock never occurs. The
DBMS analyzes the operations whether they can create a deadlock situation or not, If
they do, that transaction is never allowed to be executed.
A:17
Every transaction will lock and unlock the data item in two different phases.
Growing Phase − All the locks are issued in this phase. No locks are released, after all
changes to data-items are committed and then the second phase (shrinking phase)
starts.
Shrinking phase − No locks are issued in this phase, all the changes to data-items are
noted (stored) and then locks are released.
After the lock point has been reached, the transaction enters a shrinking phase.
Types
A transaction can release a shared lock after the lock point, but it cannot release any
exclusive lock until the transaction commits. This protocol creates a cascade less
schedule.
A transaction cannot release any lock either shared or exclusive until it commits.
The 2PL protocol guarantees serializability, but cannot guarantee that deadlock will not
happen.
Example
Let T1 and T2 are two transactions.
Here,
In the above situation T1 waits for B and T2 waits for A. The waiting time never ends.
Both the transaction cannot proceed further at least any one releases the lock
voluntarily. This situation is called deadlock.
Wait for graph: It is used in the deadlock detection method, creating a node for each
transaction, creating an edge Ti to Tj, if Ti is waiting to lock an item locked by Tj. A cycle
in WFG indicates a deadlock has occurred. WFG is created at regular intervals.
Q:18 Differentiate shared lock and exclusive lock in lock-based protocol.
[summer - 2021(3 marks)]
A:18
Q:19 Discuss view serializability in transactions. [summer - 2021(4 marks)]
A:19
A:20
Q: 21 What is log-based recovery? List and explain various fields use in log
records for log-based recovery. [summer - 2021(3 marks)]
A:21
Log-based recovery in DBMS provides the ability to maintain or recover data in case of
system failure. DBMS keeps a record of every transaction on some stable storage
device to provide easy access to data when the system fails. A log file will be created
for every operation performed on the database at that point.
An update log record represented as: <Ti, Xj, V1, V2> has these fields:
Transaction identifier: Unique Identifier of the transaction that performed the write
operation.
Q: 22 What is the schedule in truncation? How to identify that the given schedule
is conflict serializable? Explain with example. [summer - 2021(7 marks)]
A:22
Conflicting Operations
Example:
Conflict Equivalent
Example:
A:1
SQL injection
To understand SQL injection, it’s important to know what structured query language
(SQL) is. SQL is a query language used in programming to access, modify, and delete
data stored in relational databases. Since the vast majority of websites and web
applications rely on SQL databases, an SQL injection attack can have serious
consequences for organizations.
An SQL query is a request sent to a database for some type of activity or function such
as query of data or execution of SQL code to be performed. An example is when login
information is submitted via a web form to allow a user access to a site. Typically, this
type of web form is designed to accept only specific types of data such as a name
and/or password. When that information is added, it's checked against a database, and
if it matches, the user is granted entry. If not, they are denied access.
Potential problems arise because most web forms have no way of stopping additional
information from being entered on the forms. Attackers can exploit this weakness and
use input boxes on the form to send their own requests to the database. This could
potentially allow them to carry out a range of nefarious activities, from stealing sensitive
data to manipulating the information in the database for their own ends.
Because of the prevalence of web sites and servers that use databases, SQL injection
vulnerabilities are one of the oldest and most widespread types of cyber assault.
Several developments in the hacker community have increased the risk of this type of
attack, most notably the advent of tools to detect and exploit SQL injection. Freely
available from open source developers, these tools allow cybercriminals to
automatically perform attacks in only a few minutes by allowing them to access any
table or any column in the database with just a click and attack process.
Symptoms of SQLi
A successful SQL injection attack may show no symptoms at all. However, sometimes
there are outward signs, which include:
Depending on how they gain access to back-end data and the extent of the potential
damage they cause, SQL injections fall into three categories:
● In-band SQLi:
This type of SQLi attack is straightforward for attackers since they use the same
communication channel to launch attacks and gather results. This type of SQLi attack
has two sub-variations:
This type of SQLi involves attackers using the response and behavioral patterns of the
server after sending data payloads to learn more about its structure. Data doesn’t
transfer from the website database to the attacker, so the attacker doesn’t see
information about the attack in-band (hence the term ‘blind SQLi). Inferential SQLi can
be classified into two sub-types:
● Time-based SQLi: Attackers send a SQL query to the database, making the
database wait for a few seconds before it responds to the query as true or false.
● Boolean SQLi: Attackers send a SQL query to the database, letting the
application respond by generating either a true or false result.
● Out-of-band SQLi:
When a server is either too slow or unstable to carry out these actions.
A successful SQL injection attack can have serious consequences for a business. This
is because an SQL injection attack can:
● Expose sensitive data. Attackers can retrieve data, which risks exposing
sensitive data stored on the SQL server.
● Compromise data integrity. Attackers can alter or delete information from your
system.
● Compromise users’ privacy. Depending on the data stored on the SQL server,
an attack can expose sensitive user information, such as addresses, telephone
numbers, and credit card details.
● Give an attacker admin access to your system. If a database user has
administrative privileges, an attacker can gain access to the system using
malicious code.
● Give an attacker general access to your system. If you use weak SQL
commands to check usernames and passwords, an attacker could gain access to
your system without knowing a user’s credentials. From there, an attacker can
wreak havoc by accessing and manipulating sensitive information.
The cost of an SQL injection attack is not just financial: it can also involve loss of
customer trust and reputational damage, should personal information such as names,
addresses, phone numbers, and credit card details be stolen. Once customer trust is
broken, it can be very difficult to repair.
A:2
An Intrusion Detection System (IDS) is a system that monitors network traffic for
suspicious activity and issues alerts when such activity is discovered. It is a software
application that scans a network or a system for the harmful activity or policy breaching.
Any malicious venture or violation is normally reported either to an administrator or
collected centrally using a security information and event management (SIEM) system.
A SIEM system integrates outputs from multiple sources and uses alarm filtering
techniques to differentiate malicious activity from false alarms.
Although intrusion detection systems monitor networks for potentially malicious activity,
they are also disposed to false alarms. Hence, organizations need to fine-tune their IDS
products when they first install them. It means properly setting up the intrusion detection
systems to recognize what normal traffic on the network looks like as compared to
malicious activity.
Intrusion prevention systems also monitor network packets inbound the system to check
the malicious activities involved in it and at once send the warning notifications.
Q:1 When Join is used in SQL? Explain Left outer, Right outer and Full outer join
with SQL syntax. [winter - 2021(7 marks)]
A:1
A join is used to query data from multiple tables and returns the combined result from
two or more tables through a condition. The condition in the join clause indicates how
columns are matched between the specified tables.
The Left Join clause joins two or more tables and returns all rows from the left table and
matched records from the right table or returns null if it does not find any matching
record. It is also known as Left Outer Join. So, Outer is the optional keyword to use with
Left Join.
The Right Join clause joins two or more tables and returns all rows from the right-hand
table, and only those results from the other table that fulfilled the specified join
condition. If it finds unmatched records from the left side table, it returns Null value. It is
also known as Right Outer Join. So, Outer is the optional clause to use with Right Join.
Q:2 When do we require to use group by clause? How aggregate functions are
used with group by clause? [winter - 2021(4 marks)]
A:2
The GROUP BY clause is used in SQL queries to organize data that have the same
attribute values. Usually, we use it with the SELECT statement. It is always to remember
that we have to place the GROUP BY clause after the WHERE clause. Additionally, it is
paced before the ORDER BY clause.
We can often use this clause in collaboration with aggregate functions like SUM, AVG,
MIN, MAX, and COUNT to produce summary reports from the database. It's important
to remember that the attribute in this clause must appear in the SELECT clause, not
under an aggregate function. If we do so, the query would be incorrect. As a result, the
GROUP BY clause is always used in conjunction with the SELECT clause. The query
for the GROUP BY clause is grouped query, and it returns a single row for each
grouped object.
Let us understand how the GROUP BY clause works with the help of an example. Here
we will demonstrate it with the same table.
Suppose we want to know developer's average salary in a particular state and organize
results in descending order based on the state column. In that case, we would need
both the GROUP BY and ORDER BY command to get the desired result. We can do
this by executing the command as follows:
mysql> SELECT D_state, avg(D_salary) AS salary
FROM developers
GROUP BY D_state
ORDER BY D_state DESC;
Q:3 Why does the trigger require in database? Write SQL syntax for creating
database trigger. [winter - 2021(3 marks)]
A:3
A trigger is a set of SQL statements that reside in system memory with unique names. It
is a specialized category of stored procedure that is called automatically when a
database server event occurs. Each trigger is always associated with a table.
A trigger is called a special procedure because it cannot be called directly like a stored
procedure. The key distinction between the trigger and procedure is that a trigger is
called automatically when a data modification event occurs against a table. A stored
procedure, on the other hand, must be invoked directly.
The following are the main characteristics that distinguish triggers from stored
procedures:
Syntax of Trigger
We can create a trigger in SQL Server by using the CREATE TRIGGER statement as
follows:
CREATE TRIGGER schema.trigger_name
ON table_name
AFTER {INSERT, UPDATE, DELETE}
[NOT FOR REPLICATION]
AS
{SQL_Statements}
Example:
CREATE TABLE Employee
(
Id INT PRIMARY KEY,
Name VARCHAR(45),
Salary INT,
Gender VARCHAR(12),
DepartmentId INT
)
Q: 4 What is the view? How does it different from the table? [winter - 2021(3
marks)]
A:4
TYPES OF VIEW
1. Simple View
2. Complex View
Syntax:
CREATE VIEW view_name
AS
SELECT column1, column2...
FROM table_name
[WHERE condition];
Simple View
Example:
--Create View
CREATE VIEW EmpSelect
AS
SELECT Eid, Ename, Department
FROM Employee;
--Display View
Select * from EmpSelect;
Complex View
● When we create a view on more than one table, it is called complex view.
● We can only update data in complex view.
● You can't insert data in complex view.
● In particular, complex views can contain: join conditions, a group by clause, an
order by clause etc.
Example:
--Create View
CREATE VIEW Empview
AS
SELECT Employee.Eid, Employee.Ename, ConcactDetails.City
FROM Employee Inner Join ConcactDetails
On Employee.Eid= ConcactDetails.Eid;
--Display View
Select * from Empview;
Q: 5 What is the difference between Join and Sub query? Explain any two built-in
function for following category. 1) Numeric 2) String 3) Date [winter - 2021(7
marks)]
A:5
A join is a query that combines records from two or more tables. A join will be performed
whenever multiple tables appear in the FROM clause of the query. The select list of the
query can select any columns from any of these tables. If join condition is omitted or
invalid then a Cartesian product is formed. If any two of these tables have a column
name in common, then must qualify these columns throughout the query with table or
table alias names to avoid ambiguity. Most join queries contain at least one join
condition, either in the FROM clause or in the WHERE clause.
what is Subquery?
A Subquery or Inner query or Nested query is a query within SQL query and embedded
within the WHERE clause. A Subquery is a SELECT statement that is embedded in a
clause of another SQL statement. They can be very useful to select rows from a table
with a condition that depends on the data in the same or another table. A Subquery is
used to return data that will be used in the main query as a condition to further restrict
the data to be retrieved. The subquery can be placed in the following SQL clauses they
are WHERE clause, HAVING clause, FROM clause.
1. String Functions :
The string functions of MySQL can manipulate the text string in many ways. Some
commonly used string functions are being discussed below :
2. Numeric Functions :
The number functions are those functions that accept numeric values and after
performing the required operations, return numeric values. Some useful numeric
functions are being discussed below :
3. Date Functions :
A:6 SQL ALL compares a value of the first table with all values of the second table and
returns the row if there is a match with all values.
For example, if we want to find teachers whose age is greater than all students, we can
use
SELECT *
FROM Teachers
SELECT age
FROM Students
);
SELECT age
FROM Students
returns all the ages from the Students table. And, the condition
compares the student ages (returned by subquery) with the teacher's age. If the
teacher's age is greater than all student's ages, the corresponding row of the Teachers
table is selected.
EXAMPLE:
Q:7 Define the terms : a) foreign key b) candidate key.[winter - 2021(3 marks)]
A:7
Foreign Key – is a column that creates a relationship between two tables. The purpose
of Foreign keys is to maintain data integrity and allow navigation between two different
instances of an entity.
Candidate Key – is a set of attributes that uniquely identify tuples in a table. Candidate
Key is a super key with no repeated attributes.
Q.8 Explain following SQL commands with syntax and significance.Commit &
Rollback.[winter - 2021 (3 marks)]
A.8
All the commands that are executed consecutively, treated as a single unit of work and
termed as a transaction.
● If you want to save all the commands which are executed in a transaction, then
just after completing the transaction, you have to execute the commit command.
This command will save all the commands which are executed on a table. All
these changes made to the table will be saved to the disk permanently.
● Whenever the commit command is executed in SQL, all the updations which we
have carried on the table will be uploaded to the server, and hence our work will
be saved.
● The rollback command is used to get back to the previous permanent status of
the table, which is saved by the commit command.
Q: 9 What are the importance of Primary key and Unique key in database? Explain
with example. [summer - 2021(4 marks)]
A:9
A primary key is a column of table which uniquely identifies each tuple (row) in that
table. Primary key enforces integrity constraints to the table. Only one primary key is
allowed to use in a table. The primary key does not accept the any duplicate and NULL
values. The primary key value in a table changes very rarely so it is chosen with care
where the changes can occur in a seldom manner. A primary key of one table can be
referenced by foreign key of another table.
For better understanding of primary key we take table named as Student table, having
attributes such as Roll_number, Name, Batch, Phone_number, Citizen_ID.
The roll number attribute can never have identical and NULL value, because every
student enrolled in a university can have unique Roll_number, therefore two students
cannot have same Roll_number and each row in a table is uniquely identified with
student’s roll number. So, we can make Roll_number attribute as a primary key in this
case.
Unique key constraints also identifies an individual tuple uniquely in a relation or table.
A table can have more than one unique key unlike primary key. Unique key constraints
can accept only one NULL value for column. Unique constraints are also referenced by
the foreign key of another table. It can be used when someone wants to enforce unique
constraints on a column and a group of columns which is not a primary key.
For better understanding of unique key we take Student table with Roll_number, Name,
Batch, Phone_number, and Citizen_ID attributes.
Roll number attribute is already assigned with the primary key and Citizen_ID can have
unique constraints where each entry in a Citizen_ID column should be unique because
each citizen of a country must have his or her Unique identification number like Aadhaar
Number. But if student is migrated to another country in that case, he or she would not
have any Citizen_ID and the entry could have a NULL value as only one NULL is
allowed in the unique constraint.
-Primary key will not accept NULL values whereas Unique key can accept NULL values.
-A table can have only one primary key whereas there can be multiple unique key on a
table.
-A Clustered index automatically created when a primary key is defined whereas Unique
key generates the non-clustered index.
Q:10 Describe two rules of mandatory access control. [summer - 2021(3 marks)]
A:10
As users navigate through physical and digital systems, they tend to brush up against
resources and assets that they should or should not have access to. This is particularly
true in digital systems where the lateral movement to different storage, application, or
processing areas can lead to dangerous security threats that undermine the entire
infrastructure.
To maintain separation of assets and resources, security administrators use what are
known as “access controls” that define who can access those resources.
Essentially, once a user is authenticated and authorized to enter a system via a user
account or identity, an access control system sets conditions that determine who, when,
where, and sometimes how that user can navigate the system.
While this concept seems simple on its surface, there are several different access
control schemas that help secure resources against unauthorized access:
Some common forms of rule-based access include limiting system access to given
times of the day, or locations (for example, limiting access to devices at or near an office
location).
Q:11 Describe Grant and Revoke commands with suitable example. [summer -
2021(4 marks)]
❖ Explain following SQL commands with syntax and significance of Grant &
Revoke.
A:11
DCL commands are used to enforce database security in a multiple user database
environment. Two types of DCL commands are GRANT and REVOKE. Only Database
Administrator's or owner's of the database object can provide/remove privileges on a
database object.
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
The REVOKE command removes user access rights or privileges to the database
objects.
The SQL Injection is a code penetration technique that might cause loss to our
database. It is one of the most practiced web hacking techniques to place malicious
code in SQL statements, via webpage input. SQL injection can be used to manipulate
the application's web server by malicious users.
SQL injection generally occurs when we ask a user to input their username/userID.
Instead of a name or ID, the user gives us an SQL statement that we will unknowingly
run on our database. For Example - we create a SELECT statement by adding a
variable "demoUserID" to select a string. The variable will be fetched from user input
(getRequestString).
demoUserI = getrequestString("UserId");
SQL injections can do more harm other than passing the login algorithms. Some of the
SQL injection attacks include:
● Updating, deleting, and inserting the data: An attack can modify the cookies to
poison a web application's database query.
● It is executing commands on the server that can download and install malicious
programs such as Trojans.
● We are exporting valuable data such as credit card details, email, and passwords
to the attacker's remote server.
● Getting user login details: It is the simplest form of SQL injection. Web application
typically accepts user input through a form, and the front end passes the user
input to the back end database for processing.
Q:13 Consider the following relations and write SQL queries for given statements.
Assume suitable constraints.
job(job-id, job-title, minimum-salary, maximum-salary)
employee(emp-no, emp-name, emp-salary,dept-no)
deposit(acc-no, cust-name, branch-name, amount, account-date)
borrow(loan-no, cust-name, branch-name, amount)
department (dept-no, dept-name)
(1) Give name of employees whose employee number is '001'
(2) Give name of depositors whose branch name starts from ‘S’.
(3) Give employee name(s) whose salary is between Rs. 20000 to 30000 and
department name is Finance.
(4) Update the salary of employee by 10% of their salary who is working in the
Finance department. [summer - 2021(7 marks)]
A:15
A:16
Super Key – A super key is a group of single or multiple keys which identifies rows in a
table.
Primary Key – is a column or group of columns in a table that uniquely identify every
row in that table.
Candidate Key – is a set of attributes that uniquely identify tuples in a table. Candidate
Key is a super key with no repeated attributes.
Unit - 10: PL/SQL Concepts
Q:1 What is trigger? Explain its type with their syntax. [summer - 2020(3 marks)]
A:1
In SQL Server we can create triggers on DDL statements (like CREATE, ALTER and
DROP) and certain system-defined Stored Procedures that does DDL-like operations.
In SQL Server we can create triggers on DML statements (like INSERT, UPDATE and
DELETE) and Stored Procedures that do DML-like operations. DML Triggers are of two
types.
After trigger (using FOR/AFTER CLAUSE): After triggers are executed after completing
the execution of DML statements.
Example: If you insert a record/row into a table then the trigger related/associated with
the insert event on this table will executed only after inserting the record into that table.
If the record/row insertion fails, SQL Server will not execute the after trigger.
Logon triggers
● This type of trigger is executed against a LOGON event before a user session is
established to the SQL Server.
● We cannot pass parameters into triggers like stored procedure.
● Triggers are normally slow.
● When triggers can be used,
1. Based on change in one table, we want to update other table.
2. Automatically update derived columns whose values change based on other
columns.
3. Logging.
4. Enforce business rules.
Syntax of Trigger
CREATE [OR ALTER] TRIGGER trigger_name
ON table_name
{ FOR | AFTER | INSTEAD OF }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
AS
BEGIN
Executable statements
END;
CREATE [OR ALTER] TRIGGER trigger_name:- This clause creates a trigger with the
given name or overwrites an existing trigger.
[ON table_name]:- This clause identifies the name of the table to which the trigger is
related.
[FOR | AFTER | INSTEAD OF]:- This clause indicates at what time the trigger should
be fired. FOR and AFTER are similar.
[INSERT / UPDATE / DELETE]:- This clause determines on which kind of statement the
trigger should be fired. Either on insert or update or delete or combination of any or all.
More than one statement can be used together separated by Comma. The trigger gets
fired at all the specified triggering event.
Example 1
on Student
AFTER INSERT
Q:2 Write a PL/SQL block to print the given number is odd or even.[summer -
2020(4 marks)]
A:2
DECLARE
n1 NUMBER := &num1;
BEGIN
-- test if the number provided by the user is even
IF MOD(n1,2) = 0 THEN
DBMS_OUTPUT.PUT_LINE ('The number. '||n1||
' is even number');
ELSE
DBMS_OUTPUT.PUT_LINE ('The number '||n1||' is odd number.');
END IF;
DBMS_OUTPUT.PUT_LINE ('Done Successfully');
END;
/
OUTPUT:
A:4
When an SQL statement is processed, Oracle creates a memory area known as context
area. A cursor is a pointer to this context area. It contains all information needed for
processing the statement. In PL/SQL, the context area is controlled by Cursor. A cursor
contains information on a select statement and the rows of data accessed by it.
A cursor is used to referred to a program to fetch and process the rows returned by the
SQL statement, one at a time. There are two types of cursors:
1. Implicit Cursors
2. Explicit Cursors
The implicit cursors are automatically generated by Oracle while an SQL statement is
executed, if you don't use an explicit cursor for the statement.
These are created by default to process the statements when DML statements like
INSERT, UPDATE, DELETE etc. are executed.
Orcale provides some attributes known as Implicit cursor's attributes to check the status
of DML operations. Some of them are: %FOUND, %NOTFOUND, %ROWCOUNT and
%ISOPEN.
The Explicit cursors are defined by the programmers to gain more control over the
context area. These cursors should be defined in the declaration section of the PL/SQL
block. It is created on a SELECT statement which returns more than one row.
Steps:
1. You must follow these steps while working with an explicit cursor.
It defines the cursor with a name and the associated SELECT statement.
CURSOR name IS
SELECT statement;
It is used to allocate memory for the cursor and make it easy to fetch the rows returned
by the SQL statements into it.
OPEN cursor_name;
It is used to release the allocated memory. The following syntax is used to close the
above-opened cursors.
Close cursor_name;
Q:5 Write a PL/SQL block to print the sum of even numbers from 1 to 50. [summer
- 2020(4 marks)]
A:5
Declare
a number;
Begin
a:=1;
loop
sum1:=sum1+a;
a:=a+1;
end loop;
End;
Q:6 Given the following relations
TRAIN (NAME, START, DEST)
TICKET (PNRNO., START, DEST, FARE)
PASSENGER (NAME, ADDRESS, PNRNO.)
Write SQL expressions for the following queries:
Note: Assume NAME of Train is a column of Ticket.
(1) List the names of passengers who are travelling from the start to the
destination station of the train.
(2) List the names of passengers who have a return journey ticket.
(3) Insert a new Shatabti train from Delhi to Bangalore.
(4) Cancel the ticket of Tintin. [summer - 2020(7 marks)]
A: 6
(i) SELECT P.NAME FROM TRAIN T, TICKET I, PASSENGER P
WHERE P.PNRNO = I.PNRNO AND T.NAME = I.NAMEAND T.START = I.START AND
T.DEST = I.DEST
(ii) SELECT NAME FROM PASSENGERWHERE
PNRNO IN (SELECT DISTINCT A.PNRNO
FROM TICKET A, TICKET B WHERE A.PNRNO = B.PNRNO
AND A.START = B.DEST AND A.DEST = B.START)
(iii) INSERT INTO TRAINVALUES(‘Shatabdi’, ‘Delhi’, ‘Banglore’)
(iv) DELETE FROM TICKET
WHERE PNRNO = (SELECT PNRNO FROM PASSENGER
WHERE NAME = ‘Tintin’)