Dbms Module 4
Dbms Module 4
Dbms Module 4
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.
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);
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.
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.
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:
Underscore(_)replacesasinglecharacter.
Example:Retrieve allemployeeswhosenamebeginswithletter A.
SELECTFname FROMEMPLOYEEWHERENameLIKE‘A%’;
Example:Retrieve allemployeeswho werebornduring 1960s.
SELECT Fname
FROMEMPLOYE
E
WHEREBdateLIKE‘ 196_’;
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:
Is/As
<declaration>
Begin
<SQL Statement>
Exception
End procedurename;
Student(usn:string,sname:string)
stu_cnt int;
begin
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.
OUT parameters are returned from the stored procedure; it assigns values to all OUT
parameters that the user can process
Example:
CREATE PROCEDURE
Addlnventory ( IN book_isbn
CHAR(lO),
IN addedQty INTEGER)
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);
Procedures without parameters are called static procedures and with parameters are called
dynamic procedures.
emp(Essn int)as
begin
eName varchar(20);
select fname into eName from employee where ssn=Essn and dno=5; dbms_output.put_line('
end emp;
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
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.
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.
Figure:Statetransitiondiagramoftransaction
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.
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;
Whererreadoperationwwriteoperationc
commit
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.