SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
Chap 3. PL/SQL Security         1



                               3. PL/SQL SECURITY
1.   What are the different types of transactions in Oracle?
     1. An Oracle table can be manipulated through SQL or PL/SQL statements.
     2. An Oracle transaction can be made up of a single SQL sentence or several SQL sentences.
     3. This gives rise to Single Query Transactions (SQT) and Multiple Query Transactions
        (MQT).
     4. Both these transactions access Oracle tables. Since Oracle works on a multi-user platform,
        it is likely that many users will access the data either for viewing or for manipulating it (by
        using UPDATE, DELETE, INSERT commands) from the same table at the same time.
        Thus, the Oracle table is a global resource i.e., it is shared by many users.

2.   What is concurrency control and how is it implemented?
     1. An Oracle table can be manipulated through SQL or PL/SQL statements.
     2. These transactions can be Single Query Transactions (SQT) or Multiple Query
        Transactions (MQT). These transactions access Oracle tables.
     3. Since Oracle works on a multi-user platform, it is likely that many users will access the
        data either for viewing or for manipulating it (by using UPDATE, DELETE, INSERT
        commands) from the same table at the same time. Thus, the Oracle table is a global
        resource i.e., it is shared by many users.
     4. Therefore it is essential that data integrity is maintained and Oracle should permit
        simultaneous access to the tables without causing damage to the data.
     5. The technique employed by Oracle to protect table data when several people are accessing
        it is called Concurrency Control. This technique is implemented through a method called
        locking.
     6. A lock is a variable associated with a data item that describes the status of the item with
        respect to the various operations that can be performed on it. Generally, there is one lock
        for each data item in a database.

 3   What are locks? Explain Oracle’s default locking strategy (Implicit locking).
     1. Oracle is a multi-user system and several users may access a table at the same time for
        either viewing it or for manipulating it through INSERT, UPDATE and DELETE
        commands.
     2. In order to maintain the integrity of the database, Oracle uses a technique of concurrency
        control through the mechanism of locking.
     3. Thus, locks are mechanisms to ensure data integrity while allowing maximum concurrent
        access to data.
     4. This locking mechanism is fully automatic and does not require user intervention.
     5. The Oracle engine automatically locks table data while executing SQL statements. This
        type of locking is called Implicit locking.

     Implicit Locking:
     Implicit Locking is Oracle’s default locking strategy. Oracle has to decide on two issues:
        1. Types of locks to be applied,
        2. Level of lock to be applied.

     Types of Locks:
     The type of lock to be placed on a resource depends on the operation being performed on that
     resource. The two types of operations on tables are:

mukeshtekwani@hotmail.com                                                  Prof. Mukesh N. Tekwani
2    Chap 3. PL/SQL Security


             • Read Operations (SELECT statement)
             • Write Operations (INSERT, UPDATE, DELETE statements)
         Read operations do not change the underlying table and hence simultaneous read operations
         can be performed on a table without causing any damage to the table data. Oracle engine
         places a Shared lock on a table when its data is being viewed.

         Write operations cause a change in table data due to the INSERT, UPDATE and DELETE
         commands. Hence, simultaneous write operations can adversely affect the table integrity.
         Simultaneous write operations will cause loss of data consistency in the table. Therefore,
         Oracle places an Exclusive lock on a table or parts of a table when data is being written in a
         table.

         The rules for locking are as follows:
         • Data that is being changed cannot be read (write and read cannot be performed
            concurrently)
         • Writers wait for other writers if they attempt to update the same rows at the same time.

         Types of Locks supported by Oracle are:
         Shared Locks:
         • This lock is placed on a resource whenever a read operation is being performed.
         • Multiple shared locks can be placed simultaneously on a resource.

         Exclusive Locks:
         • This lock is placed on a resource whenever a write operation
            (INSERT/UPDATE/DELETE) is being performed.
         • Only one exclusive lock can be placed on a resource at a time. The first user who acquires
            an exclusive lock will continue to be the sole owner of the resource and no other user can
            acquire an exclusive lock on that resource.

         Levels of Locks:
         A table can be decomposed into rows and a row can be decomposed into fields. So we can
         design a locking system which can lock the individual fields of a record. Thus, more than one
         user can work on a single record in a table – i.e., each user can be working on a different field
         of the same record, at the same time. However, Oracle does not provide a field level lock.

         The three levels of locking provided by Oracle are:
         • Row level
         • Page level
         • Table level

         The level of locking to be used is decided by the Oracle engine; this depends on the presence
         or absence of the WHERE condition in the SQL statement. The rules are as follows:
         • If the WHERE clause evaluates to only a single row in the table, a row level lock is used.
         • If the WHERE clause evaluates to a set of data, a page level lock is used.
         • If there is no WHERE clause (i.e., the entire table is being accessed by the query), a table
             level lock is used.




    Prof. Mukesh N Tekwani                                              mukeshtekwani@hotmail.com
