Sei sulla pagina 1di 5

Oracle and PL/SQL Interview Questions

Q.What is the purpose of database links in Oracle?

Database links are created to establish communication between different


databases or different environments such as development, test and production of
the same database. The database links are usually designed to be read-only to
access other database information . They are also useful when you want to copy
production data into test environment for testing.

Q. What is Oracle's data dictionary used for?

Data dictionary in Oracle contains information about all database objects such as
tables, triggers, stored procedures, functions, indexes, constraints, views, users,
roles, monitoring information, etc.

Q. Which data dictionary objects are used to retrieve the information about the
following objects from a given schema?
1) tables
2) views
3) triggers
4) procedures
5) constraints
6) all of the above mentioned objects

The objects used are:


a> user_tables or tabs
b> user_views
c> user_triggers
d> user_procedures
e> user_constraints
f> user_objects

fferent SQL queries in the same PL/SQL program vs. design time declared explicit
cursors with an association to only one query.

Q. You want to view top 50 rows from Oracle table. How do I this?

Use ROWNUM, the pseudo column in where clause as follows:


Where rownum < 51

After complete execution of query and before displaying output of SQL query to
the user oracle internally assigns sequential numbers to each row in the output.
These numbers are held in the hidden column or pseudo column that is a
ROWNUM column. Now it is so simple to apply the above logical condition, as you
would have done to any other column of the table.
Q. How do you reference column values in BEFORE and AFTER insert and delete
triggers?

The BEFORE and AFTER insert triggers can reference column values by new
collection using keyword “:new.column name”. The before and after delete
triggers can reference column values by old collection using keyword “:old.
column name”.

Q. Can you change the inserted value in one of the columns in AFTER insert
trigger code?

This is not possible as the column values supplied by the insert SQL query are
already inserted into the table. If you try to assign new value to the column in
AFTER insert trigger code then oracle error would be raised. To alter any values
supplied by insert SQL query create BEFORE insert trigger.

Q. Explain use of SYSDATE and USER keywords.

SYSDATE is a pseudo column and refers to the current server system date. USER
is a pseudo column and refers to the current user logged onto the oracle session.
These values come handy when you want to monitor changes happening to the
table.

Q. What is the difference between explicit cursor and implicit cursor?

When a single insert, delete or update statement is executed within PL/SQL


program then oracle creates an implicit cursor for the same, executes the
statement, and closes the cursor. You can check the result of execution using
SQL%ROWCOUNT function.

Explicit cursors are created programmatically. The cursor type variable is declared
and associated with SQL query. The program then opens a cursor, fetches column
information into variables or record type variable, and closes cursor after all
records are fetched. To check whether cursor is open or not use function
SQL%ISOPEN and to check whether there are any records to be fetched from the
cursor use function SQL%FOUND.

Q. Why does a query in Oracle run faster when ROWID is used as a part of the
where clause?

ROWID is the logical address of a row - it is not a physical column. It is composed


of file number, data block number and row number within data block. Therefore
I/O time is minimized retrieving the row, resulting in a faster query.
Q. What type of exception will be raised in the following situations:

a> select..into statement returns more than one row.

b> select..into statement does not return any row.

c> insert statement inserts a duplicate record.

The errors returned are:


a> TOO_MANY_ROWS

b> NO_DATA_FOUND

c> DUP_VAL_ON_INDEX
PL-SQL Interview Questions with Answers

1. Describe the difference between a procedure, function and anonymous pl/sql


block.
Level: Low
Expected answer : Candidate should mention use of DECLARE statement, a
function must return a value while a procedure doesn't have to.

2. What is a mutating table error and how can you get around it?
Level: Intermediate
Expected answer: This happens with triggers. It occurs because the trigger is
trying to update a row it is currently using. The usual fix involves either use of
views or temporary tables so the database is selecting from one while updating
the other.

3. Describe the use of %ROWTYPE and %TYPE in PL/SQL


Level: Low
Expected answer: %ROWTYPE allows you to associate a variable with an entire
table row.
The %TYPE associates a variable with a single column type.

4. What packages (if any) has Oracle provided for use by developers?
Expected answer: Oracle provides the DBMS_ series of packages. There are many
which developers should be aware of such as DBMS_SQL, DBMS_PIPE,
DBMS_TRANSACTION,
DBMS_LOCK, DBMS_ALERT, DBMS_OUTPUT, DBMS_JOB, DBMS_UTILITY,
DBMS_DDL, UTL_FILE. If they can mention a few of these and describe how they
used them, even better. If they include the SQL routines provided by Oracle,
great, but not really what was asked.

5. Describe the use of PL/SQL tables


Expected answer: PL/SQL tables are scalar arrays that can be referenced by a
binary integer. They can be used to hold values for use in later queries
or calculations. In Oracle 8 they will be able to be of the %ROWTYPE designation,
or RECORD.

6. When is a declare statement needed ?


The DECLARE statement is used in PL/SQL anonymous blocks such as with stand
alone, non-stored PL/SQL procedures. It must come first in a PL/SQL stand alone
file if it is used.

7. In what order should a open/fetch/loop set of commands in a PL/SQL block be


implemented if you use the NOTFOUND cursor variable in the exit when
statement? Why?
Expected answer: OPEN then FETCH then LOOP followed by the exit when. If not
specified in this order will result in the final return being done twice because of
the way the %NOTFOUND is handled by PL/SQL.

8. What are SQLCODE and SQLERRM and why are they important for PL/SQL
developers?
Expected answer: SQLCODE returns the value of the error number for the last
error encountered. The SQLERRM returns the actual error message for the last
error encountered. They can be used in exception handling to report, or, store in
an error log table, the error that occurred in the code. These are especially useful
for the WHEN OTHERS exception.

9. How can you find within a PL/SQL block, if a cursor is open?


Expected answer: Use the %ISOPEN cursor status variable.

10. How can you generate debugging output from PL/SQL?


Expected answer: Use the DBMS_OUTPUT package. Another possible method is to
just use the SHOW ERROR command, but this only shows errors. The
DBMS_OUTPUT package can be used to show intermediate results from loops and
the status of variables as the procedure is executed. The new package UTL_FILE
can
also be used.

11. What are the types of triggers?


Expected Answer: There are 12 types of triggers in PL/SQL that consist of
combinations of the BEFORE, AFTER, ROW, TABLE, INSERT, UPDATE, DELETE and
ALL key words:
BEFORE ALL ROW INSERT
AFTER ALL ROW INSERT
BEFORE INSERT
AFTER INSERT etc.

1 .Explain the usage of WHERE CURRENT OF clause in cursors ?

WHERE CURRENT OF clause in an UPDATE,DELETE statement refers to


the latest row fetched from a cursor. Database Triggers
Where CURRENT OF clause means
cursor
points to present row of
the cursor

What is difference between a PROCEDURE & FUNCTION ?

A FUNCTION is always returns a value using the return statement.


A PROCEDURE may return one or more values through parameters or
may not return at all.

A function can be called from sql statements and queries while


procedure can be called in a begin end block only.

In case of function,it must have


return type.
In case of procedure,it may or may n't have return type.
In case of function, only it takes IN parameters
IN case of procedure
only it take IN,OUT,INOUT parameters..

Potrebbero piacerti anche