Dbms Module 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Module 4

SQL: Advance Queries

Q1)Explain different Set Operations in SQL.

SQL includes the operations union, intersect, and minus, which operates on the relations and
corresponds to the relational algebra operation respectively.
Use of UNION Keyword: UNION keyword is used to perform, relational algebra operation,
union (  ). UNION operation bydefault eliminates duplicate tuples, to retain duplicates we must
use UNION ALL in the place of UNION.
Example:Findthecustomershavingloan,oranaccount,orbothatMysuru branch.
(SELECTCUST_NAMEFROMDEPOSIT
WHERE BRANCH_NAME = ‘Mysuru’)
UNION
(SELECTCUST_NAMEFROMBORROW
WHERE BRANCH_NAME = ‘Mysuru’);
UseofINTERSECTKeyword:INTERSECTkeywordisusedtoperform,relationalalgebra operation,
intersect ( ). INTERSECT operation by default eliminates duplicate tuples.
Example:Findthecustomershaving bothloan,andanaccount,atMysuru branch.
(SELECTCUST_NAMEFROMDEPOSIT
WHERE BRANCH_NAME = ‘Mysuru’)
INTERSECT
(SELECTCUST_NAMEFROMBORROW
WHERE BRANCH_NAME = ‘Mysuru’);
UseofMINUSKeyword:MINUSkeywordisusedtoperform,relationalalgebraoperation, minus (  ).
MINUS operation by default eliminates duplicate tuples.
Example:Findthecustomersof Mysurubranchwho haveanaccounttherebut noloanthere.
(SELECTCUST_NAMEFROMDEPOSIT
WHERE BRANCH_NAME=‘Mysuru’)
MINUS
(SELECTCUST_NAMEFROMBORROW
WHERE BRANCH_NAME = ‘Mysuru’);
Note:Allthesesetoperationsbydefault,eliminatesduplicatetuplesfromtheresult.Asbecause
The set operations apply only to the union compatible relations, user should make sure that the
two relationshavethesameattributesandthattheattributesappearinthesame order in both relations.

Q2)Write a note on Nested Queries and Set Comparisons.


A complete SELECT query within the WHERE clause of another query is called Nested Query.
In nested queries, the sub querywill be executed first and the result of the sub query will be used
as search criteria for the select statement in the Outer query.

Set Comparison Operators: The set comparison operators are IN, SOME, ALL, ANY,
CONTAINS and NOT CONTAINS. With the keywords ANY, SOME and ALL, other operators
like >, <, =, >=, <= and<> can be combined. The key words ANY and SOME have the same
meaning.

Use IN key word: IN is a comparison operator that compares the single value ‘v’ with a set (or
multiset) of values ‘V’ and evaluates to TRUE if ‘v’ is one of the elements in V. The IN operator
can also compare tuple of values in parentheses with a set. IN keyword is equivalent to =ANYand
=SOME.

For example: Find the customers who are having account in the branch where the “Kumar”
has an account.
Selectcust_name
Fromdeposit
Wherebranch_namein(selectbranch_name
Fromdeposit
Wherecust_name=‘Kumar’);

Use of SOME (ANY) keyword: SOME is a comparison operator that compares the single value
‘v’ with a set (or multiset) of values‘V’.With thiskeyword the operators like >,<,=,>=,<=and
<>canbe combined.
For example: v>SOME V returns TRUE if value ‘v’ is greater than at least one member of the
value set ‘V’.

Use of ALL keyword: ALL is a comparison operator that compares the single value ‘v’ with aset
(or multi set) of values‘V’.With thiskeyword the operatorslike >,<,=,>=,<= and<> can be
combined.
Forexample:v>ALLVreturnsTRUEifthevalue‘v’isgreater thanallthevaluesinsetV.

For example: Find the names of employees whose salary is greater than the salary of all the
employees in department 5.
Select Fname
Fromemployee
Wheresalary>ALL(selectsalary
Fromemployee
WhereDno=5);
Use of CONTAINSKeyword: CONTAINSconstruct canbe used tocompare twosetsofvalues and
returns TRUEif one set contains all the members of some other set. For Example: Find the
employee who works on all the projects controlled by department number 5.
SelectFnameFromemployee Where((select Pno
From works_on
Wheressn=essn)
CONTAINS
(Select Pnumber
Fromproject
Where Dnum=5));
Q3)Explain correlated nested query.
Whenever a condition in the WHERE clause of a nested query references some attribute in the
outer query, the two queries are said to be correlated. The nested queryis evaluated once for each
tuple in the outer query.
Example: Retrieve the name of employees who has a dependent with the same name as the
employee.
SelectFnameFromEmployeee
Wheressnin(selectessn
From dependent
dWheree.fname=d.name)
;
Q4)Explain EXISTS, NOTEXISTS and UNIQUE functions in SQL.
EXISTS function is used in SQL to check whether the result of the correlated nested query is
empty (contains no tuples) or not. EXISTS and NOT EXISTS are usually used in conjunction
with correlated nested query.
EXISTS returns TRUE if there is at least one tuple in the result of the nested query and
FALSE otherwise.
Example:Retrievethenameofemployees whohasadependentwiththesamenameasthe employee.
Select Fname
From Employee e Where
EXISTS(select*FromDependentd
Wheree.fname=d.nameande.ssn=d.essn);