Chap 3. PL/SQL Security         3


 4   What is Exclusive locking? What is the need for exclusive locking? What are the various
     techniques for exclusive locking?
     Exclusive Locks:
     • This lock is placed on a resource whenever a write operation
         (INSERT/UPDATE/DELETE) is being performed.
     • Only one exclusive lock can be placed on a resource at a time. The first user who acquires
         an exclusive lock will continue to be the sole owner of the resource and no other user can
         acquire an exclusive lock on that resource.

     Need for exclusive locking:
     Although the Oracle engine has a default locking strategy, explicit locking is often needed.
     The following example explains the need for explicit locking:
     Consider two client computers (Client A and Client B) are entering sales orders. Each time a
     sales order is prepared, the QOH of the product must be updated in the PRODUCTS_MSTR
     table. If client A fires an UPDATE command on a record of the PRODUCTS_MSTR table,
     then Oracle will implicitly lock the record so that no further data manipulation can be done by
     any other user till the lock is released. The lock will be released only when Client A fires a
     COMMIT or a ROLLBACK command.

     In the mean time, if Client B tries to view the same record, the Oracle engine will display the
     old data for the record as the transaction for that record has not been completed by Client A.
     Thus, Client B is viewing wrong information.

     In such a situation, Client A must explicitly lock the record so that no other user can access the
     record even for viewing purposes till client A completes his transaction.

     Such a lock that is applied by the user on a row, page or the entire table is called an explicit
     lock. Explicit locking is user-defined strategy and it always overrides Oracle’s default locking
     strategy.

     Tables and rows can be explicitly locked by using either the SELECT…FOR UPDATE
     statement or the LOCK TABLE statement.

     The SELECT…FOR UPDATE statement:
     This statement is used for acquiring exclusive row-level locks. This statement informs the
     Oracle engine that data currently being used needs to be updated. It is followed by UPDATE
     statements with a WHERE clause.

     Example 1:
     Two clients A and B are recording the transactions performed in a bank for a particular
     account number simultaneously.
     Client A fires the statement:
     Client A> SELECT * FROM ACCT_MSTR WHERE ACCT_NO = ‘SB7’ FOR UPDATE;

     When the above SELECT statement is fired, Oracle’s engine locks the record SB7. This lock
     will be released only when Client A issues the COMMIT or ROLLBACK command.

     Now Client B fires a SELECT statement which points to record SB7 which has already been
     locked by Client A.
     Client B> SELECT * FROM ACCT_MSTR WHERE ACCT_NO = ‘SB7’ FOR UPDATE;

mukeshtekwani@hotmail.com                                                  Prof. Mukesh N. Tekwani
4    Chap 3. PL/SQL Security


         The Oracle engine ensures that Client B’s SQL statement waits for lock to be released on
         ACCT_MSTR by a COMMIT or ROLLBACK statement. In order to avoid unnecessary
         waiting time, a NOWAIT option can be used to inform Oracle engine to terminate the SQL
         command if the record has already been locked. In this case Oracle engine generates a
         message for Client B stating that the resource is busy. So in order not to wait for an uncertain
         period, Client B can issue the following query:

         Client B> SELECT * FROM ACCT_MSTR WHERE ACCT_NO = ‘SB7’ FOR UPDATE
         NOWAIT;

         The SELCT… FOR UPDATE clause cannot be used with the following:
            • DISTINCT and GROUP BY clauses.
            • SET OPERATORS and GROUP functions.

         Using the LOCK TABLE statement:
         Oracle’s default locking strategy can be overridden by creating a lock in a specific mode. This
         is done with the LOCK TABLE clause:

         LOCK TABLE <tablename> [,<tablename>]…
              In {ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE | SHARE | SHARE ROW
              EXCLUSIVE | EXCLUSIVE }
              [NOWAIT ]

         In this syntax:
         • <tablename> indicates the names of the tables / views to be locked.
         • IN decides what other locks on the same resource can exist simultaneously. E.g., if there is
             an exclusive lock on the table, no other user can update rows on the table. It can have the
             following values:
                  o EXCLUSIVE – Query is allowed on the locked resource but other activities are not
                     permitted
                  o SHARE – Queries are allowed on the table but does not allow updates on the table
                  o ROW EXCLUSIVE – this lock is acquired when using the commands UPDATE,
                     INSERT, or DELETE.
                  o SHARE EXCLUSIVE – they are used to look at the whole table, to carry out
                     selective updates, and to allow other users to look at rows in the table but not lock
                     the table or update the rows.
         • NOWAIT – Indicates that the Oracle engine should immediately return to the user with a
             message, if the resources are busy. If this clause is omitted the Oracle engine will wait till
             the resources available.

         RELEASING LOCKS:
         Locks are released under the following circumstances:
         • The transaction is completed successfully using the COMMIT verb.
         • A ROLLBACK is performed.
         • A ROLLBACK to a savepoint will release locks after the specific savepoint. But row-level
            locks are not released by rolling back to a savepoint.

     5   What are the important properties of a transaction that a DBMS must ensure?
         There are four important properties of transactions that a DBMS must ensure to
         maintain data in the face of concurrent access and system failures:
    Prof. Mukesh N Tekwani                                               mukeshtekwani@hotmail.com
