PLSQL - Oracle Server Flashcards
How do you retrieve the current date and time from the local database server?
In order to extract part of the date, we use the TO_CHAR function on SYSDATE and specify the format we need.
SELECT SYSDATE FROM dual;
SELECT id, TO_CHAR(SYSDATE, ‘yyyy/mm/dd’) from InterviewBitEmployeeTable where customer_id < 200;
What PL/SQL syntax returns the user id of the current session?
SELECT USER FROM dual;
What are the various PL/SQL String Functions available for manipulating character data?
The functions that are used for manipulating the character data are called String Functions.
LEFT: This function returns the specified number of characters from the left part of a string.
Syntax: LEFT(string_value, numberOfCharacters).
For example, LEFT(‘InterviewBit’, 9) will return ‘Interview’.
RIGHT: This function returns the defined number of characters from the right part of a string.
Syntax: RIGHT(string_value, numberOfCharacters)
For example, RIGHT(‘InterviewBit’,3) would return ‘Bit’.
SUBSTRING: This function would select the data from a specified start position through the number of
characters defined from any part of the string.
Syntax: SUBSTRING(string_value, start_position, numberOfCharacters)
For example, SUBSTRING(‘InterviewBit’,2,4) would return ‘terv’.
LTRIM: This function would trim all the white spaces on the left part of the string.
Syntax: LTRIM(string_value)
For example, LTRIM(’ InterviewBit’) will return ‘InterviewBit’.
RTRIM: This function would trim all the white spaces on the right part of the string.
Syntax: RTRIM(string_value)
For example, RTRIM(‘InterviewBit ‘) will return ‘InterviewBit’.
UPPER: This function is used for converting all the characters to the upper case in a string.
Syntax: UPPER(string_variable)
For example, UPPER(‘interviewBit’) would return ‘INTERVIEWBIT’.
LOWER: This function is used for converting all the characters of a string to lowercase.
Syntax: LOWER(string_variable)
For example, LOWER(‘INterviewBit’) would return ‘interviewbit’
What is the difference between ROLLBACK and ROLLBACK TO statements in PL/SQL?
ROLLBACK command is used for rolling back all the changes from the beginning of the transaction.
ROLLBACK TO command is used for undoing the transaction only till a SAVEPOINT. The transactions cannot be rolled back before the SAVEPOINT and hence the transaction remains active even before the command is specified.
What is the use of SYS.ALL_DEPENDENCIES?
SYS.ALL_DEPENDENCIES is used for describing all the dependencies between procedures, packages, triggers, functions that are accessible to the current user. It returns the columns like name, dependency_type, type, referenced_owner etc.
What are the virtual tables available during the execution of the database trigger?
The THEN and NOW tables are the virtual tables that are available during the database trigger execution. The table columns are referred to as THEN.column and NOW.column respectively.
Only the NOW.column is available for insert-related triggers.
Only the THEN.column values are available for the DELETE-related triggers.
Both the virtual table columns are available for UPDATE triggers.
Differentiate between the cursors declared in procedures and the cursors declared in the package specifications.
The cursors that are declared in the procedures will have the local scope and hence they cannot be used by other procedures.
The cursors that are declared in package specifications are treated with global scope and hence they can be used and accessed by other procedures.
What are COMMIT, ROLLBACK and SAVEPOINT statements in PL/SQL?
These are the three transaction specifications that are available in PL/SQL.
COMMIT: Whenever any DML operations are performed, the data gets manipulated only in the database buffer and not the actual database. In order to save these DML transactions to the database, there is a need to COMMIT these transactions.
COMMIT transaction action does saving of all the outstanding changes since the last commit and the below steps take place:
The release of affected rows.
The transaction is marked as complete.
The details of the transaction would be stored in the data dictionary.
Syntax: COMMIT;
ROLLBACK: In order to undo or erase the changes that were done in the current transaction, the changes need to be rolled back. ROLLBACK statement erases all the changes since the last COMMIT.
Syntax: ROLLBACK;
SAVEPOINT: This statement gives the name and defines a point in the current transaction process where any changes occurring before that SAVEPOINT would be preserved whereas all the changes after that point would be released.
Syntax: SAVEPOINT
How can you debug your PL/SQL code?
We can use DBMS_OUTPUT and DBMS_DEBUG statements for debugging our code:
DBMS_OUTPUT prints the output to the standard console.
DBMS_DEBUG prints the output to the log file.
What is the difference between a mutating table and a constraining table?
A table that is being modified by the usage of the DML statement currently is known as a mutating table. It can also be a table that has triggers defined on it.
A table used for reading for the purpose of referential integrity constraint is called a constraining table.
Is it possible to declare column which has the number data type and its scale larger than the precision? For example defining columns like: column name NUMBER (10,100), column name NUMBER (10,-84)
Yes, these type of declarations are possible.
Number (9, 12) indicates that there are 12 digits after decimal point. But since the maximum precision is 9, the rest are 0 padded like 0.000999999999.
Number (9, -12) indicates there are 21 digits before the decimal point and out of that there are 9 possible digits and the rest are 0 padded like 999999999000000000000.0
Explain the uses of database trigger.
A PL/SQL program unit associated with a particular database table is called a database trigger. It is used for :
1) Audit data modifications.
2) Log events transparently.
3) Enforce complex business rules.
4) Maintain replica tables
5) Derive column values
6) Implement Complex security authorizations
What are the two types of exceptions?
Error handling part of PL/SQL block is called Exception. They have two types :
user_defined and predefined.
Some predefined exceptions are:
DUP_VAL_ON_INDEX ZERO_DIVIDE NO_DATA_FOUND TOO_MANY_ROWS CURSOR_ALREADY_OPEN INVALID_NUMBER INVALID_CURSOR PROGRAM_ERROR TIMEOUT _ON_RESOURCE STORAGE_ERROR LOGON_DENIED VALUE_ERROR etc.
How many triggers can be applied to a table?
A maximum of 12 triggers can be applied to one table.
What operators deal with NULL?
NVL converts NULL to another specified value.
var:=NVL(var2,’Hi’);
IS NULL and IS NOT NULL can be used to check specifically to see whether the value of a variable is NULL or not.
Explain 3 basic parts of a trigger.
A triggering statement or event.
A restriction
An action
What are the uses of MERGE?
MERGE is used to combine multiple DML statements into one.
Syntax : merge into tablename
using(query)
on(join condition)
when not matched then
[insert/update/delete] command
when matched then
[insert/update/delete] command
What are the SQL* Plus required connection details?
Username:
Password:
Host String:
After successful connection we need to execute “set serveroutput on” if we need to see the output of the code. Now we are ready to work with the SQL* Plus tool.
Write a simple PL/SQL program for printing “Hello World” using “Anonymous block”.
BEGIN
dbms_output.put_line (‘Hello World..’);
END;
/
Note: A block should be always followed by ‘/’ which sends the information to the compiler about the end of the block. Till the compiler encounters ‘/’, it will not consider the block is completed, and it will not execute it.
Declaring a variable “text” of a VARCHAR2 type with size 25
Assign the value “Hello World” to the variable “text”.
Print the value of the variable “text”.
DECLARE text VARCHAR2(25); BEGIN text:= ‘Hello World’; dbms_output.put_line (text); END: /
What are the four Groups of Data Types used in PL/SQL?
A data type specifies the storage format, constraints and valid range of values.
- Scalar Data Types. No internal components
- Composite Data Types. Has internal components which can be manipulated.
- Reference Data Types
- LOB Data Types. Used to store large size data in the database.
There are at least 32 different Scalar Data Types used in PL/SQL. What are they?
BINARY_DOUBLE
BINARY_FLOAT
BINARY_INTEGER
DEC
DECIMAL
DOUBLE PRECISION
FLOAT
INT
INTEGER
NATURAL
NATURALN
NUMBER – Used to store fixed and floating point numbers. SYNTAX= Number(Precision, Scale)
PLS_INTEGER
POSITIVE
POSITIVEN
REAL
SIGNTYPE
SMALLINT
CHAR
CHARACTER – Stores the fixed length of character data. SYNTAX= CHAR(Maximum_Length)
LONG’LONG RAW
NCHAR
NVARCHAR2
RAW
ROWID and UROWID
-ROWID column stores binary values called row_id’s
-Each row_id represents the storage address of a row
-A physical row_id identified a row in an ordinary table.
-A logical row_id identified a row in an index organized table.
-ROWID Data Type can only store physical row_id’s.
-UROWID(universal row_id) data type can store physical, logical or foreign (non-Oracle)
row_id’s.
STRING
VARCHAR
VARCHAR2 – Stores the variable length of character data. SYNTAX= VARCHAR2(Maximum_Length)
BOOLEAN – Stores the values TRUE, FALSE and the non-value NULL. Does not take any parameters.
DATE – stores fixed-length data values.
Valid date range includes January 1, 4712 BC to December 31, 4712 AD.
There are at least 3 different Composite Data Types used in PL/SQL. What are they?
RECORD
TABLE
VARRAY
There are at least 2 different Reference Data Types used in PL/SQL. What are they?
REF CURSOR
REF object_type