NOTEXISTSreturnsTRUE iftherearenotuplesin theresultofthenested queryand FALSE otherwise.


Example:Retrievethenameof employeeswho haveno dependents.
Select Fname
FromEmployeee
Where NOT EXISTS(select*
From Dependent d
Wheree.ssn=d.essn);
UNIQUEfunctionreturnsTRUEiftherearenoduplicatesintheresultofthequeryand FALSE otherwise.
Q5)What areaggregate functions in SQL? Explain with examples.
Aggregate functionsareappliedtoset oftuplesandreturnasingle value.Some ofthebuiltin functions
are: COUNT, SUM, MAX, MIN, AVG.
The COUNT function returns the number of tuples or values as specified in the query. The
functions SUM, MAX, MIN, AVG are applied to a set or multiset of numeric values and return
the sum, maximum, minimum, average of those values respectively.
ThesefunctionscanbeusedintheSELECTorHAVING clause.

Example: Retrieve the total number of employees, sum of the salaries, the maximum, minimum, and average
salary of all employees.
SelectCOUNT(*),SUM(Salary),MAX(Salary),MIN(Salary),AVG(Salary)
FROM Employee;
Q6)Explain GROUP BY and HAVING clause in SQL.
The SQLGROUP BYclause is used in collaboration with the SELECT statement to arrange
identical data into groups.
For Example: Retrieve the total no of employees and total salary of employees in each
department, then GROUP BY query would be as follo ws:
SELECTDNO,COUNT(*)asNo_of_Emp,SUM(SALARY)
FROM EMPLOYEE
GROUPBY DNO;
The HAVING clause provides a condition on the group of tuples associated with each value of
the grouping attributes. Only the groups that satisfythe condition are retrieved in the result of the
query.

For example: For each department in which more than2 employees work, retrieve the total
num of employees and total salary of employees.
SELECTDNO,COUNT(*)asNo_of_Emp,SUM(SALARY)
FROM EMPLOYEE
GROUP BY DNO
HAVINGCOUNT(*)>2;

Q7)What are views in SQL?Explain how views are created and dropped.List the advantages
of views?
AviewinSQLterminologyisasingletablethatisderivedfromothertables.Aviewdoesnot necessarily
exist in physical form, it is considered to be a virtual table.
We can think of a view as a wayof specifying a table that we need to
referencefrequently,even though it may not exist physically.
Creation(orSpecification) ofViewsinSQL
InSQL,thecommandtospecifyaviewisCREATEVIEW.Theviewisgivena(virtual)table name (or view
name), a list of attribute names, and a queryto specifythe contents of the view.
CREATEVIEW<viewname>[<col1name>,<col2name>,…,<colnname>]as<select>;

Note: If none of the view attributes results from applying functions or arithmetic operations, we
do not have to specify new attribute names for the view, since they would be the same as the
names of the attributes of the defining tables in the default case.
CREATE VIEW <view name> [<col1 name>, <col2 name>, … ,<coln name>] as <select>;
Example: Create a view of all the department names along with the total number of
employees and total salary of all employees in each department.
CREATE VIEW DEPT_INFO (Dept_name, No_of_emps,
Total_sal)ASSELECTDname,COUNT(*),SUM(Salary)
FROM DEPARTMENT, EMPLOYEE
WHEREDnumber=Dn
o GROUP BY Dname;
Thecommandused todropaviewis:DROP VIEWand itssyntaxis:
DROPVIEW<view_name>;

Example:DROPVIEW DEPT_INFO;
AdvantagesofViews:
1) Simplifythespecificationofcertain queries.
2) Used assecurityandauthorizationmechanism.

Q8)Explain how views are implemented?


Twoapproachesusedforimplementingaviewforquerying are:
i. Query modification
ii. Viewmaterialization
Query modification involves modifying the view query into a query on the un derlying base
tables. The disadvantage of this approach is that it is inefficient for views defined via complex
queries that are time consuming to execute.
View materialization involves physically creating a temporary view table when the view is first
queried and keeping that table on the assumption that other queries on the view will follow.
Inthiscase,theviewtableshouldbeupdatedwhen thebasetable’scontentsare changed.
Onesuch techniqueisbased ontheconceptofincrementalupdate.

Q9)Explain the problems associated with updating of views?