Chap 3. PL/SQL Security         5


     1. Users should be able to regard the execution of each transaction as atomic: either all
        actions are carried out or none are. Users should not have to worry about the effect of
        incomplete transactions (say, when a system crash occurs).
     2. Each transaction, run by itself with no concurrent execution of other transactions, must
        preserve the consistency of the database. This property is called consistency, and the
        DBMS assumes that it holds for each transaction. Ensuring this property of a transaction is
        the responsibility of the user.
     3. Users should be able to understand a transaction without considering the effect of other
        concurrently executing transactions, even if the DBMS interleaves the actions of several
        transactions for performance reasons. This property is sometimes referred to as isolation:
        Transactions are isolated, or protected, from the effects of concurrently scheduling other
        transactions.
     4. Once the DBMS informs the user that a transaction has been successfully completed, its
        effects should persist even if the system crashes before all its changes are reflected on disk.
        This property is called durability.

     The acronym ACID is sometimes used to refer to the four properties of transactions that we
     have presented here: atomicity, consistency, isolation and durability.

 6   Write a simple program to increase the salary of employees whose salaries are less than
     the average salary by 10 percent. The program re-computes and prints out the average
     salary if it exceeds 50000 after the above update.
     DECLARE
            avg_salary NUMBER;
     BEGIN
            SELECT avg(salary) INTO avg_salary FROM employee;
            UPDATE employee SET salary = salary*1.1
                     WHERE salary < avg_salary;
            SELECT avg(salary) INTO avg_salary FROM employee;

            IF avg_salary > 50000 THEN
                   dbms_output.put_line (‘Average Salary is ‘ | | avg_salary);
            END IF;
     END;
     /


 7   Display the employee number for those employees whose salary is greater than their
     supervisor’s salary.
     DECLARE
            emp_salary NUMBER;
            emp_super_salary NUMBER;
            emp_ssn CHAR (9);
            emp_superssn CHAR (9);
            CURSOR salary_cursor IS SELECT ssn, salary, superssn FROM employee;

     BEGIN
          OPEN salary_cursor;
          LOOP
          FETCH salary_cursor INTO emp_ssn, emp_salary, emp_superssn;

