Wednesday 13 August 2014

Introduction to PL/SQL.

Let us know something about PL/SQL.

Procedural Language/Structured Query Language or PL/SQL is that part of RDBMS at which we do not pay much attention while studying. However, when it comes to develop project, it is the most important part and cannot be ignored. Using PL/SQL while designing database reduces the programmer's effort exponentially. Question arises, how? And the simple answer is, 'A major portion of programming dealing with DB is finished in the database designing phase.'
Let us consider a scenario, through which we can understand the importance of using PL/SQL.

Consider an example related to accounting, in which tax on salary is to be calculated. There exists a table named employee (in which the account details are stored, say, emp_id,name and salary). Now, on using conventional programming approach, the programmer will need to make a cursor (for which, JDBC-ODBC bridge,DSN etc), through which records of employee table would be extracted, necessary calculation would be performed on extracted data and the generated result i.e. tax would be inserted back in some table say Tax. And all this will be done by programmer.
Now let us have a look at approach in which PL/SQL is involved.While designing DB, few procedures would be made(say, calculateTax and populateTax), in which necessary calculations would be done and later, simply procedures would be called. Here the point to be observed is, making procedures and implementing necessary calculations in them, calling them, all these activities are being performed at DBMS and the programmer is far away from this.

Here's the code for above example.(according to MySQL)

Now let us look at few program units of PL/SQL
  • Procedures
  • Functions
  • Triggers
First program unit is Procedure. It is similar to functions (one which we use in programming) and are used for computations, however the difference between procedure and function is
  1. Function returns a value while procedure do not.
  2. Function can be used in SQL statements while procedures cannot.
Triggers. Basically they are stored procedures which get automatically invoked on occurrence of some specific event. For example, when the values of attribute Current Balance is to be altered on the occurrence of credit/debit events, Trigger is something which would be used. A procedure (more specifically, trigger) would be wired up with insert or delete event and as soon as the events would occur, the wired up stored procedure would be triggered. 

This was a brief introduction about PL/SQL. 

Please correct me if I got something wrong.

No comments:

Post a Comment