Updatingofviewsiscomplicatedandcan be ambiguous.
1) For a view involving joins, an update operation may be mapped to update operations on the
underlying base relations in multiple ways. Hence, it is often not possible for the DBMS to
determine which of the updates is intended.
To illustrate potential problems with updating a view defined on multiple tables, consider the
WORKS_ON1 view, and suppose that we issue the command to update the PNAME attribute
of ‘John Smith’ from ‘ProductX’ to ‘ProductY’. This view update is shown in UV1:
UV1:UPDATEWORKS_ON1
SET Pname=‘ProductY’
WHERE Lname=‘Smith’AND
Fname=‘John’ AND Pname=‘ProductX’;
This query can be mapped into several updates on the base relations to give the desired update
effect on the view. Some of these updates will create additional side effects that affect the
resultof other queries. For example, here are two possible updates, (a) and (b), on the base
relations corresponding to the view update operation in UV1:
(a) : UPDATEWORKS_ON
SETPno=(SELECTPnumber
FROMPROJECT
WHEREPname=‘ProductY’)
WHERE Essn IN ( SELECT Ssn
FROMEMPLOYEE
WHERELname=‘Smith’ANDFname=‘John’)
AND
Pno=(SELECTPnumbe
r
FROMPROJEC
T
WHEREPname=‘ProductX’);
(b) : UPDATEPROJECT
SET Pname = ‘ProductY’
WHEREPname=‘ProductX;
Update (a) relates ‘John Smith’ to the ‘ProductY’ PROJECT tuple instead of the ‘ProductX’
PROJECT tuple and is the most likely desired update. However, (b) would also give the desired
update effect on the view, but it accomplishes this by changing the name of the ‘ProductX’ tuple
in the PROJECT relation to ‘ProductY’. It is quite unlikely that the user who specified the view
updateUV1wantstheupdatetobeinterpretedasin(b),sinceitalsohasthesideeffectof
changingalltheviewtuples withPname=‘ProductX’.

2) Some view updates maynot make much sense; for example, modifying the Total_salattribute
of the DEPT_INFO view does not make sense because Total_sal is defined to be the sum of
the individual employee salaries. This request is shown as UV2:
UV2: UPDATEDEPT_INFO
SET Total_sal=100000
WHEREDname=‘Research’;
Alargenumberofupdateson theunderlying baserelationscansatisfythisviewupdate.

Insummary, wecanmakethe following observations:


. A view with a single defining table is updatable if the view attributes contain the primary key
of the base relation, as well as all attributes with the NOT NULL constraint that do not have
default values specified.
.Viewsdefined on multipletables usingjoinsaregenerallynot updatable.
.Viewsdefinedusinggroupingand aggregate functionsarenot updatable.
Q10) How are triggers and assertions defined in SQL? Explain.
TriggersandassertionsarecreatedusingthecommandCREATETRIGGER,CREATE ASSERTION
respectively.
Triggers are a technique for specifying certain types of active rules. They are used to specify
automatic actions thatthedatabasesystemwillperformwhen certain eventsandconditionsoccur. The
model used to specify rules is referred to as Event-Condition-Action or ECA model. A rule in
ECA has 3 components.
i. Theevent(s)thattriggerstherule
ii. Thecondition thatdetermineswhethertheruleaction shouldbeexecuted.
iii. Theactiontobe taken.

GeneralsyntaxinOracle(mainoptionsonly):

CREATETRIGGER<trigger_name>(BEFORE|AFTER)<triggering_event> ON
<table_name>[WHEN<condition>]
<triggeractions>;

Example: Suppose we want to check whenever an employee’s salaryis greater than the salary of
his or her direct supervisor. Several events can trigger this rule: inserting a new employee,
changing an employee’s salary, or changing an employee’s supervisor. Suppose that the action to
take would be to call an external procedure inform_supervisor, which will notify the supervisor.
The rule could then be written as:

CREATE TRIGGER Salary_Violation


BEFOREINSERT|UPDATEOFSalary,
Supervisor_ssnONEMPLOYEE
FOR EACH ROW
WHEN(NEW.Salary>(SELECTSalaryFROMEMPLOYEE WHERE
Ssn=NEW.Supervisor_Ssn))
inform_supervisor(NEW.Supervisor_Ssn,NEW.Ssn)
;
In SQL, users can specify general constraints via declarative assertions, using the CREATE
ASSERTION statement of the DDL. Each assertion is given a constraint name and is
specifiedvia a condition similar to the WHERE clause of an SQL query.
For example,to specifythe constraint that the salaryofan employee must not be greater than the
salary of the manager of the department that the employee works for in SQL, we can write the
following assertion:
CREATEASSERTIONSALARY_CONSTRAINT
CHECK ( NOT EXISTS ( SELECT *
FROMEMPLOYEEE,EMPLOYEEM,DEPARTMENTD
WHERE E.Salary>M.Salary AND E.Dno=D.Dnumber
AND D.Mgr_ssn=M.Ssn ));
The constraint name SALARY_CONSTRAINT is followed by the keyword CHECK, which is
followed by a condition in parentheses that must hold true on every database state for
theassertion to be satisfied. The constraint name can be used later to refer to the constraint or to
modify or drop it.