mukeshtekwani@hotmail.com                                                  Prof. Mukesh N. Tekwani
6    Chap 3. PL/SQL Security


                EXIT WHEN salary_cursor%NOTFOUND;
                IF emp_superssn is NOT NULL THEN
                      SELECT salary INTO emp_super_salary FROM employee
                      WHERE ssn = emp_superssn;

                    IF emp_salary > emp_super_salary THEN
                          dbms_output.put_line(emp_ssn);
                    END IF;
              END IF;
         END LOOP;

         IF salary_cursor%ISOPEN THEN CLOSE salary_cursor;
         IF salary_cursor%ISOPEN THEN CLOSE salary_cursor;
         END;
         /


     8   Write a program segment that gets a list of all the employees, increments each
         employee’s salary by 10 percent, and displays the old and the new salary.
         DECLARE
               v_fname employee.fname%TYPE;
               v_minit employee.minit%TYPE;
               v_lname employee.lname%TYPE;
               v_address employee.address%TYPE;
               v_salary employee.salary%TYPE;

                CURSOR EMP IS SELECT ssn, fname, minit, lname, salary FROM employee;
                BEGIN
                     OPEN EMP;
                     LOOP
                          FETCH EMP INTO v_ssn, v_fname, v_minit, v_lname, v_salary;
                          EXIT WHEN EMP%NOTFOUND;
                          dbms_output.putline(‘SSN:’ | | v_ssn | | ‘Old salary :’ | | v_salary);
                     UPDATE employee SET salary = salary*1.1
                     WHERE ssn = v_ssn;

                     COMMIT;
                     dbms_output.putline(‘SSN:’ | | v_ssn | | ‘New salary :’ | | v_salary*1.1);
                     END LOOP;
                CLOSE EMP;
         END;
         /




    Prof. Mukesh N Tekwani                                             mukeshtekwani@hotmail.com

Mais conteúdo relacionado

Mais procurados

10g plsql slide
10g plsql slide10g plsql slide
10g plsql slideTanu_Manu
 
11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilarrehaniltifat
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals INick Buytaert
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureFrans Jongma
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSmohdoracle
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...rehaniltifat
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statmentsrehaniltifat
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsqlSid Xing
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....Abhiram Vijay
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Developmentrehaniltifat
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQLKailash N
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07AnusAhmad
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadatarehaniltifat
 
Oracle Baisc Tutorial
Oracle Baisc TutorialOracle Baisc Tutorial
Oracle Baisc Tutorialbunny0143
 

Mais procurados (19)

Pl sql chapter 1
Pl sql chapter 1Pl sql chapter 1
Pl sql chapter 1
 
10g plsql slide
10g plsql slide10g plsql slide
10g plsql slide
 
11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
 
Oracle etl openworld
Oracle etl openworldOracle etl openworld
Oracle etl openworld
 
SQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update featureSQL/MX 3.6 Select for update feature
SQL/MX 3.6 Select for update feature
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsql
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
 
SQL
SQLSQL
SQL
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 
Introduction to PL/SQL
Introduction to PL/SQLIntroduction to PL/SQL
Introduction to PL/SQL
 
[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07[Www.pkbulk.blogspot.com]dbms07
[Www.pkbulk.blogspot.com]dbms07
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 
Oracle Baisc Tutorial
Oracle Baisc TutorialOracle Baisc Tutorial
Oracle Baisc Tutorial
 

Semelhante a Pl sql-ch3

Locks with updt nowait
Locks with updt nowaitLocks with updt nowait
Locks with updt nowaitavniS
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3avniS
 
SQL locks-presentation
SQL locks-presentationSQL locks-presentation
SQL locks-presentationNuzhat Bhat
 
Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Antonios Chatzipavlis
 
Look inside the locking mechanism
Look inside the locking mechanismLook inside the locking mechanism
Look inside the locking mechanismLiron Amitzi
 
Understanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database SystemsUnderstanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database Systemskiansahafi
 
Database security
Database securityDatabase security
Database securityJaved Khan
 
Database consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXDatabase consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXFrans Jongma
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrencyRumeysaDinsoy
 
Less08 managing data and concurrency
Less08 managing data and concurrencyLess08 managing data and concurrency
Less08 managing data and concurrencyImran Ali
 
Sql server update-lock
Sql server update-lockSql server update-lock
Sql server update-lockShah Faisal
 
Oracle Security Presentation
Oracle Security PresentationOracle Security Presentation
Oracle Security PresentationFrancisco Alvarez
 
Oracle Diagnostics : Latches and Enqueues
Oracle Diagnostics : Latches and EnqueuesOracle Diagnostics : Latches and Enqueues
Oracle Diagnostics : Latches and EnqueuesHemant K Chitale
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Oliersqlserver.co.il
 
PL/SQL Interview Questions
PL/SQL Interview QuestionsPL/SQL Interview Questions
PL/SQL Interview QuestionsSrinimf-Slides
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock managementDAGEOP LTD
 

Semelhante a Pl sql-ch3 (20)

Locks with updt nowait
Locks with updt nowaitLocks with updt nowait
Locks with updt nowait
 
Locking unit 1 topic 3
Locking unit 1 topic 3Locking unit 1 topic 3
Locking unit 1 topic 3
 
SQL locks-presentation
SQL locks-presentationSQL locks-presentation
SQL locks-presentation
 
Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)
 
