PLSQL
PLSQL
Contents
1. Advantages of PL-SQL ....................................................................................................................................... 2
2. Understanding PLSQL ....................................................................................................................................... 3
3. Conditional statements in PLSQL .................................................................................................................. 3
4. Looping statements in PLSQL ......................................................................................................................... 4
5. Functions ............................................................................................................................................................... 4
6. Procedures ............................................................................................................................................................ 5
7. Cursors ................................................................................................................................................................... 5
8. Exception Handling ............................................................................................................................................ 7
9. Guidelines for Avoiding and Handling PLSQL Errors and Exceptions .............................................. 7
10. Advantages of PLSQL Exceptions .............................................................................................................. 8
11. Summary of predefined PLSQL Exceptions............................................................................................ 8
12. Views ................................................................................................................................................................... 8
13. Advantages and disadvantages of views ................................................................................................. 9
14. Comparison between Views and Tables .................................................................................................. 9
15. Triggers............................................................................................................................................................ 10
16. Benefits and components of a trigger..................................................................................................... 10
2. Understanding PLSQL
Ans
1. PL/SQL combines the data-manipulating power of SQL with the processing power of procedural languages.
2. You can control program flow with statements like IF and LOOP.
3. As with other procedural programming languages, you can declare variables, define procedures and functions
and trap runtime errors.
4. PL/SQL lets you break down complex problems into easily readable procedural code, and reuse this code across
multiple platforms/applications.
5. When a problem can be solved through plain SQL, you can issue SQL commands directly inside your PL/SQL
programs, without learning new API`s.
6. PLSQL data types correspond with SQL column types, making it easy to interchange PLSQL variable with data
inside a table.
a. Decision making statements are those statements which are in charge of executing a statement out of multiple
given statements based on some condition.
b. The condition will return either true or false. Based on what the condition returns, the associated statement
is executed.
c. This can be logically implemented in PL/SQL block using decision making statements.
d. The decision-making statements in PL/SQL are of two types:
a. If Else statements
b. Case statement
e. IF Statement
a. The if statement, or the if...then statement can be used when there is only a single condition to be
tested.
b. If the result of the condition is TRUE then certain specified action will be performed otherwise if it is
FALSE then no action is taken and the control of program will just move out of the if code block.
f. IF..THEN..ELSE
a. Using this statement group we can specify two statements or two set of statements, dependent on a
condition such that when the condition is true then one set of statements is executed and if the
condition is false then the other set of statements is executed.
g. IF..THEN..ELSIF..ELSE
a. It is used to check multiple conditions. Sometimes it is required to test more than one condition in that
case if...then...else statement cannot be used.
b. For this purpose, if...then...elsif...else statement is suitable in which all the conditions are tested one
by one and whichever condition is found to be TRUE, that block of code is executed.
c. And if all the conditions result in FALSE then the else part is executed.
a. There may be a situation when you need to execute a block of code several number of times. In general,
statements are executed sequentially:
b. The first statement in a function is executed first, followed by the second, and so on.
c. Programming languages provide various control structures that allow for more complicated execution paths.
d. A loop statement allows us to execute a statement or group of statements multiple times and following is the
general form of a loop statement in most of the programming languages
e. Types of PLSQL Loops
a. Basic Loop
i. PL/SQL exit loop is used when a set of statements is to be executed at least once before the
termination of the loop.
ii. There must be an EXIT condition specified in the loop, otherwise the loop will get into an
infinite number of iterations.
iii. After the occurrence of EXIT condition, the process exits the loop.
b. While Loop
i. PL/SQL while loop is used when a set of statements has to be executed as long as a condition
is true, the While loop is used.
ii. The condition is decided at the beginning of each iteration and continues until the condition
becomes false.
c. For Loop
i. PL/SQL for loop is used when when you want to execute a set of statements for a
predetermined number of times.
ii. The loop is iterated between the start and end integer values.
iii. The counter is always incremented by 1 and once the counter reaches the value of end integer,
the loop ends.
d. Cursor For Loop
i. Covered in Cursor Section
5. Functions
Ans
6. Procedures
Ans
7. Cursors
Ans
a. PLSQL uses implicit and explicit cursors. PLSQL declares a cursor implicitly for all SQL data manipulation
statements, including queries that return only one row.
b. If you want precise control over query processing, you can declare an explicit cursor in the declarative part of
any PLSQL block, subprogram, or package.
c. You must declare an explicit cursor for queries that return more than one row.
d. Implicit cursors
8. Exception Handling
Ans
a. Because reliability is crucial for database programs, use both error checking and exception handling to ensure
your program can handle all possibilities:
a. Add exception handlers whenever there is any possibility of an error occurring. Errors are especially
likely during arithmetic calculations, string manipulation, and database operations. Error could also
occur at other times.
b. Add error-checking code whenever you can predict that an error might occur if your code gets bad
input data. Except that at some time, your code will be passed incorrect or null parameters, that your
queries will return no rows or more rows than you except
c. Make your programs robust enough to work even if the database is not in the state you except.
d. Handle named exceptions whenever possible, instead of using WHEN OTHERS in exception handlers.
Learn the names and causes of the predefined exceptions. If your database operations might cause
particular ORA-errors, associate name with these errors so you can write handlers for them
e. Test your code with different combinations of bad data to see what potential errors arise
f. Write out debugging information in you exception handlers. You might store such information in a
separate table.
g. Carefully consider whether each exception handler should commit the transaction, roll it back, or let
it continue.
a. Using exceptions for error handling has several advantages. With exceptions, you can reliable handle potential
errors from many statements with a single exception handler.
b. Instead of checking for an error at every point it might occur, justa dd an exception handler to your PLSQL
block. If the exception is every raised in that block, you can be sure it will be handled.
c. Sometime the error is not immediately obvious, and could not be detected until later when you perform
calculations using bad data. Again, a single exception handler can trap all divison-by-zero errors, bad array
subscripts, and so on.
d. If you need to check for errors at the specific spot, you can enclose a single statement or a group of statements
inside its own BEGIN-END block with its own exception handler. You can make the checking as general or as
precise as you like
e. Isolating error-handling routines makes the rest of the program easier to read and understand.
a. An internal exception is raised automatically if your PLSQL program violates an Oracle rule or exceeds a system-
dependent limit.
b. PLSQL predefines some common Oracle errors as exception.
c. You can use the pragma EXCEPTION_INIT to associate exception names with other Oracle error codes that you
can anticipate.
d. To handle unexcepted Oracle errors, you can use the OTHER handler. Within this handler, you can call the
functions SQLCODE and SQLERRM to return the Oracle error code and message text.
e. Once you know the error code, you can use it with pragma EXCEPTION_INIT and write a handler specifically
for that error
f. PLSQL declares predefined exceptions globally in package STANDARD. You need not declare them yourself. You
can write handlers for predefined exception.
12. Views
Ans
a. A view is a virtual table in the database whose contents are defined via a query.
b. The views which has a set of named columns and rows of database data, appears to database users exactly like
an actual table.
c. When the data is queried, the rows and columns are visible to the view just way they are when a table is
searched.
d. The data of one or more tables are represented by views, which are also known as logical tables.
e. The base table on which a view is based, are where the view gets its data from
f. Any DML operations carried out on a view actually have an impact on the base table of the view i.e., any
insertion, updating or deletion done in a view will reflect in the base table
g. Views can be used in the same way as any other table
h. Types of views
a. Horizontal views
i. A horizontal view allows a user to only see the records of the deceased by horizontally slicing
the source database to produce the view
a. Advantages
a. Security
i. Each user can be given permission to access the database with the help of view that contain
specific data which is authorized to be accessed by the users.
b. Insulation from change
i. A view can present a consistent unchanged image of the database structure
c. Data Integrity
i. If data is accessed or entered to a view then the DBMS automatically ensures the integrity of
the database
d. Query simplicity
i. A view can be draw or certain data from different table and present it as a single table allowing
executing single table commands
b. Disadvantages
a. Performance
i. Performance is one of the disadvantages as if views created from multiple tables can slow
down the execution process
b. Update restriction
i. If view is created with the help of with check option then the updation in the view is restricted
against the where clause condition on the view is created
a. Views
a. This is one type of relation which is not a part of the physical database
b. It has no direct or physical relation with the database
c. Views can be used to provide security mechanism
d. Modifications through a view is not permitted
b. Table
a. A base relation is a relation that is not a derived relation
b. While it can manipulate the conceptual or physical relations stored in the data
15. Triggers
Ans
a. A trigger is a plsql block structure which is fired when a DML statements like INSERT, UPDATE and DELETE is
executed on a database table.
b. A trigger is triggered automatically when an associated DML statement in executed
c. Syntax for Creating a Trigger
[ON table_name]
WHEN (conditions)
BEGIN
---sql statements
END;
d. CREATE [OR REPLACE] TRIGGER trigger_name: This clause creates a trigger with the given name or overwrites
an existing trigger with the same name
e. {BEFORE | AFTER}: This clause indicates at what time should the trigger gets fired. For example, Before or after
updating a table.
f. [INSERT [OR] | UPDATE [OR] | DELETE]: This clause determines the triggered event. More than one triggering
events can be used together separated by OR keyword. The trigger gets fired at all the specified triggered event
g. ON table_name: This clause identifies the name of the table or view to which the trigger is associated
h. FOR EACH ROW: this clause is used to determine whether a trigger must fire when each row gets affected or
just once when the entire statement is executed
i. WHEN (condition): This clause is valid only for row level triggers. This trigger is fired only for rows that satisfy
the condition specified.
a. Benefits
a. Generating some derived column values automatically
b. Enforcing referential integrity
c. Event logging and storing information on table access
d. Auditing
e. Synchronous replication of tables
f. Imposing security authorizations
g. Preventing invalid transactions
b. Components of trigger