Q21)Explain sub string pattern matching in SQL?


PatternmatchingcanbedoneusingLIKEcomparisonoperator.Partialstringsarespecifiedusing two
reserved characters:
%replacesanarbitrarynumber ofzeroormorecharacters.

Underscore(_)replacesasinglecharacter.
Example:Retrieve allemployeeswhosenamebeginswithletter A.
SELECTFname FROMEMPLOYEEWHERENameLIKE‘A%’;
Example:Retrieve allemployeeswho werebornduring 1960s.
SELECT Fname
FROMEMPLOYE
E
WHEREBdateLIKE‘ 196_’;

Note:Dateformat:dd-mmm-yyyy(mmmis first3letters ofmonth)

STORED PROCEDURES

Stored procedure is a set of logical group of SQL statements which are grouped to
perform aspecific task.

Benefits :

reduces the amount of information transfer between client and database server

Compilation step is required only once when the stored procedure is created.
Then after it does not require recompilation before executing unless it is modified
and reutilizes the same execution plan whereas the SQL statements need to be
compiled every time whenever it is sent for execution even if we send the same
SQL statement every time

It helps in re usability of the SQL code because it can be used by multiple users
and by multiple clients since we need to just call the stored procedure instead of
writing the same SQL statement every time. It helps in reducing the development
time
Syntax:

Create or replace procedure <procedure Name> [(arg1 datatype, arg2 datatype)]

Is/As

<declaration>

Begin

<SQL Statement>

Exception

End procedurename;

Creating a Simple Stored Procedure

Consider the following schema:

Student(usn:string,sname:string)

Let us now write a stored procedure to

create or replace procedure ssis

stu_cnt int;

begin

select count(*) into stu_cnt from students where sname='AKSHAY';

dbms_output.put_line('the count of student is :' || stu_cnt);

end ss;

Stored procedures can also have parameters. These parameters have to be valid SQL types,
andhave one of three different modes: IN, OUT, or INOUT.

IN parameters are arguments to the stored procedure

OUT parameters are returned from the stored procedure; it assigns values to all OUT
parameters that the user can process

INOUT parameters combine the properties of IN and OUT parameters: They


contain values to be passed to the stored procedures, and the stored procedure can
set their values as return values

Example:

CREATE PROCEDURE

Addlnventory ( IN book_isbn

CHAR(lO),

IN addedQty INTEGER)

UPDATE Books SET qty_in_stock = qtyjn_stock +

addedQtyWHERE bookjsbn = isbn

In Embedded SQL, the arguments to a stored procedure are usually variables in the host
language.For example, the stored procedure AddInventory would be called as follows:
EXEC SQL BEGIN DECLARE SECTION
char isbn[lO];
long qty;
EXEC SQL END DECLARE SECTION
/ / set isbn and qty to some values
EXEC SQL CALL AddInventory(:isbn,:qty);

Stored procedures enforce strict type conformance: If a parameter is of type INTEGER, it


cannot becalled with an argument of type VARCHAR.

Procedures without parameters are called static procedures and with parameters are called
dynamic procedures.

Example: stored procedure with parameter

create or replace procedure

emp(Essn int)as

begin
eName varchar(20);
select fname into eName from employee where ssn=Essn and dno=5; dbms_output.put_line('

the employee name is :'||Essn ||eName);

end emp;

Calling Stored Procedures

Stored procedures can be called in interactive SQL with the CALL

statement:CALL storedProcedureName(argl, arg2, .. ,argN);

Cursors
A major problem in embedding SQL statements in a host language like C is that an
impedance mismatch occurs because SQL operates on sets of records, whereas
languages like C do not cleanly support a set-of-records abstraction. The solution is to
essentially provide a mechanism that allows us to retrieve rows one at a time from a
relation- this mechanism is called a cursor
We can declare a cursor on any relation or on any SQL query. Once a cursor is declared, we can
open it (positions the cursor just before the first row)
Fetch the next row
Move the cursor (to the next row,to the row after the next n, to the first row or
previous rowetc by specifying additional parameters for the fetch command)
Close the cursor
Cursor allows us to retrieve the rows in a table by positioning the cursor at a particular
row andreading its contents.
Basic Cursor Definition and Usage
Cursors enable us to examine, in the host language program, a collection of rows
computed by anEmbedded SQL statement:
We usually need to open a cursor if the embedded statement is a SELECT. we
can avoidopening a cursor if the answer contains a single row
INSERT, DELETE and UPDATE statements require no cursor. some variants of
DELETEand UPDATE use a cursor.
Examples:
i) Find the name and age of a sailor, specified by assigning a value to the host
variable c_sid,declared earlier
EXEC SQL SELECT s.sname,s.age
INTO :c_sname, :c_age
FROM Sailaor s
WHERE s.sid=:c.sid;