Look inside the locking mechanism
Look inside the locking mechanismLook inside the locking mechanism
Look inside the locking mechanism
 
Understanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database SystemsUnderstanding Locking Mechanisms in Database Systems
Understanding Locking Mechanisms in Database Systems
 
Locking And Concurrency
Locking And ConcurrencyLocking And Concurrency
Locking And Concurrency
 
Database security
Database securityDatabase security
Database security
 
Database consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MXDatabase consistency in NonStop SQL/MX
Database consistency in NonStop SQL/MX
 
Locking and concurrency
Locking and concurrencyLocking and concurrency
Locking and concurrency
 
Less08 managing data and concurrency
Less08 managing data and concurrencyLess08 managing data and concurrency
Less08 managing data and concurrency
 
01 oracle architecture
01 oracle architecture01 oracle architecture
01 oracle architecture
 
Locking in SQL Server
Locking in SQL ServerLocking in SQL Server
Locking in SQL Server
 
Sql server update-lock
Sql server update-lockSql server update-lock
Sql server update-lock
 
Oracle Security Presentation
Oracle Security PresentationOracle Security Presentation
Oracle Security Presentation
 
Sql server concurrency
Sql server concurrencySql server concurrency
Sql server concurrency
 
Oracle Diagnostics : Latches and Enqueues
Oracle Diagnostics : Latches and EnqueuesOracle Diagnostics : Latches and Enqueues
Oracle Diagnostics : Latches and Enqueues
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
 
PL/SQL Interview Questions
PL/SQL Interview QuestionsPL/SQL Interview Questions
PL/SQL Interview Questions
 
Managing Memory & Locks - Series 2 Transactions & Lock management
Managing  Memory & Locks - Series 2 Transactions & Lock managementManaging  Memory & Locks - Series 2 Transactions & Lock management
Managing Memory & Locks - Series 2 Transactions & Lock management
 

Mais de Mukesh Tekwani

ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - PhysicsMukesh Tekwani
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion Mukesh Tekwani
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Mukesh Tekwani
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversionMukesh Tekwani
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion Mukesh Tekwani
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversionMukesh Tekwani
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Mukesh Tekwani
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prismMukesh Tekwani
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surfaceMukesh Tekwani
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomMukesh Tekwani
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesMukesh Tekwani
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEMukesh Tekwani
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference ModelMukesh Tekwani
 

Mais de Mukesh Tekwani (20)

Circular motion
Circular motionCircular motion
Circular motion
 
Gravitation
GravitationGravitation
Gravitation
 
ISCE-Class 12-Question Bank - Electrostatics - Physics
ISCE-Class 12-Question Bank - Electrostatics  -  PhysicsISCE-Class 12-Question Bank - Electrostatics  -  Physics
ISCE-Class 12-Question Bank - Electrostatics - Physics
 
Hexadecimal to binary conversion
Hexadecimal to binary conversion Hexadecimal to binary conversion
Hexadecimal to binary conversion
 
Hexadecimal to decimal conversion
Hexadecimal to decimal conversion Hexadecimal to decimal conversion
Hexadecimal to decimal conversion
 
Hexadecimal to octal conversion
Hexadecimal to octal conversionHexadecimal to octal conversion
Hexadecimal to octal conversion
 
Gray code to binary conversion
Gray code to binary conversion Gray code to binary conversion
Gray code to binary conversion
 
What is Gray Code?
What is Gray Code? What is Gray Code?
What is Gray Code?
 
Decimal to Binary conversion
Decimal to Binary conversionDecimal to Binary conversion
Decimal to Binary conversion
 
Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21Video Lectures for IGCSE Physics 2020-21
Video Lectures for IGCSE Physics 2020-21
 
Refraction and dispersion of light through a prism
Refraction and dispersion of light through a prismRefraction and dispersion of light through a prism
Refraction and dispersion of light through a prism
 
Refraction of light at a plane surface
Refraction of light at a plane surfaceRefraction of light at a plane surface
Refraction of light at a plane surface
 
Spherical mirrors
Spherical mirrorsSpherical mirrors
Spherical mirrors
 
Atom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atomAtom, origin of spectra Bohr's theory of hydrogen atom
Atom, origin of spectra Bohr's theory of hydrogen atom
 
Refraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lensesRefraction of light at spherical surfaces of lenses
Refraction of light at spherical surfaces of lenses
 
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGEISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
ISCE (XII) - PHYSICS BOARD EXAM FEB 2020 - WEIGHTAGE
 
Cyber Laws
Cyber LawsCyber Laws
Cyber Laws
 
XML
XMLXML
XML
 
Social media
Social mediaSocial media
Social media
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 

Último

AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 

Último (20)

YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 

Pl sql-ch3

  • 1. Chap 3. PL/SQL Security 1 3. PL/SQL SECURITY 1. What are the different types of transactions in Oracle? 1. An Oracle table can be manipulated through SQL or PL/SQL statements. 2. An Oracle transaction can be made up of a single SQL sentence or several SQL sentences. 3. This gives rise to Single Query Transactions (SQT) and Multiple Query Transactions (MQT). 4. Both these transactions access Oracle tables. Since Oracle works on a multi-user platform, it is likely that many users will access the data either for viewing or for manipulating it (by using UPDATE, DELETE, INSERT commands) from the same table at the same time. Thus, the Oracle table is a global resource i.e., it is shared by many users. 2. What is concurrency control and how is it implemented? 1. An Oracle table can be manipulated through SQL or PL/SQL statements. 2. These transactions can be Single Query Transactions (SQT) or Multiple Query Transactions (MQT). These transactions access Oracle tables. 3. Since Oracle works on a multi-user platform, it is likely that many users will access the data either for viewing or for manipulating it (by using UPDATE, DELETE, INSERT commands) from the same table at the same time. Thus, the Oracle table is a global resource i.e., it is shared by many users. 4. Therefore it is essential that data integrity is maintained and Oracle should permit simultaneous access to the tables without causing damage to the data. 5. The technique employed by Oracle to protect table data when several people are accessing it is called Concurrency Control. This technique is implemented through a method called locking. 6. A lock is a variable associated with a data item that describes the status of the item with respect to the various operations that can be performed on it. Generally, there is one lock for each data item in a database. 3 What are locks? Explain Oracle’s default locking strategy (Implicit locking). 1. Oracle is a multi-user system and several users may access a table at the same time for either viewing it or for manipulating it through INSERT, UPDATE and DELETE commands. 2. In order to maintain the integrity of the database, Oracle uses a technique of concurrency control through the mechanism of locking. 3. Thus, locks are mechanisms to ensure data integrity while allowing maximum concurrent access to data. 4. This locking mechanism is fully automatic and does not require user intervention. 5. The Oracle engine automatically locks table data while executing SQL statements. This type of locking is called Implicit locking. Implicit Locking: Implicit Locking is Oracle’s default locking strategy. Oracle has to decide on two issues: 1. Types of locks to be applied, 2. Level of lock to be applied. Types of Locks: The type of lock to be placed on a resource depends on the operation being performed on that resource. The two types of operations on tables are: mukeshtekwani@hotmail.com Prof. Mukesh N. Tekwani
  • 2. 2 Chap 3. PL/SQL Security • Read Operations (SELECT statement) • Write Operations (INSERT, UPDATE, DELETE statements) Read operations do not change the underlying table and hence simultaneous read operations can be performed on a table without causing any damage to the table data. Oracle engine places a Shared lock on a table when its data is being viewed. Write operations cause a change in table data due to the INSERT, UPDATE and DELETE commands. Hence, simultaneous write operations can adversely affect the table integrity. Simultaneous write operations will cause loss of data consistency in the table. Therefore, Oracle places an Exclusive lock on a table or parts of a table when data is being written in a table. The rules for locking are as follows: • Data that is being changed cannot be read (write and read cannot be performed concurrently) • Writers wait for other writers if they attempt to update the same rows at the same time. Types of Locks supported by Oracle are: Shared Locks: • This lock is placed on a resource whenever a read operation is being performed. • Multiple shared locks can be placed simultaneously on a resource. Exclusive Locks: • This lock is placed on a resource whenever a write operation (INSERT/UPDATE/DELETE) is being performed. • Only one exclusive lock can be placed on a resource at a time. The first user who acquires an exclusive lock will continue to be the sole owner of the resource and no other user can acquire an exclusive lock on that resource. Levels of Locks: A table can be decomposed into rows and a row can be decomposed into fields. So we can design a locking system which can lock the individual fields of a record. Thus, more than one user can work on a single record in a table – i.e., each user can be working on a different field of the same record, at the same time. However, Oracle does not provide a field level lock. The three levels of locking provided by Oracle are: • Row level • Page level • Table level The level of locking to be used is decided by the Oracle engine; this depends on the presence or absence of the WHERE condition in the SQL statement. The rules are as follows: • If the WHERE clause evaluates to only a single row in the table, a row level lock is used. • If the WHERE clause evaluates to a set of data, a page level lock is used. • If there is no WHERE clause (i.e., the entire table is being accessed by the query), a table level lock is used. Prof. Mukesh N Tekwani mukeshtekwani@hotmail.com
  • 3. Chap 3. PL/SQL Security 3 4 What is Exclusive locking? What is the need for exclusive locking? What are the various techniques for exclusive locking? Exclusive Locks: • This lock is placed on a resource whenever a write operation (INSERT/UPDATE/DELETE) is being performed. • Only one exclusive lock can be placed on a resource at a time. The first user who acquires an exclusive lock will continue to be the sole owner of the resource and no other user can acquire an exclusive lock on that resource. Need for exclusive locking: Although the Oracle engine has a default locking strategy, explicit locking is often needed. The following example explains the need for explicit locking: Consider two client computers (Client A and Client B) are entering sales orders. Each time a sales order is prepared, the QOH of the product must be updated in the PRODUCTS_MSTR table. If client A fires an UPDATE command on a record of the PRODUCTS_MSTR table, then Oracle will implicitly lock the record so that no further data manipulation can be done by any other user till the lock is released. The lock will be released only when Client A fires a COMMIT or a ROLLBACK command. In the mean time, if Client B tries to view the same record, the Oracle engine will display the old data for the record as the transaction for that record has not been completed by Client A. Thus, Client B is viewing wrong information. In such a situation, Client A must explicitly lock the record so that no other user can access the record even for viewing purposes till client A completes his transaction. Such a lock that is applied by the user on a row, page or the entire table is called an explicit lock. Explicit locking is user-defined strategy and it always overrides Oracle’s default locking strategy. Tables and rows can be explicitly locked by using either the SELECT…FOR UPDATE statement or the LOCK TABLE statement. The SELECT…FOR UPDATE statement: This statement is used for acquiring exclusive row-level locks. This statement informs the Oracle engine that data currently being used needs to be updated. It is followed by UPDATE statements with a WHERE clause. Example 1: Two clients A and B are recording the transactions performed in a bank for a particular account number simultaneously. Client A fires the statement: Client A> SELECT * FROM ACCT_MSTR WHERE ACCT_NO = ‘SB7’ FOR UPDATE; When the above SELECT statement is fired, Oracle’s engine locks the record SB7. This lock will be released only when Client A issues the COMMIT or ROLLBACK command. Now Client B fires a SELECT statement which points to record SB7 which has already been locked by Client A. Client B> SELECT * FROM ACCT_MSTR WHERE ACCT_NO = ‘SB7’ FOR UPDATE; mukeshtekwani@hotmail.com Prof. Mukesh N. Tekwani
  • 4. 4 Chap 3. PL/SQL Security The Oracle engine ensures that Client B’s SQL statement waits for lock to be released on ACCT_MSTR by a COMMIT or ROLLBACK statement. In order to avoid unnecessary waiting time, a NOWAIT option can be used to inform Oracle engine to terminate the SQL command if the record has already been locked. In this case Oracle engine generates a message for Client B stating that the resource is busy. So in order not to wait for an uncertain period, Client B can issue the following query: Client B> SELECT * FROM ACCT_MSTR WHERE ACCT_NO = ‘SB7’ FOR UPDATE NOWAIT; The SELCT… FOR UPDATE clause cannot be used with the following: • DISTINCT and GROUP BY clauses. • SET OPERATORS and GROUP functions. Using the LOCK TABLE statement: Oracle’s default locking strategy can be overridden by creating a lock in a specific mode. This is done with the LOCK TABLE clause: LOCK TABLE <tablename> [,<tablename>]… In {ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE } [NOWAIT ] In this syntax: • <tablename> indicates the names of the tables / views to be locked. • IN decides what other locks on the same resource can exist simultaneously. E.g., if there is an exclusive lock on the table, no other user can update rows on the table. It can have the following values: o EXCLUSIVE – Query is allowed on the locked resource but other activities are not permitted o SHARE – Queries are allowed on the table but does not allow updates on the table o ROW EXCLUSIVE – this lock is acquired when using the commands UPDATE, INSERT, or DELETE. o SHARE EXCLUSIVE – they are used to look at the whole table, to carry out selective updates, and to allow other users to look at rows in the table but not lock the table or update the rows. • NOWAIT – Indicates that the Oracle engine should immediately return to the user with a message, if the resources are busy. If this clause is omitted the Oracle engine will wait till the resources available. RELEASING LOCKS: Locks are released under the following circumstances: • The transaction is completed successfully using the COMMIT verb. • A ROLLBACK is performed. • A ROLLBACK to a savepoint will release locks after the specific savepoint. But row-level locks are not released by rolling back to a savepoint. 5 What are the important properties of a transaction that a DBMS must ensure? There are four important properties of transactions that a DBMS must ensure to maintain data in the face of concurrent access and system failures: Prof. Mukesh N Tekwani mukeshtekwani@hotmail.com
  • 5. Chap 3. PL/SQL Security 5 1. Users should be able to regard the execution of each transaction as atomic: either all actions are carried out or none are. Users should not have to worry about the effect of incomplete transactions (say, when a system crash occurs). 2. Each transaction, run by itself with no concurrent execution of other transactions, must preserve the consistency of the database. This property is called consistency, and the DBMS assumes that it holds for each transaction. Ensuring this property of a transaction is the responsibility of the user. 3. Users should be able to understand a transaction without considering the effect of other concurrently executing transactions, even if the DBMS interleaves the actions of several transactions for performance reasons. This property is sometimes referred to as isolation: Transactions are isolated, or protected, from the effects of concurrently scheduling other transactions. 4. Once the DBMS informs the user that a transaction has been successfully completed, its effects should persist even if the system crashes before all its changes are reflected on disk. This property is called durability. The acronym ACID is sometimes used to refer to the four properties of transactions that we have presented here: atomicity, consistency, isolation and durability. 6 Write a simple program to increase the salary of employees whose salaries are less than the average salary by 10 percent. The program re-computes and prints out the average salary if it exceeds 50000 after the above update. DECLARE avg_salary NUMBER; BEGIN SELECT avg(salary) INTO avg_salary FROM employee; UPDATE employee SET salary = salary*1.1 WHERE salary < avg_salary; SELECT avg(salary) INTO avg_salary FROM employee; IF avg_salary > 50000 THEN dbms_output.put_line (‘Average Salary is ‘ | | avg_salary); END IF; END; / 7 Display the employee number for those employees whose salary is greater than their supervisor’s salary. DECLARE emp_salary NUMBER; emp_super_salary NUMBER; emp_ssn CHAR (9); emp_superssn CHAR (9); CURSOR salary_cursor IS SELECT ssn, salary, superssn FROM employee; BEGIN OPEN salary_cursor; LOOP FETCH salary_cursor INTO emp_ssn, emp_salary, emp_superssn; mukeshtekwani@hotmail.com Prof. Mukesh N. Tekwani
  • 6. 6 Chap 3. PL/SQL Security EXIT WHEN salary_cursor%NOTFOUND; IF emp_superssn is NOT NULL THEN SELECT salary INTO emp_super_salary FROM employee WHERE ssn = emp_superssn; IF emp_salary > emp_super_salary THEN dbms_output.put_line(emp_ssn); END IF; END IF; END LOOP; IF salary_cursor%ISOPEN THEN CLOSE salary_cursor; IF salary_cursor%ISOPEN THEN CLOSE salary_cursor; END; / 8 Write a program segment that gets a list of all the employees, increments each employee’s salary by 10 percent, and displays the old and the new salary. DECLARE v_fname employee.fname%TYPE; v_minit employee.minit%TYPE; v_lname employee.lname%TYPE; v_address employee.address%TYPE; v_salary employee.salary%TYPE; CURSOR EMP IS SELECT ssn, fname, minit, lname, salary FROM employee; BEGIN OPEN EMP; LOOP FETCH EMP INTO v_ssn, v_fname, v_minit, v_lname, v_salary; EXIT WHEN EMP%NOTFOUND; dbms_output.putline(‘SSN:’ | | v_ssn | | ‘Old salary :’ | | v_salary); UPDATE employee SET salary = salary*1.1 WHERE ssn = v_ssn; COMMIT; dbms_output.putline(‘SSN:’ | | v_ssn | | ‘New salary :’ | | v_salary*1.1); END LOOP; CLOSE EMP; END; / Prof. Mukesh N Tekwani mukeshtekwani@hotmail.com