Powered By Blogger

Sunday 25 March 2012

Locks


                                       LOCKS

Locks are the mechanisms used to prevent destructive interaction between users accessing same resource simultaneously. Locks provides high degree of data concurrency.

TYPES

Ø  Row level locks
Ø  Table level locks

ROW LEVEL LOCKS

In the row level lock a row is locked exclusively so that other cannot modify the row until the transaction holding the lock is committed or rolled back. This can be done by using select..for update clause.

Ex:
    SQL> select * from emp where sal > 3000 for update of comm.;

TABLE LEVEL LOCKS

A table level lock will protect table data thereby guaranteeing data integrity when data is being accessed concurrently by multiple users. A table lock can be held in several modes.

Ø  Share lock
Ø  Share update lock
Ø  Exclusive lock

SHARE LOCK

A share lock locks the table allowing other users to only query but not insert, update or delete rows in a table. Multiple users can place share locks on the same resource at the same time.

Ex:
     SQL> lock table emp in share mode;
SHARE UPDATE LOCK

It locks rows that are to be updated in a table. It permits other users to concurrently query, insert , update or even lock other rows in the same table. It prevents the other users from updating the row that has been locked.

Ex:
     SQL> lock table emp in share update mode;          

EXCLUSIVE LOCK

Exclusive lock is the most restrictive of tables locks. When issued by any user, it allows the other user to only query. It is similar to share lock but only one user can place exclusive lock on a table at a time.

Ex:
     SQL> lock table emp in share exclusive mode;

NOWAIT

If one user locked the table without nowait then another user trying to lock the same table then he has to wait until the user who has initially locked the table issues a commit or rollback statement. This delay could be avoided by appending a nowait clause in the lock table command.

Ex:
     SQL> lock table emp in exclusive mode nowait.

DEADLOCK

A deadlock occurs when tow users have a lock each on separate object, and they want to acquire a lock on the each other’s object. When this happens, the first user has to wait for the second user to release the lock, but the second user will not release it until the lock on the first user’s object is freed. In such a case, oracle detects the deadlock automatically and solves the problem by aborting one of the two transactions.

Subqueries and Exists


                                   SUBQUERIES AND EXISTS
SUBQUERIES

Nesting of queries, one within the other is termed as a subquery.
A statement containing a subquery is called a parent query.
Subqueries are used to retrieve data from tables that depend on the values in the table itself.

TYPES

Ø  Single row subqueries
Ø  Multi row subqueries
Ø  Multiple subqueries
Ø  Correlated subqueries

SINGLE ROW SUBQUERIES

In single row subquery, it will return one value.

Ex:
    SQL> select * from emp where sal > (select sal from emp where empno = 7566);

     EMPNO   ENAME      JOB        MGR    HIREDATE    SAL   COMM     DEPTNO
    ---------- ---------- --------- ---------- ------------  ------- ---------- ----------
      7788         SCOTT   ANALYST    7566   19-APR-87   3000                    20
      7839         KING     PRESIDENT            17-NOV-81  5000                    10
      7902         FORD    ANALYST     7566   03-DEC-81   3000                    20

MULTI ROW SUBQUERIES

In multi row subquery, it will return more than one value. In such cases we should include operators like any, all, in or not in between the comparision operator and the subquery.

Ex:
     SQL> select * from emp where sal > any (select sal from emp where sal between 2500 and
             4000);

     EMPNO    ENAME      JOB      MGR     HIREDATE   SAL   COMM     DEPTNO
    ---------- ---------- --------- ---------- -----------   -------- ---------- ----------
      7566         JONES   MANAGER   7839 02-APR-81   2975                   20
      7788         SCOTT   ANALYST    7566 19-APR-87    3000                   20
      7839         KING     PRESIDENT          17-NOV-81   5000                   10
      7902         FORD    ANALYST     7566 03-DEC-81    3000                   20

       SQL> select * from emp where sal > all (select sal from emp where sal between 2500 and  
              4000);
  
     EMPNO      ENAME    JOB       MGR     HIREDATE     SAL    COMM  DEPTNO
     ---------- ---------- --------- ---------- -------------  ------ ---------- ----------
      7839         KING     PRESIDENT            17-NOV-81  5000                    10

MULTIPLE SUBQUERIES

There is no limit on the number of subqueries included in a where clause. It allows nesting of a query within a subquery.

Ex:
     SQL> select * from emp where sal = (select max(sal) from emp where sal < (select
              max(sal) from emp));

     EMPNO      ENAME   JOB      MGR       HIREDATE   SAL   COMM     DEPTNO
     ---------- ---------- --------- ---------- ------------  ------- ---------- ----------
      7788         SCOTT   ANALYST  7566    19-APR-87   3000                    20
     7902          FORD    ANALYST   7566    03-DEC-81   3000                    20

CORRELATED SUBQUERIES

A subquery is evaluated once for the entire parent statement where as a correlated subquery is evaluated once for every row processed by the parent statement.
Ex:
     SQL> select distinct deptno from emp e where 5 <= (select count(ename) from emp where
             e.deptno = deptno);
  
 DEPTNO
    ----------
        20
        30

EXISTS

Exists function is a test for existence. This is a logical test for the return of rows from a query.

Ex:
     Suppose we want to display the department numbers which has more than 4 employees.
     SQL> select deptno,count(*) from emp group by deptno having count(*) > 4;

   DEPTNO   COUNT(*)
   ---------    ----------
       20             5
       30             6

     From the above query can you want to display the names of employees?
      SQL> select deptno,ename, count(*) from emp group by deptno,ename having count(*) > 4;

     no rows selected

     The above query returns nothing because combination of deptno and ename never return     
     more than one count.
     The solution is to use exists which follows.
      
      SQL> select deptno,ename from emp e1 where exists (select * from emp e2
             where e1.deptno=e2.deptno group by e2.deptno having count(e2.ename) > 4) order by
             deptno,ename;
 
    DEPTNO   ENAME
     ---------- ----------
        20            ADAMS
        20            FORD
        20            JONES
        20            SCOTT
        20            SMITH
        30            ALLEN
        30            BLAKE
        30            JAMES
        30            MARTIN
        30            TURNER
        30            WARD

NOT EXISTS

SQL> select deptno,ename from emp e1 where not exists (select * from emp e2
        where e1.deptno=e2.deptno group by e2.deptno having count(e2.ename) > 4) order by
        deptno,ename;

   DEPTNO ENAME
    --------- ----------
       10             CLARK
       10             KING
       10             MILLER