The INTO clause allows us assign the columns of the single answer row to the host
variable c_sname and c_age. Therefore, we do not need a cursor to embed this query in
a host language program.

ii) Compute the name and ages of all sailors with a rating greater than the current value
of the hostvariable c_minrating
SELECT s.sname,s.age
FROM sailors s WHERE s.rating>:c_minrating;
The query returns a collection of rows. The INTO clause is inadequate. The solution is
to use a cursor:
DECLARE sinfo CURSOR FOR
SELECT s.sname,s.age
FROM sailors s
WHERE s.rating>:c_minrating;
This code can be included in a C program and once it is executed, the cursor sinfo is
defined.We can open the cursor by using the syntax:
OPEN sinfo;

with it.When the cursor is opened, it is positioned just before the first row.
We can use the FETCH command to read the first row of cursor sinfo into host language variables:
FETCH sinfo INTO :c_sname, :c_age;
When the FETCH statement is executed, the cursor is positioned to point at the next row
and the column values in the row are copied into the corresponding host variables. By
repeatedly executing this FETCH statement, we can read all the rows computed by the
query, one row at time.
When we are done with a cursor, we can close it:
CLOSE sinfo;
iii) To retrieve the name, address and salary of an employee specified by the variable ssn
Properties of Cursors
The general form of a cursor declaration is:
DECLARE cursorname [INSENSITIVE]
[SCROLL] CURSOR[WITH HOLD]
FOR some query
[ORDER BY order-item-list ]
[FOR READ ONLY I FOR UPDATE ]
A cursor can be declared to be a read-only cursor (FOR READ ONLY) or updatable
cursor (FOR UPDATE).If it is updatable, simple variants of the UPDATE and DELETE
commands allow us to update or delete the row on which the cursor is positioned. For
example, if sinfo is an updatable cursor and open, we can execute the following
statement:
UPDATE Sailors S
SET S.rating = S.rating -1
WHERE CURRENT of sinfo;
A cursor is updatable by default unless it is a scrollable or insensitive cursor in which
case it is read-only by default.
If the keyword SCROLL is specified, the cursor is scrollable, which means that variants
of the FETCH command can be used to position the cursor in very flexible ways;
otherwise, only the basic FETCH command, which retrieves the next row, is allowed
If the keyword INSENSITIVE is specified, the cursor behaves as if it is ranging over a
private copy of the collection of answer rows. Otherwise, and by default, other actions of
some transaction couldmodify these rows, creating unpredictable behavior.
A holdable cursor is specified using the WITH HOLD clause, and is not closed when the
transactionis committed.
Optional ORDER BY clause can be used to specify a sort order. The order-item-list is a
list of order- items. An order-item is a column name, optionally followed by one of the
keywords ASC or DESC Every column mentioned in the ORDER BY clause must also
appear in the select-list of the query associated with the cursor; otherwise it is not clear
what columns we should sort on

ORDER BY minage ASC, rating DESC

The answer is sorted first in ascending order by minage, and if several rows have the same
minagevalue, these rows are sorted further in descending order by rating
TRANSACTIONMANAGEMENT

Single-UserVersusMultiuser Systems
Oneofthecriteriaforclassifyingthedatabasesystemisbythenumberofuserswhocan use the
system concurrently. They are Single-user and Multiuser.
Singleuser:A DBMSissingleuserifatmostonlyoneuserata timecanusethesystem
.SingleuserDBMSare mostlyrestrictedtosome microcomputersystem.

Multiuser: A DBMS is multiuser if many users can use the system


