Upload
others
View
5
Download
1
Embed Size (px)
Citation preview
DataBase Management System
RCS-501 (2019-20)
Question bank Solution
COURSE OUTCOMES
By the end of the course, students will be able to:
CO 302.1 Differentiate database systems from file systems by enumerating the features
provided by database systems and describe each in both function and benefit.
CO 302.2 Demonstrate an understanding of the relational data model.
CO 302.3 Describe normalization theory and apply such knowledge to the normalization of a
database.
CO 302.4 Select different queries to deal with Online transactions concepts.
CO 302.5 Identify the concept of Concurrency Control and the methods to deal with Data
Base Failures and Recovery Mechanism.
UNIT-I Q1. Explain the difference between file systems, DBMS and RDBMS in detail.
Ans. Difference between DBMS and RDBMS Although DBMS and RDBMS both are used to store information in physical database but
there are some remarkable differences between them. The main differences between DBMS and RDBMS are given below:
No. DBMS RDBMS
1) DBMS applications store data as file. RDBMS applications store data in a tabular
form.
2) In DBMS, data is generally stored in either In RDBMS, the tables have an identifier
a hierarchical form or a navigational form. called primary key and the data values are
stored in the form of tables.
3) Normalization is not present in DBMS. Normalization is present in RDBMS.
4)
DBMS does not apply any security with
RDBMS defines the integrity constraint for
regards to data manipulation. the purpose of ACID (Atomocity,
Consistency, Isolation and Durability)
property.
5) DBMS uses file system to store data, so In RDBMS, data values are stored in the
there will be no relation between the form of tables, so a relationship between
tables. these data values will be stored in the form of
a table as well.
6) DBMS has to provide some uniform RDBMS system supports a tabular structure
methods to access the stored information. of the data and a relationship between them
to access the stored information.
7) DBMS does not support distributed RDBMS supports distributed database.
database.
8) DBMS is meant to be for small RDBMS is designed to handle large amount
organization and deal with small data. it of data. it supports multiple users.
supports single user.
9) Examples of DBMS are file Example of RDBMS are mysql, postgre, sql
systems, xml etc. server, oracle etc.
Q2. What is data Model? List the types of data model used.
Ans. DATA MODELS
A Data Model defines the underlying structure of a Database. It comprises a collection of
conceptual tools for describing the Data, the Data Relationships, the Data Semantics and the
Data Integrity Constraints.
CATEGORIES OF DATA MODELS
Basically, there are three categories of data models:
(a) Object Based Logical Models. (b) Record Based Logical Models.
(a) Object Based Logical Models: The Object Based Logical Models view the universe as a
collection of objects.
(i) Entity-Relationship Model.
- An Entity refers to a real-world ‗object‘ or a ‗concept‘ that is distinguishable from other
objects and other concepts in the real-world. For example, a person, a bank-account, a
payment are all entities of different kinds. - An Entity will have a set of properties, known as Attributes; for example, the Entity ―Account‖ may have attributes like ―Account-Number‖, ―CurrentBalance‖ etc - Each attribute will have a set of permitted values, called its Domain; for example the
domain of Balance of an account can be the set of +ve real numbers. - A collection of entities of the same kind, having same set of attributes, is called an ―Entity
Set‖.
- A relationship refers to the association amongst entities. For example, in a banking
database, an entity ‗Customer‘ can have relationship ‗Depositor‘ with another entity
‗Account‘. - A set of Relationships of the same kind, having the same set of attributes is called a
Relationship Set. - A database in E-R Model is modeled as a collection of Entity Sets and Relationships Sets. - E-R Model also specifies certain constraints, like Mapping Cardinalities i.e. whether the
relationship is one-to-one, one-to-many, many-to-one or many-to-many. - The E-R Diagram below depicts two Entity Sets ―STUDENT‖, ―COURSE‖ and a
relationship set ―RESULT‖ indicating the marks obtained by students in different Courses.
(ii) Object-Oriented Model: Like the E-R Model, this model also models a database as a
Collection of Objects. An Object Body encapsulates Data (Variables) as well as Methods
(Functions) to manipulate the Data (Variables). The Objects that contain same Type of Data
Variables and same Type of Functions are grouped together as a Class. Thus, a Class may be
viewed as a Type Definition of the Objects. The only way an Object ―A‖ can access the Data
Items of another Object ―B‖ is by invoking the Methods of ―B‖. ―A‖ can accomplish this
by making calls to the methods of ―B‖, through B‘s Interface. The methods defined within
an object are made visible to the external world, through its Interface.
The structure of an object-oriented database is modeled as a set of classes and database will
comprise of objects belonging to those classes.
(b) Record Based Logical Models. These models describe data at the Logical level, as a
collection of fixed-format Records of different types. Each Record Type can have a fixed
number of Fields (or Attributes) and each Field is usually of fixed length. Use of fixed-length
Records simplifies the Physical Level implementation of a database. The most widely used
Record Based Logical Models are:
(i) Hierarchical Model. (ii) Network Model. (iii) Relational Model.
Q3. Give the example of one to one and one to many relationship.
Ans.
Q4. Give example of a simple and composite attributes of an entity.
Ans. Simple Vs Composite Attributes: A Simple attribute is the one, which is not divisible
into sub-parts like ‗BRANCH‘. On the other hand, a Composite attribute is the one, which
can be divided into sub-parts like ‗DATE-OF-BIRTH‘, which may be divided into ‗birth-
date‘, ‗birth-month‘ & ‗birth-year‘.
Q5. What do you mean by DDL and DML?
Ans. DATABASE LANGUAGES
A DBMS will support two kinds of languages; one called Data Definition Language (DDL)
to specify the Database Schema and the other called Data Manipulation Language (DML) to
enable accessing and manipulation of the data stored in the database. (a) DDL. A database schema is specified by a set of definitions expressed in DDL.
In a Relational Database, the result of interpretation of DDL statements will be a set of
Tables that are stored in a special file called Data Dictionary or Data Directory or DBMS
Catalogue. This data stored in Data Dictionary is called Metadata i.e. data about data.
Whenever the database is to be accessed, the DBMS will first make a reference to the Data
Dictionary with a view to determine the structure of data to be accessed; only then it will
access the actual data in the database. Thus the data dictionary is accessed during processing
of each query. The storage structure and access methods used by the database system are
specified by a set of definitions in a special type of DDL called Data Storage and Definition
Language. The result of interpretation of these definitions will be a set of physical schema
structures and a set of access methods supported by the system. These details are usually
hidden from the database-users. (b) DML. A DML is a language that enables users to access and manipulate the data
stored in the database. A DML query is a statement specifying information to be accessed for
retrieval or insert/update/delete. The portion of a DML that involves information retrieval is
called a query language. The goal of a DML is to provide an efficient and friendly human
interface for the following operations in a database:- (i) Retrieval on information stored in the database. (ii) Insertion of new information into the database. (iii) Deletion of information from the database.
(iv)Update of information stored in the
database. There are two types of DMLs:- (i) Procedural DMLs. A query in procedural DML requires the user to specify not only ―what data is required to be extracted from the database‖ but
also to specify ―how to extract those data‖.
(ii) Non-Procedural DMLs. A Query in Non-Procedural DML requires
the user to specify only ―what data is needed‖, without specifying how to get
those data. Non-procedural DMLs are easier to learn and to use than the procedural DMLs.
However, since non-Procedural DMLs do not specify ―how to get the data‖, the queries in
Non-Procedural DMLs may not generate as efficient code as the
equivalent queries in Procedural DMLs. This limitation of Non-Procedural
DMLs is overcome by performing query optimization at the System Level.
Q6. What do you mean by referential Integrity?
Ans. Referential integrity requires that a foreign key must have a matching primary key or it
must be null. This constraint is specified between two tables (parent and child); it maintains
the correspondence between rows in these tables. It means the reference from a row in one
table to another table must be valid.
Examples of referential integrity constraint in the Customer/Order database of the
Company:
• Customer(CustID, CustName)
• Order(OrderID, CustID, OrderDate)
To ensure that there are no orphan records, we need to enforce referential integrity. An
orphan record is one whose foreign key FK value is not found in the
corresponding entity – the entity where the PK is located. Recall that a typical join is between
a PK and FK.
The referential integrity constraint states that the customer ID (CustID) in the Order table
must match a valid CustID in the Customer table. Most relational databases have declarative
referential integrity. In other words, when the tables are created the referential integrity
constraints are set up.
Here is another example from a Course/Class database:
• Course(CrsCode, DeptCode, Description)
• Class(CrsCode, Section, ClassTime)
The referential integrity constraint states that CrsCode in the Class table must match a valid
CrsCode in the Course table. In this situation, it‘s not enough that the CrsCode and Section in
the Class table make up the PK, we must also enforce referential integrity.
When setting up referential integrity it is important that the PK and FK have the same data
types and come from the same domain, otherwise the relational database management system
(RDBMS) will not allow the join. RDBMS is a popular database system that is based on the
relational model introduced by E. F. Codd of IBM‘s San Jose Research Laboratory.
Relational database systems are easier to use and understand than other database systems.
Q7. Explain the difference between a weak and strong entity set with example.
Ans. An Entity Set is said to be a Weak Entity Set if it does not have sufficient attributes to
form its Primary Key. On the other hand, an entity set having a primary key of its own is
called a Strong Entity Set. A Weak Entity Set (say E2) will be dependent for its existence on
a Strong Entity Set (say E1) to form its Candidate Key. Then Entity Set E2 is said to be
―Existence-Dependent‖ on E1 and E1 is said to be the ―Owner Entity Set‖ of E2. The
relationship R between E2 and E1 is called ―Identifying Relationship‖. The Weak Entity Set
E2 will have a set of attributes called its ―Discriminator‖, which together with the Primary
Key of E1 will form the Primary Key of E2.
Example:-Suppose an Entity Set EMPLOYEE (EMP_ID, EMP_NAME, SALARY,
DEPENDENTS) has an attribute DEPENDENT which is multi-valued i.e. an employee may
have none or one or more than dependents. This situation can be best modeled as follows:-
- The Weak Entity Set ―DEPENDENT‖ has a Discriminator Attribute D-NAME,
which along with primary key EMP-ID of EMPLOYEE, forms Primary Key of the weak
entity set DEPENDENT. In E-R Diagram, the Discriminator (also called Partial Key) of a
weak entity set is marked by underlining with a broken line.
Q8. Discuss three level of abstraction or schemas architecture of DBMS.
Ans. DATA ABSTRACTION & VARIOUS SCHEMAS OF A DATABASE
There are three levels of data abstraction in a database; and each level is described by a
schema as explained below:- (a) Physical Level. This is the lowest level of abstraction. At this level, a
Physical Schema describes ―how data is physically stored‖. The Physical Schema may
describe complex structures, used to store the data, with the sole aim of achieving an efficient
access of the data.
(b) Logical Level. This is the intermediate level of abstraction. At this level, a Logical Schema (or Conceptual Schema) would describe ―what data is stored in the database‖ and
―what are the relationships amongst the data‖. This Schema is used by Database Administrators, who decide what information is to be kept in the Database. It would describe
the logical structure of database, data types and integrity constraints. As compared to
Physical Level, Database at Logical Level is described by relatively smaller number of
simpler structures. But, the implementation of these simple structures may be quite complex
at the Physical Level. The user operating at Logical Level need not be aware of the
complexities at the Physical Level. (c) View Level. This is the highest level of abstraction. At this level, there will be many
Views, defined for different categories of users. A View for a certain group of users describes ―what subset of the database is to be made visible‖ to that group. A view will describe only a
subset of the underlying database. This is the subset, which the intended group of users needs to
access. There may be many Views, tailored to the specific needs of various users. At the view
level, the main goal is to provide an efficient and a user-friendly human-interaction with the
system. So, the interface at this level is made as simple and user-friendly as possible. A user
doesn‘t have to be aware of the complexities at the conceptual level and physical level.
Q9. Draw overall structure of a DBMS and explain its component in brief.
Ans. Database systems are partitioned into modules for different functions. Some functions
(e.g. file systems) may be provided by the operating system.
1. Components include: o File manager manages allocation of disk space and data structures used to
represent information on disk. o Database manager: The interface between low-level data and
application programs and queries. o Query processor translates statements in a query language into low-level
instructions the database manager understands. (May also attempt to find an equivalent but more efficient form.)
o DML precompiler converts DML statements embedded in an application program to normal procedure calls in a host language. The precompiler interacts with the query processor.
o DDL compiler converts DDL statements to a set of tables containing metadata stored in a data dictionary.
In addition, several data structures are required for physical system implementation:
o Data files: store the database itself. o Data dictionary: stores information about the structure of the database. It is
used heavily. Great emphasis should be placed on developing a good design and efficient implementation of the dictionary.
o Indices: provide fast access to data items holding particular values.
Q10. Explain the three level representation of data. What is data independence?
Explain the Various types of data independence in detail.
Ans. The details of these levels are as follows:
Physical Level
This is the lowest level in the three level architecture. It is also known as the internal level.
The physical level describes how data is actually stored in the database. In the lowest level,
this data is stored in the external hard drives in the form of bits and at a little high level, it can
be said that the data is stored in files and folders. The physical level also discusses
compression and encryption techniques.
Conceptual Level
The conceptual level is at a higher level than the physical level. It is also known as the logical
level. It describes how the database appears to the users conceptually and the relationships
between various data tables. The conceptual level does not care for how the data in the
database is actually stored.
External Level
This is the highest level in the three level architecture and closest to the user. It is also known
as the view level. The external level only shows the relevant database content to the users in
the form of views and hides the rest of the data. So different users can see the database as a
different view as per their individual requirements.
DATA INDEPENDENCE
The ability of a DBMS to modify its Schema definition at one level, without affecting
a Schema definition at the next higher level, is called Data Independence. There are
two levels of Data Independence:-
(a) Physical Data Independence. It is the ability of DBMS to modify the Physical Schema
without causing any changes in the schema at the logical level and at the view level.
Modifications at Physical Level are driven by advancements in hardware technology and by
the requirements to upgrade hardware for improving system performance. (b) Logical Data Independence.This refers to the ability of DBMS to modify the Logical
Schema without causing any changes in the application programs at the view level.
Modifications at Logical Level are necessitated by need to alter the Logical Structure of the
database. The Logical Data Independence is much more difficult to achieve than the Physical
Data independence, since the application programs are heavily dependent on the logical
structure of the database. Q11. What is aggregation? Give an ER diagram as example to show the meaning of
aggregation and also show how aggregation is converted into tables.
Ans. Aggregation is a process in which a single entity alone is not able to make sense in a
relationship so the relationship of two entities acts as one entity. I know it sounds confusing
but don‘t worry the example we will take, will clear all the doubts.
Aggregration Example
If we look at STUDENT and COURSE from SUBJECT‘s point of view, it does not
differentiate both of them. It offers it‘s subject to both of them. So what can we do here is,
merge STUDENT and COURSE as one entity. This process of merging is called aggregation.
It is completely different from generalization. In generalization, we merge entities of same
domain into one entity. In this case we merge related entities into one entity.
Here we have merged STUDENT and COURSE into one entity STUDENT_COURSE. This
new entity forms the mapping with SUBJECTS. The new entity STUDENT_COURSE, in
turn has two entities STUDENT and COURSE with ‗Attends‘ relationship.
Q12. Discuss the role of database administrator.
Ans. Functions of a Database Administrator (DBA)
DBA is the custodian of the Database System placed under his control and is responsible for
the following functions:-
1. Creation of Conceptual Schema and its periodic update to adapt to the changed
requirements. 2. Implementation of efficient Storage Structure and Access Methods.
3. Liaise with the Users to ensure that the information required by the Users is made
available. 4. Ensure system security, through Grant and Revoke of Access Rights to the Users. A user
must have only as much rights as required by his role in the organization- nothing more,
nothing less. 5. To ensure Physical Security of Database against malicious access and accidents like fire
etc. 6. Take periodic backups and keep the archived data safely. 7. Execute immediate recovery procedures in case of failures.
8. Monitor the system performance. In case of degradation in system performance, perform
tuning procedures. If necessary, upgrade the system (hardware / software) to meet the
changed requirements of the organization. 9. Ensure sufficient Disk Space is always available. If needed, upgrade the Disk Drives to
meet the increased requirements. 10. To liaise with the DBMS vendor to obtain necessary technical supports and to obtain the
necessary tools & software upgrades, whenever made available by the vendor.
Q14. What do you mean by key to a relation? Explain the Primary Key, Super Key,
Foreign Key and Candidate key with example.
Ans. Key is an attribute or set of an attribute which helps you to identify a row(tuple) in a
relation(table). They allow you to find the relation between two tables. Keys help you
uniquely identify a row in a table by a combination of one or more columns in that table. Super Key: A Super Key of an Entity Set or Relationship Set refers to the set of attributes, which
when taken collectively, will uniquely determine an entity within the Entity Set or a Relationship
within the Relationship Set. If K forms a Super Key (SK) of an Entity Set E then any super set of
K will also be a Super Key of E. So, a Super Key may have some extraneous (unnecessary)
attributes, which if removed, the balance set may still form a Super Key of R. Example :- Suppose each student in the Entity Set STUDENT (ROLL_NO, NAME,
BRANCH, FATHERS-NAME, ADDRESS, DOB, TEL-NO) has a unique value of
ROLL-NO. This implies that no two students can have same ROLL-NO. Then
{ROLL-NO, NAME} forms a super key of Entity-Set STUDENT. In this, the
attribute NAME is extraneous; which if removed, the balance set i.e. {ROLL-NO}
still forms a Super Key of STUDENT. Candidate Key: A Super Key, whose no proper subset forms a Super Key, is called
a Candidate Key. Thus, Candidate Key is a minimal Super Key (i.e. a Super Key
having no extraneous attributes). An Entity Set may have more than one Candidate
Keys. Example:- The Entity Set STUDENT will have at least two Candidate Keys i.e.
{ROLL-NO} and {NAME, FATHERS-NAME, DOB, ADDRESS}. Primary Key: Primary Key is one of the Candidate Keys that is designated by the
database designers as primary means of identifying entities within an entity set. In the
E-R Diagram, the Primary Key Attributes are underlined with a firm line.
Q15. Draw an ER diagram for small marketing company database assuming your own
data requirement.
Solution:
Q16. Design a generalization specialization hierarchy for a motor vehicle sale company
which sells motorcycles, passenger cars vans and buses. Justify your placement of
attributes at each level of hierarchy. Explain why they should not be placed at higher or
lower level.
Solution:
Q17. Construct an E-R diagram for a hospital with a set of patients and a set of medical
doctors. Associate with each patient a log of the various tests and examinations
conducted
Solution:
Q18. A university registrar‟s office maintains data about the following entities:
(a) courses, including number, title, credits, syllabus, and prerequisites; (b) course offerings,includingcoursenumber,year,semester,sectionnumber,instr uctor(s),
timings, and classroom; (c) students, including student-id, name, and program; and (d) instructors, including identification number, name, department, and title. Further,
the enrollment of students in courses and grades awarded to students in each course
they are enrolled for must be appropriately modeled. Construct an E-R diagram for the
registrar‟s office. Document all assumptions that you make about the mapping
constraints.
Solution:
Q19. Explain the difference between physical level and logical level data independence
with example.
Ans. Difference between Physical and Logical Data Independence
Logica Data Independence Physical Data Independence
Logical Data Independence is mainly concerned
with the structure or changing the data
definition.
Mainly concerned with the storage of the
data.
It is difficult as the retrieving of data is mainly It is easy to retrieve. dependent on the logical structure of data.
Compared to Logic Physical independence it is
difficult to achieve logical data independence.
Compared to Logical Independence it is easy
to achieve physical data independence.
You need to make changes in the Application
program if new fields are added or deleted from
the database.
A change in the physical level usually does
not need change at the Application program
level.
Modification at the logical levels is significant
whenever the logical structures of the database
are changed.
Modifications made at the internal levels may
or may not be needed to improve the
performance of the structure.
Concerned with conceptual schema Concerned with internal schema
Example: Add/Modify/Delete a new attribute Example: change in compression techniques,
hashing algorithms, storage devices, etc
Examples of changes under Physical Data Independence
Due to Physical independence, any of the below change will not affect the conceptual layer.
• Using a new storage device like Hard Drive or Magnetic Tapes
• Modifying the file organization technique in the Database
• Switching to different data structures.
• Changing the access method.
• Modifying indexes.
• Changes to compression techniques or hashing algorithms.
• Change of Location of Database from say C drive to D Drive
Examples of changes under Logical Data Independence
Due to Logical independence, any of the below change will not affect the external layer.
1. Add/Modify/Delete a new attribute, entity or relationship is possible without a rewrite
of existing application programs 2. Merging two records into one 3. Breaking an existing record into two or more records
Q20. Consider a database used to record the marks that students get in different exams
of different course offerings. Construct an E-R diagram that uses only a binary
relationship between students and course-offerings. Make sure that only one
relationship exists between a particular student and course-offering pair, yet you can
represent the marks that a student gets in different exams of a course offering.
Solution:
UNIT-II
Q21. Differentiate between DDL and DML command.
Ans. Data Definition Language (DDL) It is used for defining the database schema i.e. to
CREATE, ALATER & DROP Tables. Some other commands are Truncate and Rename.
Data Manipulation Language (DML) The DML commands are used to manipulate the
information in the tables; like INSERT, UPDATE or DELETE commands
Q22. What is Foreign key?
Ans. Foreign Key A relation schema R may include among its attributes some primary key of
another schema S. This is called a Foreign Key in schema R.
For example, {Univ_Roll_No} forms a Primary Key in STUDENT but it forms Foreign Key
in RESULT. Similarly {Sub_Code} forms a Primary Key of SUBJECT but a Foreign Key in
RESULT.
Q23. With an example show how the referential integrity can be implemented.
Ans. Referential integrity (RI) is a relational database concept, which states that table
relationships must always be consistent. In other words, any foreign key field must agree
with the primary key that is referenced by the foreign key. Thus, any primary key field
changes must be applied to all foreign keys, or not at all. The same restriction also applies to
foreign keys in that any updates (but not necessarily deletions) must be propagated to the
primary parent key.
Consider a bank database, which contains two tables:
• CUSTOMER_MASTER Table: This holds basic customer/account holder data such as name, social security number, address and date of birth.
• ACCOUNTS_MASTER Table: This stores basic bank account data such as account type, account creation date, account holder and withdrawal limits.
To uniquely identify each customer/account holder in the CUSTOMER_MASTER table, a primary key column named CUSTOMER_ID is created.
To identify a customer and bank account relationship in the ACCOUNTS_MASTER table,
an existing customer in the CUSTOMER_MASTER table must be referenced. Thus, the
CUSTOMER_ID column – also created in the ACCOUNTS_MASTER table – is a foreign
key. This column is special because its values are not newly created. Rather, these values
must reference existing and identical values in the primary key column of another table,
which is the CUSTOMER_ID column of the CUSTOMER_MASTER table.
Referential integrity is a standard that means any CUSTOMER_ID value in the
CUSTOMER_MASTER table may not be edited without editing the corresponding value in
the ACCOUNTS_MASTER table. For example, if Andrew Smith‘s customer ID is changed
in the CUSTOMER_MASTER table, this change also must be applied to the
ACCOUNTS_MASTER table, thus allowing Andrew Smith‘s account information to link to
his customer ID.
Q24. What do you mean by Cursor?
Ans. A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds the rows (one or more) returned by a SQL statement. The set of rows the cursor holds is referred to as the active set.
You can name a cursor so that it could be referred to in a program to fetch and process the rows returned by the SQL statement, one at a time. There are two types of cursors −
• Implicit cursors
• Explicit cursors
Q25. Discuss the concept of Trigger with a suitable example? Also differentiate between
Views and Indexe.
Ans. Trigger: A trigger is a stored procedure in database which automatically invokes
whenever a special event in the database occurs. For example, a trigger can be invoked when a
row is inserted into a specified table or when certain table columns are being updated.
Syntax: create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Example:
create trigger stud_marks
before INSERT
on
Student
for each row
set Student.total = Student.subj1 + Student.subj2 + Student.subj3, Student.per = Student.total * 60 / 100;
VIEW • View is a logical table. It is a physical object which stores data logically. View just refers
to data that is tored in base tables.
• A view is a logical entity. It is a SQL statement stored in the database in the system tablespace. Data for a view is built in a table created by the database engine in the TEMP tablespace.
INDEX • Indexes are pointres that maps to the physical address of data. So by using indexes
data manipulation becomes faster.
• An index is a performance-tuning method of allowing faster retrieval of records. An index creates an entry for each value that appears in the indexed columns.
Q26. What is Trigger? Explain different trigger with example.
Ans. A MySQL trigger is a stored program (with queries) which is executed automatically to respond to a specific event such as insertion, updation or deletion occurring in a table. There are 6 different types of triggers in MySQL:
1. Before Update Trigger: As the name implies, it is a trigger which enacts before an update is invoked. If we write an update statement, then the actions of the trigger will be performed before the update is implemented. Example:
Considering tables:
create table customer (acc_no integer primary key,
cust_name varchar(20),
avail_balance decimal);
create table mini_statement (acc_no integer,
avail_balance decimal,
foreign key(acc_no) references customer(acc_no) on delete cascade);
2. After Update Trigger: As the name implies, this trigger is invoked after an updation occurs. (i.e., it gets implemented after an update statement is executed.). Example:
We create another table: create table micro_statement (acc_no integer,
avail_balance decimal,
foreign key(acc_no) references customer(acc_no) on delete cascade);
3. Before Insert Trigger: As the name implies, this trigger is invoked before an insert, or before an insert statement is executed. Example:
Considering tables: create table contacts (contact_id INT (11) NOT NULL AUTO_INCREMENT,
last_name VARCHAR (30) NOT NULL, first_name VARCHAR (25),
->birthday DATE, created_date DATE,
created_by VARCHAR(30),
CONSTRAINT contacts_pk PRIMARY KEY (contact_id));
4. After Insert Trigger:
As the name implies, this trigger gets invoked after an insert is implemented. Example:
Consider tables:
create table contacts (contact_id int (11) NOT NULL AUTO_INCREMENT,
last_name VARCHAR(30) NOT NULL,
first_name VARCHAR(25), birthday DATE,
->CONSTRAINT contacts_pk PRIMARY KEY (contact_id));
create table contacts_audit (contact_id integer,
created_date date,
created_by varchar (30));
5. Before Delete Trigger: As the name implies, this trigger is invoked before a delete occurs, or before deletion statement is implemented. Example:
Consider tables: create table contacts (contact_id int (11) NOT NULL AUTO_INCREMENT,
last_name VARCHAR (30) NOT NULL, first_name VARCHAR (25),
birthday DATE, created_date DATE, created_by VARCHAR(30),
CONSTRAINT contacts_pk PRIMARY KEY (contact_id));
create table contacts_audit (contact_id integer, deleted_date date, deleted_by varchar(20));
6. After Delete Trigger: As the name implies, this trigger is invoked after a delete occurs, or after a delete operation is implemented. Example:
Consider the tables: create table contacts (contact_id int (11) NOT NULL AUTO_INCREMENT,
last_name VARCHAR (30) NOT NULL, first_name VARCHAR (25),
birthday DATE, created_date DATE, created_by VARCHAR (30),
CONSTRAINT contacts_pk PRIMARY KEY (contact_id));
create table contacts_audit (contact_id integer, deleted_date date, deleted_by varchar(20));
Q27. Explain Tuple calculus and Domain calculus.
Ans. Relational Calculus
o Relational calculus is a non-procedural query language. In the non-procedural query
language, the user is concerned with the details of how to obtain the end results.
o The relational calculus tells what to do but never explains how to do.
Types of Relational calculus:
1. Tuple Relational Calculus (TRC)
o The tuple relational calculus is specified to select the tuples in a relation. In
TRC, filtering variable uses the tuples of a relation.
o The result of the relation can have one or more tuples.
Notation:
{T | P (T)} or {T | Condition (T)}
Where
T is the resulting tuples
P(T) is the condition used to fetch T.
For example:
{ T.name | Author(T) AND T.article = 'database' }
OUTPUT: This query selects the tuples from the AUTHOR relation. It returns a tuple with
'name' from Author who has written an article on 'database'. TRC (tuple relation calculus) can be quantified. In TRC, we can use Existential (∃) and Universal Quantifiers (∀).
For example: { R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)} Output: This query will yield the same result as the previous one.
2. Domain Relational Calculus (DRC)
o The second form of relation is known as Domain relational calculus. In domain
relational calculus, filtering variable uses the domain of attributes.
o Domain relational calculus uses the same operators as tuple calculus. It uses logical connectives ∧ (and), ∨ (or) and ┓ (not).
o It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
Notation:
{ a1, a2, a3, ..., an | P (a1, a2, a3, ...
,an)} Where
a1, a2 are attributes
P stands for formula built by inner attributes
For example: {< article, page, subject > | ∈ javatpoint ∧ subject = 'database'}
Output: This query will yield the article, page, and subject from the relational javatpoint, where the subject is a database.
Q28. Differentiate between SQL and PL/SQL?
Ans.
BASIS FOR
SQL
PL/SQL
COMPARISON
Basic In SQL you can execute a single In PL/SQL you can execute a block of
query or a command at a time. code at a time.
Full form
Structured Query Language
Procedural Language, extension of SQL.
Purpose It is like a source of data that is to It is language that creates an application
be displayed. that display's the data acquired by SQL.
Writes In SQL you can write queries and In PL/SQL you can write block of code
command using DDL, DML that has procedures, functions, packages or
statements. variables, etc.
Use Using SQL, you can retrieve,
modify, add, delete, or manipulate
the data in the database.
Using PL/SQL, you can create
applications or server pages that display's
the information obtained from SQL in a
proper format.
Embed
You can embed SQL statement in
You can not embed PL/SQL in SQL
PL/SQL.
Q29. Consider the following Scheme: SUPPLIER (SUPPLIER ID,
SUPPLIER_NAME, SUPPLIER_ADDRESS) PARTS (PART ID, PART_NAME,
COLOR) CATALOG (SUPPLIER ID, PART ID, COST) Write the following queries in
Relational Algebra and in SQL:
(i)Find the name of the suppliers who supply Black Parts.
(ii) Find the name of suppliers who supply both Blue and Black Parts.
(iii) Find the name of suppliers who supply all Parts.
Solution:
Relation Algebra:
SQL:
Q30. What are the relational algebra operations supported in SQL? Write the SQL
statements for each operation?
Ans. Relational algebra is a widely used procedural query language. It collects instances of relations as input and gives occurrences of relations as output. It uses various operation to perform this action. Relational algebra operations are performed recursively on a relation. The output of these operations is a new relation, which might be formed from one or more input relations.
Operation Purpose
Select(σ) The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition
Projection(π)
The projection eliminates all attributes of the input relation but those
mentioned in the projection list.
Union Operation(∪) UNION is symbolized by symbol. It includes all tuples that are in
tables A or in B.
Set Difference(-)
- Symbol denotes it. The result of A - B, is a relation which includes all
tuples that are in A but not in B.
Intersection(∩) Intersection defines a relation consisting of a set of all tuple that are in
both A and B.
Cartesian Product(X) Cartesian operation is helpful to merge columns from two relations.
Inner Join Inner join, includes only those tuples that satisfy the matching criteria.
Theta Join(θ) The general case of JOIN operation is called a Theta join. It is denoted
by symbol θ.
EQUI Join When a theta join uses only equivalence condition, it becomes a equi
join.
Natural Join(⋈)
Natural join can only be performed if there is a common attribute
(column) between the relations.
Outer Join In an outer join, along with tuples that satisfy the matching criteria.
Left Outer Join( )
In the left outer join, operation allows keeping all tuple in the left
relation.
Right Outer join( ) In the right outer join, operation allows keeping all tuple in the right relation.
Full Outer Join( ) In a full outer join, all tuples from both relations are included in the
result irrespective of the matching condition.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as input
and yields instances of relations as output. It uses operators to perform queries. An operator can be either unary or binary. They accept relations as their input and yield relations as
their output. Relational algebra is performed recursively on a relation and intermediate results are also considered relations.
The fundamental operations of relational algebra are as follows −
• Select
• Project
• Union
• Set different
• Cartesian product
• Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic formula which may use connectors like and, or, and not. These terms may use relational operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those
books published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books. Union Operation (∪)
It performs binary union between two given relations and is defined as − r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
• r, and s must have the same number of attributes.
• Attribute domains must be compatible.
• Duplicate tuples are automatically eliminated. ∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s Where r and s are relations and their output will be defined as − r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename operation allows us to rename the output relation. 'rename' operation is denoted with small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
• Set intersection
• Assignment
• Natural join
Q31. What do you understand by Entity Integrity and Referential Integrity?
Ans. Entity Integrity: It says that no component of a primary key may be null.
All entities must be distinguishable. That is, they must have a unique identification of some
kind. Primary keys perform unique identification function in a relational database. An
identifier that was wholly null would be a contradiction in terms. It would be like there was
some entity that did not have any unique identification. That is, it was not distinguishable
from other entities. If two entities are not distinguishable from each other, then by
definition there are not two entities but only one.
Referential Integrity: Referential integrity is a database concept that ensures that
relationships between tables remain consistent. When one table has a foreign key to another
table, the concept of referential integrity states that you may not add a record to the table
that contains the foreign key unless there is a corresponding record in the linked table. It
also includes the techniques known as cascading update and cascading delete, which ensure
that changes made to the linked table are reflected in the primary table.
Consider the situation where we have two tables: Employees and Managers. The Employees
table has a foreign key attribute entitled ManagedBy which points to the record for that
employee‘s manager in the Managers table. Referential integrity enforces the following
three rules:
1. We may not add a record to the Employees table unless the ManagedBy attribute points to a valid record in the Managers table.
2. If the primary key for a record in the Managers table changes, all corresponding records in the Employees table must be modified using a cascading update.
3. If a record in the Managers table is deleted, all corresponding records in the Employees table must be deleted using a cascading delete
Q32. Explain Embedded SQL and Dynamic SQl in detail.
Ans. Embedded SQL: This is a method for combining data manipulation capabilities of SQL
and computing power of any programming language. Then embedded statements are in line
with the program source code of the host language. The code of embedded SQL is parsed by
a preprocessor which is also embedded and is replaced by the host language called for the
code library it is then compiled via the compiler of the host.
Two steps which define by SQL standards community, they are – Module language defining which is formalization and then an embedded SQL standard
derived from the module language. Most popular hosting language is C, it is called for example Pro*C in Oracle and Sybase database management systems and ECPG in the
PostgreSQL database management system. Need for Embedded SQL in DBMS When you embed SQL with another language. The language that is embedded is known as host language and the SQL standard which defines the embedding of SQL is known as embedded SQL. • The result of a query is made available to the program which is embedded as one tuple
or record at a time • For identification of this, we request to the preprocessor via EXEC SQL statement:
EXEC SQL embedded SQL statement END-EXEC • Its statements are declare cursor, fetch and open statements.
• It can execute the update, insert a delete statement Systems that Support Embedded SQL
i. Altibase
ii. Microsoft SQL Server
iii. Oracle Database
Dynamic SQL: explanations are put away a series of characters that are entered when the
program runs. They can be entered by the developer or created by the program itself.
However, not at all like static SQL explanations, they are not inserted in the source program. Likewise, as opposed to static SQL statements, dynamic SQL clause can change starting with
one execution then onto the next. Dynamic in SQL clause we can compose by individuals with nearly small programming
knowledge, in light of the fact that the program does the greater part of the genuine age of the code. A potential issue decreases the execution (expanded preparing time) if there is
excessively dynamic in SQL running at any given time. Using dynamic SQL is to execute SQL that will conceivably create and execute another
SQL articulation. While questioning information, you may need to dynamically set segments
you might want to inquiry. Then again, you may need to parameterize tables on which you need to work. The main point one may think of is to use factors and set them as required segment names or table names. Be that as it may, such an approach isn‘t upheld by T-SQL.
1. DECLARE @tablename AS NVARCHAR(255) = N'dbo.Table';
2. SELECT *
3. FROM @tablename 4. -- this code will fail
• Dynamic SQL Example –
1. Table name (FROM clause). 2. Database name (USE clause). 3. Column names (SELECT, WHERE, GROUP BY, HAVING, and ORDER
BY clauses). 4. Lists (IN, PIVOT clauses).
Use of Dynamic Structured Query Language
Dynamic SQL is an upgraded type of Structured Query Language (SQL) that not at all like
standard (or static) SQL, encourages the programmed age and execution of program explanations. This can be useful when it is important to compose code that can change in
accordance with fluctuating databases, conditions, or servers. It likewise makes it simpler to robotize errands that rehashes commonly.
Q33. What is Join? Write difference between Natural Join, left outer join and right
outer join with suitable example.
Ans. SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to combine two or more tables". In SQL, JOIN clause is used to combine the records from two or more tables in a database.
Types of SQL JOIN
1. INNER JOIN 2. LEFT JOIN
3. RIGHT JOIN 4. FULL JOIN
Sample Table
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36
6
Marry
Canada
600000
48
PROJECT
PROJECT_NO EMP_ID DEPARTMENT
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as long as the condition is satisfied. It returns the combination of all rows from both the tables where the condition satisfies.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
INNER JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table and the matching values from the right table. If there is no matching join value, it will return NULL.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
LEFT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME
DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and the matched values from the left table. If there is no matching in both tables, it will return NULL. Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
RIGHT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join tables
have all the records from both tables. It puts NULL on the place of matches not found.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
FULL JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME
DEPARTMENT
Angelina Testing
Robert
Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
Q34. Consider the relational database. Give an expression in the relational algebra for
each request: employee (person-name, street, city) , manages (person-name, manager-
name) works (person-name, company-name, salary) , company(company-name, city) a.
Find the names of all employees who work for First Bank Corporation.
b. Find the names and cities of residence of all employees who work for Firs Bank
Corporation
c. Find the names, street address, and cities of residence of all employees who work for
First Bank Corporation and earn more than $10,000per annum.
d. Find the names of all employees in this database who live in the same city as the
company for which they work.
e. Find the names of all employees who live in the same city and on the same street as do
their managers.
Solution:
a. Πperson-name (σcompany-name = ―FirstBank Corporation‖ (works)) b. Πperson-name,city(employee⋈ (σcompany-name = ―First
BankCorporation‖ (works))) c. Πperson-name, street, city (σ(company-name = ―First BankCorporation‖
∧ salary > 10000) works ⋈ employee) d. Πperson-name (employee ⋈works ⋈ company)
e. Πperson- name((employee⋈manages)⋈(managername=employee2.personname∧ employee.street=employee2.street∧employee.city=employee2.city) (ρemployee2 (employee)))
Q35. What is Query Language. Explain the different operations in Relational Algebra.
Ans.
1. Data Definition Language (DDL) 2. Data Manipulation Language (DML) 3. Data Query Language (DQL) 4. Data Control Language (DCL) 5. Data Administrative Statements (DAS) 6. Transaction Control Statements (TCS)
Data Definition Language (DDL) It is used for defining the database schema i.e. to
CREATE, ALATER & DROP Tables, Views and Indexes; like CREATE TABLE, ALTER
TABLE, DROP TABLE, CREATE VIEW, DROP VIEW, CREATE INDEX, DROP
INDEX.
Data Manipulation Language (DML) The DML commands are used to manipulate the
information in the tables; like INSERT, UPDATE or DELETE commands.
Data Query Language (DQL) This refers to SELECT command, which is used to extract
information from Tables. Syntax:
SELECT < list of attributes and/or aggregate of attributes of tables listed below> FROM <list
of tables> WHERE <predicate involving attributes of tables listed above and literals>
GROUP BY <list of attributes>
HAVING <predicate involving aggregated values> ORDER BY ASC/DESC < list of
attributes of tables listed above>
Data Control Language (DCL) These are security-related commands, which control user
access to the database. Database administrator grants or revokes user privileges by using
GRANT and REVOKE commands.
Data Administrative Statement (DAS) These are basically Audit commands used to
analyze the system performance. There are two commands START AUDIT and STOP
AUDIT.
Transaction Control Statements (TCS) These commands are used to control transactions;
like SET TRANSACTION, SAVEPOINT, COMMIT and ROLLBACK.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as input
and yields instances of relations as output. It uses operators to perform queries. An operator can be either unary or binary. They accept relations as their input and yield relations as
their output. Relational algebra is performed recursively on a relation and intermediate
results are also considered relations.
The fundamental operations of relational algebra are as follows −
• Select • Project
• Union
• Set different
• Cartesian product
• Rename
Q36. Write difference between Cross Join, Natural Join, left outer join and right outer
join with suitable example.
Ans.
A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on
the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. CARTESIAN OR CROSS JOIN: returns the Cartesian product of the sets of records from
the two or more joined tables. LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table. RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table.
Q37. Consider the following table : " Persons" table:( P_Id, LastName, FirstName,
Address, City) Give an expression in SQL for each of the following queries :
1. Select the content of the columns named "LastName" and "FirstName" from the
table . 2. Select only the distinct values from the column named "City" from the table above. 3. Select only the persons with the first name equal to "Tove" AND the last name
equa to "Svendson":
4. Select all the persons from the table above, however, we want to sort the persons by
their last name. 5. Insert a new row in the "Persons" table.
Solution:
Q38. Consider the following relation. The Primary key is Rollno, Isbn Student(RollNo,
Name, Branch), Book(Isbn, Title, Author, Publisher) Issue(Rollno, Isbn, te_of_issue).
Write the query in Relational algebra of the following-
i)List the Roll Number and Name of All CSE Branch Student.
ii) Find the name of students who have issued a book of publication „BPB‟. iii) List the title and author of all books which are issued by a student name started with ―a‟. iv)List the title of all books issued on or before 20/09/2012. v) List the name of student who will read the book of author named
―Sanjeev‟. Solution:
Q39. Write the SQL Queries for the given schema Student(Name, Studentnumber,
Class, Major) Course(CourseName, CourseNumber, Credit Hours, Department)
Section(Sectionidentifier, coursenumber, Semester, Year, Instructor)
Grade_report(studentNumber, SectionIdentifier, Grade)
Prerequisite(Coursenumber, Prerequisite Number)
a) Change the class of student „Uday‟ to 2.
b) Delete the record for the student wose name is „GEETA‟ and whose student
number is
c) For each section taught by Prof. Jain., retrieve the course number, semester, year and
number of students who took the section. d) Retrieve the names of all senior students majoring in ―CS‟
e) Retrieve the names and major departments of all students who have a grade of A in
all their courses.
Solutoin:
Q40. Student (RollNo, Name, Father_Name, Branch) Book(ISBN, Title, Author,
Publisher) SP(RollNo, ISBN, Bate_of_issue) Answer the following queries in SQL. (i) List Roll Number and name of all students of the branch ‟CSE‖.
(ii) Find the name of students who have issued a book published by „ABC‟ publisher.
(iii) List title of all books and their author issued by a student „XYZ‟. (iv) List title of all books issued on or before Jan,1,2011. (v) List all books published by publisher ―ABC‖.
Solution:
Q41. Dealer (Pealer-no. DealerName' address)
Part (Part-no. Part-name, color)
Assigned-to (Dealer-no, Part-no, cost)
Give an expression in relational algebra the following queries:
(i)Find the name of all dealers who supply 'Red' Parts'
(ii) Find the name of the dealers who supply both Yellow and Green Parts' (iii) Find the name of the dealers who supply all the Parts. (iv) Calculate total costs involved in purchasing, all parts. (v) List all dealer names.
Solution:
Q42. Consider the following schema
EMPLOYE (EID. EmployeeName, Street, City, Deptt, CompanyName)
COMPANY (Company),Name, City)
WORKS (EmployeeName, CompanyName, Salay);
MANAGES (EmployeeName. ManagerName)
Write SQL queries for the following : (i) Find out the names of all employees that have 'A'* anywhere in their name are in IT
department (ii) List the names of departments in ascending order and their employees in descending
order. (iii) Find the names, city, deptt, of all employees who work for 'TCS'. (iv) Find the name of employee who earns salary more than 30000. (v) List all manager names.
Solution:
Q43. Consider the following relational database employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name).
Give an expression in SQL to express each of the following queries:
i) Find the names and cities of residence of all employees who work for XYZ bank. ii) Find the names, street address, and cities of residence of all employees who work
for XYZ Bank and earn more than Rs. 10,000 per annum. iii) Find the names of all employees in this database who live in the same city as
the company for which they work.
Solution:
UNIT-III
Q45. Discuss the various anomalies associated with normalization?
Ans. 1) Insertion anomalies: Cannot insert a project unless an employee is assigned to it.
2) Deletion anomalies: When a project is deleted, it will result in deleting all the
employees who work on that project.
3) Updation anomalies: Changing the name of project number P1 from ―Billing‖ to
―Customer-Accounting‖ may cause this update to be made for all 100 employees
working on project P1.
Q46. MVD is a special case of JD. Discuss?
Ans.
Q47. Define 2 NF.
Ans. Second Normal Form:
Q49. Mention the desirable properties of relation decomposition.
Ans. Decomposition:
Q50.Distinguish between Functional dependency and Multivalued dependency.
Ans. Functional dependency:
Multivalued dependency.
Q51. Verify the statement ―A relation schema in 4 NF is in BCNF‖
Ans. BCNF
A relation schema RR is in BCNF with respect to a set F of functional dependencies if, for all functional dependencies in F+F+ of the form α→βα→β, where α⊆Rα⊆R and β⊆Rβ⊆R, at least one of the following holds: α→βα→β is a trivial functional dependency (that is, β⊆αβ⊆α).
αα is a superkey for schema RR
3NF A relation schema RR is in third normal form with respect to a set FF of functional dependencies if, for all functional dependencies in F+F+ of the form α→βα→β, where α⊆Rα⊆R and β⊆Rβ⊆R, at least one of the following holds:
α→βα→β is a trivial functional dependency.
αα is a superkey for RR.
Each attribute AA in β−αβ−α is contained in a candidate key for RR
So 3NF definition adds an additional 3rd condition to the BCNF definition: "Each
attribute AA in β−αβ−α is contained in a candidate key for RR". I think we can
correctly interpret this condition as β−αβ−α are key attributes.
Q52. What are RAT axioms? Also discuss the algorithm for finding the closure of
functional dependency with a suitable example?
Ans. RAT axioms or Inference Rule:
Algorithm for finding F closure
Q53. What is the purpose of Normalization? Explain 1NF, 2NF, 3NF and BCNF with
suitable example?
Ans. Normalization
First Normal Form
Second Normal Form:
Third Normal Form
BCNF
Q54. What do you mean by Loss-Less Join Decomposition? Explain with suitable
example that how functional dependency can be used to show that decompositions are
loss-less?
Ans. Lossless Join Decomposition
Q55. Write difference between BCNF Vs 3 NF.
Ans.
.NO. 3NF BCNF
Full 3rd Normal Form Boyce–Codd Normal Form Form
Definition A relation is in 3NF if it is in 2NF and A relation R is in BCNF if it is in 3NF and
no non-prime attribute transitively for each functional dependency X ⟶A in R,
depends on the primary key. In other X is a key or superkey in R. In other words,
words, a relation R is in 3NF if for the only difference between 3NF and BCNF
each functional dependency X ⟶A in is that in BCNF it is not present the second
R at least one of the following condition of the 3NF. This makes BCNF
conditions are met: stricter than 3NF as any relation that is in
X is a key or superkey in R
BCNF will be in 3NF but not necessarily every relation that is in 3NF will be in
A is a prime attribute in R BCNF.
Q56. Short Notes of the Following- i) MVD or JD ii) Normalization with
advantages
Ans. MVD:
Join Dependency:
Normalization with advantages
Q57. What do you mean by decomposition of a relation? Consider the relational
scheme R (A,B,C,D,E,F) and FDg ABC, C->A, D -> E, F -> A, E -> D Is the
decomposition of R into R1 (A, C, D), R2 (B, C, D) and R3 (E, F, D) lossless?
Solution: Apply algorithm 15.2 from Chapter 15 of Navathe to test if the decomposition of
R into {R1,R2,R3 is a lossless join decomposition.
The initial table looks as follows:
A B C D E F
R1(A,C,D) a1 b12 a3 a4 b15 b16
R2(B,C,D) b21 a2 a3 a4 b25 b26
R3(E,F,D) b31 b32 b33 a4 a5 a6
Apply FD A -> BC to the initial table to modify the violating dependencies. There will be
no change in the table as no values are matching.
A B C D E F
R1(A,C,D) a1 b12 a3 a4 b15 b16
R2(B,C,D) b21 a2 a3 a4 b25 b26
R3(E,F,D) b31 b32 b33 a4 a5 a6
Apply FD C->A to the initial table to modify the violating dependencies. b21 will be
changed to a1 as there is one value where C is a3 and A is a1.
A B C D E F
R1(A,C,D) a1 b12 a3 a4 b15 b16
R2(B,C,D) b21 a1 a2 a3 a4 b25 b26
R3(E,F,D) b31 b32 b33 a4 a5 a6
Apply FD D->E to the initial table to modify the violating dependencies. b15, b25 will
be changed to a5 as there is one value where D is a5 and E is a1.
A B C D E F
R1(A,C,D) a1 b12 a3 a4 b15 a5 b16
R2(B,C,D) b21 a1 a2 a3 a4
a5 b26 b25
R3(E,F,D) b32 b33 a4 a5 a6
Apply FD E->D to the initial table to modify the violating dependencies. No change will
be there.
A B C D E F
R1(A,C,D) a1 b12 a3 a4 b15 a5 b16
R2(B,C,D) b21 a1 a2 a3 a4 b25 a5 b26
R3(E,F,D) b32 b33 a4 a5 a6
Apply FD F->A to the initial table to modify the violating dependencies.
A B C D E F
R1(A,C,D) a1 b12 a3 a4 b15 a5 b16
R2(B,C,D) b21 a1 a2 a3 a4 b25 a5 b26
R3(E,F,D) b31 b32 b33 a4 a5 a6
No change is there as no value is matching from F to A. All values are different.
There is no row which has all a‘s that‘s why, it is a lossy decomposition
Q58. Explain the requirements for lossless decomposition and dependency preserving.
Ans. Decomposition of a relation is done when a relation in relational model is not in
appropriate normal form. Relation R is decomposed into two or more relations if
decomposition is lossless join as well as dependency preserving.
Decomposition is lossy if R1 U R2 ≠ R
Decomposition is lossless if R1 U R2 = R
To check for lossless join decomposition using FD set, following conditions must hold:
Union of Attributes of R1 and R2 must be equal to attribute of R. Each attribute of R must be
either in R1 or in R2.
Att(R1) U Att(R2) = Att(R)
Intersection of Attributes of R1 and R2 must not be NULL.
Att(R1) ∩ Att(R2) ≠ Φ
Common attribute must be a key for at least one relation (R1 or R2)
Att(R1) ∩ Att(R2) -> Att(R1) or Att(R1) ∩ Att(R2) ->
Dependency Preserving Decomposition:
If we decompose a relation R into relations R1 and R2, All dependencies of R either must be
a part of R1 or R2 or must be derivable from combination of FD‘s of R1 and R2. For
Example, A relation R (A, B, C, D) with FD set{A->BC} is decomposed into R1(ABC) and
R2(AD) which is dependency preserving because FD A->BC is a part of R1(ABC).
Q59. Define partial functional dependency. Consider the following two sets of functional
dependencies F= {A ->C, AC ->D, E ->AD, E ->H} sets of functional dependencies F=
{A ->C, AC ->D, E ->AD, E ->H} and G = {A ->CD, E - >AH}. Check whether or not
they are equivalent.
Solution:
Q60. Define a minimal cover. Suppose a relation R(A,B,C) have FD set F={ A→B, B→C
, A→C , AB→B,AB→C , AC→B }. Convert this set into minimal cover.
Solution:
Q62. Explain Trivial and non-trivial functional dependency.
Ans. Trivial − If a functional dependency (FD) X → Y holds, where Y is a subset of X, then
it is called a trivial FD. Trivial FDs always hold.
Non-trivial − If an FD X → Y holds, where Y is not a subset of X, then it is called a non-
trivial FD.
Q63. Define canonical cover. Compute canonical cover for the following:
R=(A,B,C),F={ A→BC, B→C , A→B , AB→C }.
Solution:
Q64. Consider a relation R(A, B, C) with the FDs :
A→B, B→C Is the decomposition of R into Rl(B, C) and R2(A, B)lossless ?
Solution:
UNIT-IV
Q65. What is the difference between Aborted and Failed state of a Transaction.
Ans. Failed –Transaction is fail after the discovery that normal execution can no longer
proceed.
Aborted – transaction abort after the transaction has been rolled back and the database
restored to its state prior to the start of the transaction. Two options after it has been aborted:
a)restart the transaction: can be done only if no internal logical error
b)kill the transaction
Q66. Explain I in ACID Property.
Ans. Isolation. Although multiple transactions may execute concurrently, each transaction
must be unaware of other concurrently executing transactions. Intermediate transaction
results must be hidden from other concurrently executed transactions.
That is, for every pair of transactions Ti and Tj, it appears to Ti that either Tj, finished
execution before Ti started, or Tj started execution after Ti finished.
Q67. Define schedule.
Ans. Schedule:
Q68. Define the term ACID properties?
Ans. 1)Atomicity. Either all operations of the transaction are properly reflected in the
database or none are.
2)Consistency. Execution of a transaction in isolation preserves the consistency of the
database.
3)Isolation. Although multiple transactions may execute concurrently, each transaction must
be unaware of other concurrently executing transactions. Intermediate transaction results
must be hidden from other concurrently executed transactions.
That is, for every pair of transactions Ti and Tj, it appears to Ti that either Tj, finished
execution before Ti started, or Tj started execution after Ti finished.
4)Durability. After a transaction completes successfully, the changes it has made to the
database persist, even if there are system failures.
Q69. Why is concurrency control needed?
Ans. Concurrency Control is needed:
The Lost Update Problem
This occurs when two transactions that access the same database items have their operations
interleaved in a way that makes the value of some database item incorrect.
The Temporary Update (or Dirty Read) Problem
This occurs when one transaction updates a database item and then the transaction fails for
some reason
The updated item is accessed by another transaction before it is changed back to its original
value.
The Incorrect Summary Problem
If one transaction is calculating an aggregate summary function on a number of records while
other transactions are updating some of these records, the aggregate function may calculate
some values before they are updated and others after they are updated.
Q70. What is Serializability? How it is tested?
Ans. Serializability:
Testing of Serializability:
Q71. What is recoverable schedule? Why is recoverability of schedule is desirable? Are
there any circumstances under which it would be desirable to allow non-recoverable
schedules? Explain Your answer.
Ans. Recoverable schedule:
A recoverable schedule is one where, for each pair of transactions Ti and Tj such that Tj
reads data items previously written by Ti, the commit
operation of Ti appears before the commit operation of Tj. Recoverable schedules are
desirable because failure of a transaction might otherwise bring the system into an
irreversibly inconsistent state. Non recoverable schedules may sometimes be needed when
updates must be made visible early due to time constraints, even if they have not yet been
committed, which may be required for veryl ong duration transactions.
Q72. Compare and contrast the features of log based recovery mechanism versus check
pointing based recovery.
Ans. Check pointing is done with log-based recovery schemes to reduce the time required for
recovery after a crash. If there is no check pointing, then the entire log must be searched after
a crash, and all transactions undone/redone from the log. If check pointing had been
performed, then most of the log-records prior to the checkpoint can be ignored at the time of
recovery. Another reason to perform checkpoints is to clear log-records from stable storage as
it gets full. Since check points cause some loss in performance while they are being taken,
their frequency should be reduced if fast recovery is not critical. If we need fast recovery
check pointing frequency should be increased. If the amount of stable storage available is
less, frequent check pointing is unavoidable. Checkpoints have no effect on recovery from a
disk crash; archival dumps are the equivalent of checkpoints for recovery from disk crashes.
Log Based Recovery:
Check pointing based Recovery:
Q73. Explain about characterizing schedules based on serializability.
Ans. Based on Serializability schedules can be characterize as
Conflict Serializable:
View Serializable:
Q74.Write short notes on: i. Deadlock Avoidance and Recovery in Database
Transaction
Ans. Deadlock Avoidance:
Deadlock Rcovery:
Q75. Define Transaction and explain its properties with suitable example.
Ans.Transaction: Logical unit of database processing that includes one or more access
operations (read -retrieval, write - insert or update, delete).
A transaction (set of operations) may be stand-alone specified in a high level language like
SQL submitted interactively, or may be embedded within a program.
ACID Properties
1)Atomicity. Either all operations of the transaction are properly reflected in the database or
none are.
2)Consistency. Execution of a transaction in isolation preserves the consistency of the
database.
3)Isolation. Although multiple transactions may execute concurrently, each transaction must
be unaware of other concurrently executing transactions. Intermediate transaction results
must be hidden from other concurrently executed transactions.
That is, for every pair of transactions Ti and Tj, it appears to Ti that either Tj, finished
execution before Ti started, or Tj started execution after Ti finished.
4)Durability. After a transaction completes successfully, the changes it has made to the
database persist, even if there are system failures.
Example: Transaction to transfer $50 from account A to account B:
1. read(A)
2. A:=A–50
3. write(A)
4. read(B)
5. B:=B+50
6. write(B)
Atomicity requirement
if the transaction fails after step 3 and before step 6, money will be ―lost‖ leading to an
inconsistent database state
Failure could be due to software or hardware
the system should ensure that updates of a partially executed transaction are not reflected in
the database
Durability requirement — once the user has been notified that the transaction has
completed (i.e., the transfer of the $50 has taken place), the updates to the database by the
transaction must persist even if there are software or hardware failures.
Consistency requirement in above example:
the sum of A and B is unchanged by the execution of the transaction
In general, consistency requirements include
Explicitly specified integrity constraints such as primary keys and foreign keys
Implicit integrity constraints
e.g. sum of balances of all accounts, minus sum of loan amounts must equal value of cash-in-
hand
A transaction must see a consistent database.
During transaction execution the database may be temporarily inconsistent.
When the transaction completes successfully the database must be consistent
Erroneous transaction logic can lead to inconsistency
Isolation requirement — if between steps 3 and 6, another transaction T2 is allowed to
access the partially updated database, it will see an inconsistent database (the sum A + B will
be less than it should be).
T1 T2
1. read(A)
2. A:=A–50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B:=B+50
6. write(B
Isolation can be ensured trivially by running transactions serially that is, one after the other.
Q76. What is schedule? What are its types? Explain view serializable and cascadeless
schedule with suitable example of each.
Ans. Schedule –Schedule is a sequences of instructions that specify the chronological order
in which instructions of concurrent transactions are executed. A schedule for a set of
transactions must consist of all instructions of those transactions. Schedule must preserve the
order in which the instructions appear in each individual transaction.
Schedule are of two types:
Serial Schedule
Non serial schedule
View serializable schedule:
Cascadeless schedule:
Q77. What is log file? Write the steps for log based recovery of a system with suitable
example
Ans. Log:
Q78. What is deadlock? What are necessary conditions for it? How it can be detected
and recovered?
Ans. Deadlock:
Consider the partial schedule
Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to wait for T3 to
release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock
on A.
Such a situation is called a deadlock.
To handle a deadlock one of T3 or T4 must be rolled back and its locks released.
Deadlock detection:
Deadlock Recovery:
Q79. Which of the following schedules are conflicts serializable? For each serializable
schedule find the equivalent schedule. S1: r1(x); r3(x); w3(x); w1(x); r2(x) S2: r3(x);
r2(x); w3(x); r1(x); w1(x) S3: r1(x); r2(x); r3(y); w1(x); r2(z); r2(y); w2(y)
Solution:
Q80. Determine whether the following schedule is conflict serializable or not:
T1: R(X), T2: R(X), T1:W(X), T2:R(Y), T3:R(Z),T2:W(Z), T3:W(X), T3:W(Y),
T2:W(Y), T3:W(Z)
Solution:
Q81. Briefly describe cascadeless schedule.
Ans. Cascadeless Schedule:
Q82. What do you mean by serializability? Discuss the conflict and view serialzability
with example. Discuss the testing of serializability also.
Ans. Serializability:
Conflict Serializability:
View Serializability:
Testing of Serializability:
Q83. What is Log? How is it maintained? Discuss the features of deferred database
modification and immediate database modification in brief.
Ans. Log:
Deferred Database Modification
Immediate Database Modificatoin:
Q84. Compare the deferred- and immediate-modification versions of the log-based
recovery scheme in terms of ease of implementation and over head cost.
Ans. • The recovery scheme using a log with deferred updates has the following
advantagesover the recoveryscheme with immediate updates: a. The scheme is easier and
simpler to implement since fewer operations and routines areneeded, i.e., no UNDO. b. The
scheme requires less overhead since no extra I/O operations need to be done until commit
time (log records can be kept in memory the entire time).
c. Since the old values of data do not have to be present in the log-records, this scheme
requires less log storage space. • The disadvantages of the deferred modification scheme are:
a. When a data item needs to accessed, the transaction can no longer directly read the correct
page from the database buffer, because a previous write by the same transaction to the same
data item may not have been propagated to the database yet. It might have updated a local
copy of the data item and deferred the actual database modification. Therefore finding the
correct version of a data item becomes more expensive. b. This scheme allows less
concurrency than the recovery scheme with immediate updates. This is because write-lock
sare held by transactions till commit time. c. For long transaction with many updates, the
memory space occupied by log record sand local copies of data items may become too high.
Q85. What are distributed databases? Discuss the various concurrency protocol used in
distributed database in detail?
Ans.
A distributed database is a collection of multiple interconnected databases, which are spread
physically across various locations that communicate via a computer network.
Concurrency Control in Distributed Systems
1)Locking Based Concurrency Control Protocols
a) One-phase Locking Protocol: In this method, each transaction locks an item before use
and releases the lock as soon as it has finished using it. This locking method provides for
maximum concurrency but does not always enforce serializability. b) Two-phase Locking Protocol: In this method, all locking operations precede the first
lock-release or unlock operation. The transaction comprise of two phases. In the first phase, a
transaction only acquires all the locks it needs and do not release any lock. This is called the
expanding or the growing phase. In the second phase, the transaction releases the locks and
cannot request any new locks. This is called the shrinking phase.
2) Timestamp Concurrency Control Algorithms
Timestamp-based concurrency control techniques generate serializable schedules such that
the equivalent serial schedule is arranged in order of the age of the participating transactions.
Some of timestamp based concurrency control algorithms are −
Basic timestamp ordering algorithm.
Conservative timestamp ordering algorithm.
Multiversion algorithm based upon timestamp ordering.
3)Distributed Timestamp Concurrency Control
In a centralized system, timestamp of any transaction is determined by the physical clock
reading. But, in a distributed system, any site‘s local physical/logical clock readings cannot
be used as global timestamps, since they are not globally unique. So, a timestamp comprises
of a combination of site ID and that site‘s clock reading.
For implementing timestamp ordering algorithms, each site has a scheduler that maintains a
separate queue for each transaction manager. During transaction, a transaction manager sends
a lock request to the site‘s scheduler. The scheduler puts the request to the corresponding
queue in increasing timestamp order. Requests are processed from the front of the queues in
the order of their timestamps, i.e. the oldest first.
4) Distributed Optimistic Concurrency Control Algorithm
Rule 1 − According to this rule, a transaction must be validated locally at all sites when it
executes. If a transaction is found to be invalid at any site, it is aborted. Local validation
guarantees that the transaction maintains serializability at the sites where it has been
executed. After a transaction passes local validation test, it is globally validated.
Rule 2 − According to this rule, after a transaction passes local validation test, it should be
globally validated. Global validation ensures that if two conflicting transactions run together
at more than one site, they should commit in the same relative order at all the sites they run
together. This may require a transaction to wait for the other conflicting transaction, after
validation before commit. This requirement makes the algorithm less optimistic since a
transaction may not be able to commit as soon as it is validated at a site.
Q86. Define timestamp.
Ans. Timestamp:
Q87. Define Exclusive Lock.
Ans. Exclusive lock:
Q88. What do you mean by time stamping protocols for concurrency control? Discuss
multi-version scheme of concurrency control also.
Ans.Time Stamp based Protocol:
Multiversion Timestamp ordering protocol:
Q89. What do you mean by Locking techniques of concurrency control? Discuss the
various locking techniques and recovery with concurrent transaction also in detail.
Ans. Locking techniques for concurrency control
One of the main techniques used to control concurrency execution of transactions (that is, to
provide serializable execution of transactions) is based on the concept of locking data items.
A lock is a variable associate with a data item in the database and describes the status of that
data item with respect to possible operations that can be applied to the item. Generally
speaking, there is one lock for each data item in the database. The overall purpose of locking
is to obtain maximum concurrency and minimum delay in processing transactions.
In the next a few sections, we will discuss the nature and types of locks, present several two-
phase locking protocols that use locking to guarantee serialisability of transaction schedules,
and, finally, we will discuss two problems associated with the use of locks â€― namely
deadlock and livelock â€― and show how these problems are handled.
Types of locks
The idea of locking is simple: when a transaction needs an assurance that some object,
typically a database record that it is accessing in some way, will not change in some
unpredictable manner while the transaction is not running on the CPU, it acquires a lock on
that object. The lock prevents other transactions from accessing the object. Thus the first
transaction can be sure that the object in question will remain in a stable state as long as the
transaction desires.
There are several types of locks that can be used in concurrency control. Binary locks are the
simplest, but are somewhat restrictive in their use.
Binary locks
A binary lock can have two states or values: locked and unlocked (or 1 and 0, for simplicity).
A distinct lock is associated with each database item X. If the value of the lock on X is 1,
item X is locked and cannot be accessed by a database operation that requests the item. If the
value of the lock on X is 0, item X is unlocked, and it can be accessed when requested. We
refer to the value of the lock associated with item X as LOCK(X).
Two operations, lock and unlock, must be included in the transactions when binary locking is
used. A transaction requests access to an item X by issuing a lock(X) operation. If LOCK(X)
= 1, the transaction is forced to wait; otherwise, the transaction sets LOCK(X) := 1 (locks the
item) and allows access. When the transaction is through using the item, it issues an
unlock(X) operation, which sets LOCK(X) := 0 (unlocks the item) so that X may be accessed
by other transactions. Hence, a binary lock enforces mutual exclusion on the data item. The
DBMS has a lock manager subsystem to keep track of and control access to locks.
When the binary locking scheme is used, every transaction must obey the following
rules:
A transaction T must issue the operation lock(X) before any read_item(X) or write_item(X)
operations are performed in T.
A transaction T must issue the operation unlock(X) after all read_item(X) and write_item(X)
operations are completed in T.
A transaction T will not issue a lock(X) operation if it already holds the lock on item X.
A transaction T will not issue an unlock(X) operation unless it already holds the lock on item
X.
These rules can be enforced by a module of the DBMS. Between the lock(X) and unlock(X)
operations in a transaction T, T is said to hold the lock on item X. At most, one transaction
can hold the lock on a particular item. No two transactions can access the same item
concurrently.
Shared and exclusive locks
The binary locking scheme described above is too restrictive in general, because at most one
transaction can take hold on a given item. We should allow several transactions to access the
same item X if they all access X for reading purposes only. However, if a transaction is to
write an item X, it must have exclusive access to X. For this purpose, we can use a different
type of lock called multiple-mode lock. In this scheme, there are three locking operations:
read_lock(X), write_lock(X) and unlock(X). That is, a lock associated with an item X,
LOCK(X), now has three possible states: 'read-locked', 'write-locked' or 'unlocked'. A read-
locked item is also called share-locked, because other transactions are allowed to read access
that item, whereas a write-locked item is called exclusive-locked, because a single transaction
exclusively holds the lock on the item.
If a DBMS wishes to read an item, then a shared (S) lock is placed on that item. If a
transaction has a shared lock on a database item, it can read the item but not update it. If a
DBMS wishes to write (update) an item, then an exclusive (X) lock is placed on that item. If
a transaction has an exclusive lock on an item, it can both read and update it. To prevent
interference from other transactions, only one transaction can hold an exclusive lock on an
item at any given time.
If a transaction A holds a shared lock on item X, then a request from another transaction B
for an exclusive lock on X will cause B to go into a wait state (and B will wait until A’s
lock is released). A request from transaction B for a shared lock on X will be granted (that is,
B will now also hold a shared lock on X).
If transaction A holds an exclusive lock on record X, then a request from transaction B for a
lock of either type on X will cause B to go into a wait state (and B will wait until A’s
lock is released).
Q90. Explain two phase locking protocol with suitable example.
Ans. Two Phase Locking Protocol:
Q91. Write the salient features of graph based locking protocol with suitable example
Ans. Graph Based Protocols are yet another way of implementing Lock Based Protocols.
As we know the prime problems with Lock Based Protocol has been avoiding Deadlocks and
ensuring a Strict Schedule. We‘ve seen that Strict Schedules are possible with following
Strict or Rigorous 2-PL. We‘ve even seen that Deadlocks can be avoided if we follow
Conservative 2-PL but the problem with this protocol is it cannot be used practically.
Graph Based Protocols are used as an alternative to 2-PL. Tree Based Protocols is a simple
implementation of Graph Based Protocol.
A prerequisite of this protocol is that we know the order to access a Database Item. For this
we implement a Partial Ordering on a set of the Database Items (D) {d1, d2, d3, ….., dn} .
The protocol following the implementation of Partial Ordering is stated as-
If di –> dj then any transaction accessing both di and dj must access di before accessing
dj.
Implies that the set D may now be viewed as a directed acyclic graph (DAG), called a
database graph
Q92. What do you mean by multi granularity? How the concurrency is maintained in
this case.
Ans. Multi granularity:
Q93. What is Two phase Locking (2PL)? Describe with the help of example
Ans. Two Phase Locking Protocol:
Q94. What are multi version schemes of concurrency control? Describe with the help of
an example. Discuss the various Time stamping protocols for concurrency control also.
Ans. Multiversion Schemes of Concurrency control
Time stamp protocol:
Q95. Explain the following protocols for concurrency control.
i) Lock based protocols ii) Time Stamp based protocols
Ans. i) Lock based protocols:
ii) Time Stamp based protocols
Q96. Explain the working of various timeStamping protocols for concurrency control.
Ans. Time Stamping protocols
Q97. What do you mean by multiple granularities? How is it implemented in
transaction system.
Ans. Multiple Granularity:
Q98. Explain the phantom phenomenon. Devise a timestamp based protocol that avoids
the phantom phenomenon.
Ans. Phantom phenomenon :
The phantom phenomenon arises when, due to an insertion or deletion, two transactions
logically conflict despite not locking any data items in common. The insertion case is
described in the book. Deletion can also lead to this phenomenon. Suppose Ti deletes a tuple
from a relation while Tj scans the relation. If Ti deletes the tuple and then Tj reads the
relation, Ti should be serialized before Tj. Yet there is no tuple that both Ti and Tj conflict
on. An interpretation of 2PL as just locking the accessed tuples in a relation is incorrect.
There is also an index or a relation data that has information about
thetuplesintherelation.Thisinformationisreadbyanytransactionthatscans the relation, and
modified by transactions that update, or insert into, or delete from the relation. Hence locking
must also be performed on the index or relation data,and this will avoid the phantom
phenomenon.
In the text, we considered two approaches to dealing with the
phantomphenomenonbymeansoflocking.Thecoarsergranularityapproachobviously works for
time stamps as well. The B+-tree index based approach can be adapted to time stamping by
treating index buckets as data items with timestamps associated with them, and requiring that
all read accesses use an index. We now show that this simple method works. Suppose a
transaction Ti wants to access all tuples with a particular range of search-key values, using a
B+ tree index on that search-key. Ti will need to read all the buckets in that index which have
key values in that range. It can be seen that any delete or insert of a tuple with a key-value in
the same range will need to write one of the index buckets read by Ti. Thus the logical
conflict is converted to a conflict on an index bucket, and the phantom phenomenon is
avoided. Example-
Here,
T1 reads X
T2 reads X
T1 deletes X
T2 tries read X but does not find it
In this example-
1.T2 finds that there does not exist any variable X when it tries reading X again
2. T2 wonders who deleted the variable X because according to it,it is running in
isolation.
Q99. What are the pitfalls of lock based protocols?
Ans.
Consider the partial schedule
Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to wait for T3 to
release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock
on A.
Such a situation is called a deadlock.
To handle a deadlock one of T3 or T4 must be rolled back and its locks released.
The potential for deadlock exists in most locking protocols. Deadlocks are a necessary evil.
Starvation is also possible if concurrency control manager is badly designed. For example:
A transaction may be waiting for an X-lock on an item, while a sequence of other
transactions request and are granted an S-lock on the same item.
The same transaction is repeatedly rolled back due to deadlocks
Q100. What is Time stamp based protocol? What is need of time stamping protocol?
Explain how timestamp ordering protocol works?
Ans. Time stamp based protocol