PL/SQL Interview Questions
How would you determine the time zone under which a database was operating?
SELECT dbtimezone FROM DUAL;
Explain the use of setting GLOBAL_NAMES equal to TRUE.
It ensures the use of consistent naming conventions for databases and links in a networked environment.
What command would you use to encrypt a PL/SQL application?
WRAP
Explain the difference between a FUNCTION, PROCEDURE and PACKAGE.
They are all named PL/SQL blocks.
A Function must return a value. A function can be called inside a query.
A Procedure may or may not return a value.
A Package is the collection of like functions, procedures, and variables which can be logically grouped together.
Explain the use of table functions.
Name three advisory statistics you can collect.
Where in the Oracle directory tree structure are audit traces placed?
Explain materialized views and how they are used?
When a user process fails, what background process cleans up after it?
PMON
What background process refreshes materialized views?
Job Queue Process (CJQ)
How would you determine what sessions are connected and what resources they are waiting for?
V$SESSION, and V$SESSION_WAIT
Describe what redo logs are.
How would you force a log switch?
ALTER SYSTEM SWITCH LOGFILE;
Give two methods you could use to determine what DDL changes have been made.
What does coalescing a tablespace do?
Coalesce simply takes contiguous free extents and makes them into a single larger free extent.
What is the difference between a TEMPORARY tablespace and a PERMANENT tablespace?
TEMP tablespace gets cleared once the transaction is done, whereas PERMANENT tablespace retains the data.
Name a tablespace automatically created when you create a database.
SYSTEM, SYSAUX, TEMPORARY, USER, UNDOTBS1
When creating a user, what permissions must you grant to allow them to connect to the database?
GRANT CREATE SESSION TO USERNAME;
How do you add a data file to a tablespace?
ALTER TABLESPACE USERS ADD DATAFILE'/ora01/oradata/users02.dbf' SIZE 50M;
How do you resize a data file?
ALTER DATABASE DATAFILE '/ora01/oradata/users02.dbf' RESIZE 100M;
What view would you use to look at the size of a data file?
DBA_DATA_FILES
What view would you use to determine free space in a tablespace?
DBA_FREE_SPACE
How would you determine who has added a row to a table?
By implementing an INSERT trigger for logging details during each INSERT operation on the table.
How can you rebuild an index?
ALTER INDEX index_name REBUILD;
Explain what partitioning is and what its benefit is.
A table partition is also a table segment, and by using partitioning techniques we can enhance performance of table access.
You have just compiled a PL/SQL package but got errors, how would you view the errors?
show errors
How can you gather statistics on a table?
exec DBMS_STATS.GATHER_TABLE_STATS
Also, remember to analyze all associated indexes on that table using DBMS_STATS.GATHER_INDEX_STATS
How can you enable a trace for a session?
ALTER SESSION SET SQL_TRACE = 'TRUE';
What is the difference between the SQL*Loader and IMPORT utilities?
SQL*LOADER loads external data which is in OS files to Oracle database tables. While the IMPORT utility imports only data which is exported by the EXPORT utility of Oracle.
Name two files used for network connection to a database.
TNSNAMES.ORA and SQLNET.ORA
Describe the difference between a procedure, function and anonymous PL/SQL block.
An anonymous PL/SQL block uses the DECLARE statement in the declaration section. A function must return a value, while a procedure doesn't have to return a value.
What is a mutating table error and how can you get around it?
This happens with triggers. It occurs because the trigger is trying to update a row it is currently using. The usual fix involves either the use of views or temporary tables so the database is selecting from one while updating the other.
Describe the use of %ROWTYPE and %TYPE in PL/SQL.
%ROWTYPE allows you to associate a variable with an entire table row. The %TYPE associates a variable with a single column type.
What packages (if any) has Oracle provided for use by developers?
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, and 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.
Describe the use of PL/SQL tables.
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.
When is a declare statement needed?
The DECLARE statement is used in PL/SQL anonymous blocks such as with standalone, non-stored PL/SQL procedures. It must come first in the declaration section of a PL/SQL standalone file if it is used.
In what order should an 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?
OPEN then FETCH then LOOP followed by the EXIT WHEN statement. If not specified in this order, it will result in the final RETURN being done twice because of the way the %NOTFOUND is handled by PL/SQL.
What are SQLCODE and SQLERRM and why are they important for PL/SQL developers?
SQLCODE returns the value of the error number for the last error encountered. 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.
How can you find, within a PL/SQL block, if a cursor is open?
Use the %ISOPEN cursor status variable.
How can you generate debugging output from PL/SQL?
Use the DBMS_OUTPUT package to display output to the screen. 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 package UTL_FILE can also be used to write output to a file on the OS.
What are the types of triggers?
There are 12 types of triggers in PL/SQL. These 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.