concurrently.Multiple users can use the system simultaneously because of the concept
of multiprogramming , which allows the computer to process multiple programs at the
same time. Ifthere is only one CPU and the multiprogramming OS executes some
commands from one program , then suspend that program and execute some commands
from next program , and so on. A program is resumed at the point where it
wassuspended when it gets it turn to use the CPU. This concept is calledinterleaved
concurrency. If the computer system has multiple hardware processors (CPUs)
simultaneous processing of multiple programs is possibleleading to simultaneous
concurrency.
1) Define Transaction. Explain Read, Write operations of a transaction.
Transaction:Theexecutionofaprogramthataccessorchangesthecontentsofthe
databaseiscalledaTransaction.Thedataaccessoperationsthatatransactioncan include are,
Read_item(X):ReadsadatabaseitemnamedXintoaprogramvariable.
Executing the Read_item(X)command includes the following steps .
1. Findtheaddress of the diskblock that contains item X.
2. Copythedisk blockinto abufferin main memory(ifthat disk blockis not alreadyin some
main memorybuffer )
3. Copyitem Xfrom thebuffer totheprogramvariable named X.
Write_item(X):Writesthevalueoftheprogramvariable Xinto thedatabaseitem
named X . ExecutingaWrite_item(X) command includes the following steps
1. Findtheaddressof the diskblock thatcontains item X
2. Copythat disk block intoa buffer in main memory( if that disk block is not already in
some main memorybuffer )
3. CopytheitemXfromprogramvariablenamedXintoitscorrectlocationinthe buffer
4. Storethe updatedblock fromthe buffer block to disk.
Usuallythedecisionaboutwhentostorebackamodifieddiskblockthatisinamain memory
buffer is handled by the recovery manager or the operating system.
Needofconcurrencycontrol
2) Whataretheproblemswithconcurrency?Explaineachwithanexample.
Severalproblemscanoccurwhenconcurrenttransactionsexecuteinanuncontrolled manner,
some of the problems are:
1. The Lost Update Problem:This occurs when two transactions that access the same
database items have their operations interleave in a waythat makes the value of some
database item incorrect. Suppose that transactions T1 and T2 are submitted at
approximately same time, and suppose their operations are interleaved by the
operating system then the final value of item X is incorrect, because T2 reads thevalue
of X before T1 changes it, in the database, and hence the updated valueresulting from
T1 is lost.
2. 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.
For example consider two transactions T1 and T2 where T1 updates item X and then
fails before completion, so the system must change X back to its original value.
Before it can do so, transaction T2 may read temporary value of X, which will not be
recorded permanently in the database because of the failure of T1. The value of item
X that is read by T2 is called dirty data, because it has
beencreatedbytransactionthathasnotcompleted,hencethisproblemis alsoknownasthe
dirty read problem.
3. The Incorrect Summary Problem: This problem occur if one transaction is
calculating an aggregate summary function on a number of records while other
transactionsareupdatingsomeoftheserecords,theaggregatefunctionmaycalculate some
values before they are updated .
4. Unrepeatable read: This problem occurs if transaction T reads an item twice, andthe
item is changed by another transaction T1 between the two reads. Hence transaction T
receives different values for its two reads of the same item. This may
occur,forexample,ifduringanairlinereservationtransaction,acustomerinquires
about seat availability on several flights. When the customer decides on a
particular flight, the transaction then reads the number of seats on that flight a
second time before completing the reservation, and it may end up reading a
different value for the item.

3) Explainwhyrecoveryisneeded.OR
Explainthedifferenttypesoftransactionfailures.
Transaction failures: Whenever the transaction is submitted to DBMS for execution
the system is responsible for making sure that either (a) all operations in transaction are
completed successfullyand their effect is recorded permanentlyin the database or (b) the
transaction has no effect on the database or on any other transactions.
The DBMS must not permit some operations of transaction T to be applied to the
database while other operations of T are not. This may happen if transaction fails after
executing some of its operations but before executing all of them.
Types of Failures: Transaction fail in the middle of the execution for many reasons
as explained below.
1. Computer failure (System crash): A hardware or software error occurs in the
computersystem duringtransactionexecution. Ifthehardwarecrashes the contentsof the
computer internal memory may be lost.
2. A transaction or system error: Some operation in the transaction may cause it to
fail, such as integer overflow or division by zero. Transaction failure may also occur
because of logical programming error. In addition the user may interrupt the
transaction during its execution.
3. Local error or exception conditions detected by the transaction: During
transaction execution certain condition may occur that necessitate the cancellation of
the transaction. For example, data for the transaction may not be found.
4. Concurrency control enforcement : Theconcurrencycontrol method maydecide to
abort the transaction, to restarted later( due to deadlock )
5. Disk failure: Some disk blocks may lose their data because of a read or write
malfunction or because of disk read/write head crash.
6. Physical problem and catastrophes: This refers to an endless list of problems that
includes power failure, fire, theft, overwriting disk or tape by mistake etc.

Failures of types 1, 2, 3, and 4 are more common than those of types 5 or 6. Whenever a
failure of type 1 through 4 occurs, the system must keep sufficient information to
quickly recover from the failure. Disk failure or other catastrophic failures of type 5 or 6
do not happen frequently; if they do occur, recovery is a major task.

4) Explain the operations recovery manager keeps track of for recovery


purpose.
For recovery purpose, the system needs to keep track of when the transaction starts,
terminates, and commits or aborts. Hence the recovery manager keeps track of the
following operations.
 BEGIN_TRANSACTION:Thismarksthebeginningofthetransaction execution.
 READ or WRITE: These specify read or write operation on the database items that
are executed as a part of transaction.
 END_TRANSACTION: This specifies READ and WRITE transaction operations
have ended and marks the end limit of transaction execution. At this point it maybe
necessary to check whether the changes introduced by the transaction can be
permanently applied to the database (committed) or whether the transaction has to be
aborted because it violates concurrencycontrolor for some other reason.
 COMMIT_TRANSACTION: This signals the successful end of the transaction so
that any changes executed by the transaction can be safely committed to the database
and will not be undone.
 ROLLBACK (or ABORT):This indicates that the transaction hasended
unsuccessfully,sothatanychangesor effectsthatthetransactionmayhaveapplied to the
database must be undone.
5) With a neat state transition diagram, explain the different states of a
transaction.

Figure:Statetransitiondiagramoftransaction

Atransactiongoes intoan activestateimmediatelyafteritstartsexecution,whereitissue Read


or Write operations. When the transaction ends it moves to a partially committed
state. At this point some concurrency control techniques require that certain checks be
made to ensure that the transaction did not interferewith other executingtransactions and
some Recovery protocols need to ensure that a system failure will not result in
aninability to record the changes of the transaction permanently. Once both checks are
successful, the transaction is said to reach its commit point and enters the committed
state.
Howeverthetransaction can go to the failed stateifoneofthechecks fails orifit is
aborted during its active state. The terminated state corresponds to the transaction
leaving the system. The transaction information that is maintained in system tables
while the transaction has been running is removed when the transaction terminates.
6) Explain the entries in System Log, Commit point of transaction, and
checkpoints in System Log.
TheSystemLog:Toabletorecoverfromtransactionfailures,thesystem maintainsa log.
The log keeps track of all transaction operations that affect the value of database items.
The log is kept on disk so, it is not affected by the any type of failure except for disk or
catastrophic failure. Some of the log entries are
1. [start_transaction,T]:RecordsthatthetransactionThasstartedexecution
2. [write_item,T,old_value, new_value]:RecordsthattransactionThaschangedthe value of
database itemX from old_value to new_value .
3. [read_item,T,X]:RecordsthattransactionThasreadthevalueofdatabase itemX.
4. [commit,T]:RecordsthattransactionThascompletedsuccessfully,andindicates thatits
effect can be committed ( recorded permanently)to the database.
5. [abort,T]:RecordsthatthetransactionThasaborted.

Commit Point of a Transaction:A transaction T reaches its commit point when all
its operations that access the database have been executed successfully and the effect of
all the transaction operations on the database has been recorded in the log. Beyond the
commit point, the transaction is said to be committed, and its effect is assumed to be
permanently recorded in the database. The committed transaction writes an entry
[commit, T] into the log. If a system failure occurs, we search back in the log for all
transaction T that have written a [start_transaction, T] entry into the log but have not
written their [commit, T] entry yet.
The transactions that have written their commit entry in the log must also have recorded
all their WRITE operations in the log otherwise they could not be committed.

Checkpoints in the System Log:Checkpoint one type of entry in the log. A


[checkpoint] record is written in to the log, periodically at the point when the system
writes out to the database on the disk, the effect of all WRITE operations of committed
transactions. Hence all transactions that have their [commit, T] entries in the log before
a [checkpoint] entry do not need to have their WRITE operations redone in case of
system crash.
Takingcheckpointconsistsofthefollowingactions.
1. Suspendexecutionoftransactiontemporarily
2. Force-writeallupdateoperationsofcommittedtransactionsfrommainmemorybuffer to
disk.
3. Writea[checkpoint]recordtothelog,andforce-writethelogto disk.
4. Resumeexecutingtransactions.
Therecoverymanager ofDBMSmust decideat whatintervalsto takeacheckpoint.
A check point may also include the additional information , such as list of active
transaction ids, and the locations (address) of the first and most recent records in the log
for each active transaction.

7) ExplainthedesirablepropertiesofTransaction.
Atomic transactions should possess several properties. These properties are often called
the ACID Properties. The following are the ACID properties.
1. Atomicity: A transaction is an atomic unit of processing; it is either performed in its
entirety (whole) or not performed at all. The atomicity property requires that we
execute a transaction to completion. It is the responsibility of the recovery method to
ensure atomicity.
2. Consistency preservation: A correct execution of the transaction must take the
database from one consistent state to another. The consistency preservation propertyis
generally considered to be the responsibility of the programmers, to enforce the
integrity constraints.
3. Isolation:Atransaction shouldnotmakeits updatesvisibletoothertransactionsuntil it is
committed; this property, when enforced strictly, solves the temporary update
problem. Isolation is enforced by the concurrency control method.
A transaction is said to have degree 0(zero) isolation, if it does not overwrite the dirty
reads of higher degree transactions.
Adegree1(one)isolation hasnolostupdates:
Degree2(two)isolationhas nolost updates andno dirtyreads
degree3( three)isolation (also called true isolation) has , degree two properties and
repeatable reads.
4. Durability or Permanency: Once a transaction changes the database and thechanges
are committed, these changes must never be lost because of subsequent failure. The
durability property is the responsibility ofthe recovery method

8) ExplainSchedule,CompleteSchedule,Conflict.
Whenthetransactionsareexecutingconcurrentlyinaninterleavedfashion,theorderofexecution of
operations from the various transactions forms what is known as transaction Schedule or
History.
A Schedule S ofntransactions T1, T2, T3, . . . . . . Tnis an ordering of the operations of the
transactions subject to the constraintthat, for each transactionTithat participates inS , the
operations of Tiin S must appear in the same order in which they occur inTi.
AscheduleSinvolvingtwotransactionscanbewrittenas S:
r1(X); r2(X); w1(X); r1(Y); w2(X); c2; w1(Y); c1;
Whererreadoperationwwriteoperationc
commit

CompleteSchedule:AscheduleSofntransactionsT1,T2, ....................... ,Tnissaidtobeacomplete


scheduleifthefollowingconditionshold:
1. Theoperations in S areexactlythoseoperationsinT1,T2,. . . ..Tn ,includinga commit or abort
operation as the last operation for each transaction in the schedule.
2. For anypair of operationsfromthe same transactionTitheir order ofappearance in S is the same
as their order of appearance inTi
3. Foranyconflictingoperations,oneofthetwomust occurbeforetheotherintheschedule.

Conflict:Twooperationsinaschedulearesaidtobeconflictiftheybelongtodifferenttransactions, if they access


the same item X, and if one of the operationsisa write_item(X).

9) ExplaintheSchedulescharacterizedbasedonRecoverability.
For some schedules it is easy to recover from transaction failures, where as for other
schedules the recovery process can be quite involved. Hence it is important to
characterize the types of schedules for which recovery is possible, as well as for which
recovery is simple. Based on the recoverability we have
1. Recoverable Schedules: AscheduleS issaid to be recoverable if no transaction T
inScommits until all other transactionthat have written an item, thatT reads have
committed. For example S: r1(X); w1(X); r2(X); r1(Y); w2(X); c2;a1this is not a
recoverabletransaction, because T2 readsitem X fromT1, and then T2 commits before
T 1 commits .
2. Avoid cascading rollback Schedule:A schedule is said to avoid cascading rollback if
every transaction in the schedule only reads items that have written by committed
transactions.
3. Strictschedule:Inthistypeofschedulethetransactionscanneither read norwritean item X
until the last transaction that wrote X has committed or aborted. Strict schedules
simplify the process of recovering write operations to a matter of restoring the before
image of a data item X, which is the value that X had prior to the aborted write
operation.
10) WriteashortnoteonTransactionsupportinSQL.
The basic definition of an SQL transaction is similar to our already defined
concept of a transaction. That is, it is a logical unit of work and is guaranteed to be
atomic.
AsingleSQLstatement is always considered to beatomic—either it
completesexecution without an error or it fails and leaves the database unchanged.
With SQL, there is no explicit Begin_Transaction statement. Transaction initiation
isdone implicitly when particular SQL statements are encountered. However,
every transaction must have an explicit end statement, which is either a COMMIT
or a ROLLBACK.
Every transaction has certain characteristics attributed to it. These characteristics
are specified by a SET TRANSACTION statement in SQL.
Thecharacteristicsaretheaccess mode,thediagnosticareasize, andthe isolation
level. The access mode can be specified as READ ONLY or READ WRITE. The
default is READ WRITE, unless the isolation level of READ UNCOMMITTED
is specified (see below), in which case READ ONLY is assumed. A mode of
READ WRITE allowsselect, update, insert, delete, and create commands to be
executed. A mode of READ ONLY, as the name implies, is simply for data
retrieval.
The diagnostic area size option, DIAGNOSTIC SIZE n, specifies an integer
value n, which indicates the number of conditions that can be held simultaneously
in the diagnostic area. These conditions supply feedback information (errors or
exceptions) to the user or program on the n most recently executed SQL
statement. The isolation level option is specified using the statement ISOLATION
LEVEL, where the value for can be
READUNCOMMITTED,READCOMMITTED,REPEATABLEREAD,or
SERIALIZABLE. The default isolation level is SERIALIZABLE, although
somesystems use READ COMMITTED as their default.
If a transaction executes at a lower isolation level than SERIALIZABLE, then one
or more of the following three violations may occur:
1. Dirtyread.AtransactionT1mayreadtheupdateofatransactionT2,whichhasnot yet
committed. If T2 fails and is aborted, then T1 would have read a value that does not
exist and is incorrect.
2. Nonrepeatable read. A transaction T1 may read a given value from a table. If
another transaction T2 later updates that value and T1 reads that value again, T1 will
see adifferent value.
3. Phantoms. A transaction T1 may read a set of rows from a table perhaps based
on some condition specified in the SQL WHERE-clause. Now suppose that a
transaction T2
inserts a new row that also satisfies the WHERE-clause condition used in T1, into the table used by
T1. If T1 is repeated, then T1 will see a phantom, a row that previously did not exist.
Table 1 summarizes the possible violations for the different isolation levels. An
entry of Yes indicates that a violation is possible and an entry of No indicates that
it is not possible. READ UNCOMMITTED is the most forgiving, and
SERIALIZABLE is the most restrictive in that it avoids all three of the problems
mentioned above.
Table1:Possibleviolationsbasedon IsolationlevelsasdefinedinSQL.

You might also like

pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy