0% found this document useful (0 votes)
9 views10 pages

PL SQL Fundamentals Descriptives

Uploaded by

Md. Mesbah Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

PL SQL Fundamentals Descriptives

Uploaded by

Md. Mesbah Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PL SQL Fundamentals Descriptive

Chapter: one
1. What is PL/SQL?
PL/SQL:
• Stands for Procedural Language extension to SQL
• Is Oracle Corporation’s standard data access language for relational databases
• Seamlessly integrates procedural constructs with SQL
• Provides a block structure for executable units of code. Maintenance of code is made easier with
such a well-defined structure.
• Provides procedural constructs.
2. Write down the benefits of PL/SQL.
a. Integration of procedural constructs with SQL
b. Improved performance
c. Modularized program development
d. Integration with Oracle tools
e. Portability
f. Exception handling
3. Write down the PL/SQL Block Structure.
DECLARE (Optional)
Variables, cursors, user-defined exceptions
BEGIN (Mandatory)
SQL statements
PL/SQL statements
EXCEPTION (Optional)
Actions to perform
when errors occur
END; (Mandatory)
4. What are the types of PL/SQL block?
There are three types of blocks that make up a PL/SQL program:
• Procedures: Is a type of subprogram that performs an action
• Functions: Is a named PL/SQL block that returns a value
• Anonymous blocks:
Anonymous blocks are unnamed blocks. They are declared inline at the point in an applicationwhere
they are to be executed and are compiled each time the application is executed. Theseblocks are not
stored in the database.
5. What is subprogram?
Subprograms: Subprograms are complementary to anonymous blocks. They are named PL/SQL blocks that
are stored in the database.
Chapter: two
6. What is variable?
In Oracle/PLSQL, a variable allows a programmer to store data temporarily during the execution of code.
Variable declarations allocate storage for a value, specify its datatype, and specify a name that you can
reference.
7. Write down the use of Variables.
Variables can be used for:
• Temporary storage of data
• Manipulation of stored values
• Reusability
8. Write down the naming rules for variable.
• Must start with a letter
• Can include letters or numbers
• Can include special characters (such as $, _, and #)
• Must limit the length to 30 characters
• Must not be reserved words
9. How to handle the variable in PL/SQL?
Variables are:
• Declared and initialized in the declarative section
• Used and assigned new values in the executable section
• Passed as parameters to PL/SQL subprograms
• Used to hold the output of a PL/SQL subprogram
10. What are the types of variables?
a. PL/SQL variables:
i. Scalar
ii. Composite
iii. Reference
iv. Large objects (LOB)
b. Non-PL/SQL variables: Bind variables
11. Write down the Guidelines for Declaring and Initializing PL/SQL Variables.
a. Follow naming conventions.
b. Use meaningful names for variables.
c. Initialize variables designated as NOT NULL and CONSTANT.
d. Initialize variables with the assignment operator (:=) or the DEFAULT keyword:
 Myname VARCHAR2(20):='John';
 Myname VARCHAR2(20) DEFAULT 'John';
e. Declare one identifier per line for better readability and code maintenance.
12. What is scalar data type variable?
A scalar data type holds a single value and has no internal components. Scalar data types can be classified
into four categories: number, character, date, and Boolean.
13. What is %TYPE attribute?
The %TYPE attributes:
• Is used to declare a variable according to:
– A database column definition
– Another declared variable
• Is prefixed with:
– The database table and column
– The name of the declared variable
14. Write down the Advantages of the %TYPE Attribute.
Advantages of the %TYPE Attribute:
• You can avoid errors caused by data type mismatch or wrong precision.
• You can avoid hard coding the data type of a variable.
• You need not change the variable declaration if the column definition changes.
15. What is LOB Data Type Variables?
Large objects (LOBs) are meant to store a large amount of data. A database column can be of the LOB
category. With the LOB category of data types (BLOB, CLOB, BFILE and NCLOB)
16. What is a composite data type variable?
A composite data type stores values that have internal components. You can pass entire composite variables
to subprograms as parameters, and you can access internal components of composite variables individually.
Internal components can be either scalar or composite.
17. What is bind variables?
Bind variables are:
• Created in the environment
• Also called host variables
• Created with the VARIABLE keyword
• Used in SQL statements and PL/SQL blocks
• Accessed even after the PL/SQL block is executed
• Referenced with a preceding colon
Chapter: three
18. What is Lexical Units in a PL/SQL Block?
Lexical units:
• Are building blocks of any PL/SQL block
• Are sequences of characters including letters, digits, tabs, spaces, returns, and symbols
19. Write down the classification of Lexical Units.
Can be classified as:
i. Identifiers
ii. Delimiters
iii. Literals
iv. Comments
20. What is Identifiers?
Identifiers: Identifiers are the names given to PL/SQL objects. You have learned to identify valid and invalid
identifiers. Recollect that keywords cannot be used as identifiers.
Quoted Identifiers: Quoted identifiers are used to:
- Make identifiers case sensitive
- Include characters such as spaces
- Use reserved words
21. What is Delimiters?
Delimiters: Delimiters are symbols that have special meaning. You have already learned that the symbol “;”
is used to terminate a SQL or PL/SQL statement. Therefore, “;” is the best example of a delimiter.
22. What is Literals?
Literals: Any value that is assigned to a variable is a literal. Any character, number, Boolean, or date value
that is not an identifier is a literal. Literals are classified as:
a. Character literals.
b. Numeric literals.
c. Boolean literals.
23. What is Comments?
A comment is text that the PL/SQL compiler ignores. Its primary purpose is to document code, Well-placed
comments are extremely valuable for code readability and future code maintenance. PL/SQL has both single-
line and multiline comments.
24. How we can use the comments?
a. Prefix single-line comments with two dashes (--).
b. Place multiple-line comments between the symbols “/*” and “*/”.
25. Write down about SQL Functions in PL/SQL.
a. Available in procedural statements:
i. Single-row number
b. Not available in procedural statements:
i. DECODE
ii. Group functions
26. What are the categories of SQL Functions that you can use to manipulate data in PL/SQL?
SQL functions help you to manipulate data; they fall into the following categories:
• Number
• Character
• Conversion
• Date
• Miscellaneous
27. Write down the Programming Guidelines.
Make code maintenance easier by:
• Documenting code with comments
• Developing a case convention for the code
• Developing naming conventions for identifiers and other objects
• Enhancing readability by indenting
Chapter: four
28. Write down the Guidelines for Retrieving Data in PL/SQL
Guidelines for Retrieving Data in PL/SQL
• Terminate each SQL statement with a semicolon (;).
• Every value retrieved must be stored in a variable using the INTO clause.
• The WHERE clause is optional and can be used to specify input variables, constants, literals, or
PL/SQL expressions. However, when you use the INTO clause, you should fetch only one row and the
usage of WHERE clause is a must in such cases.
• Specify the same number of variables in the INTO clause as database columns in the SELECT clause.
Be sure that they correspond positionally and that their data types are compatible.
• Use group functions, such as SUM, in a SQL statement, because group functions apply to groups of
rows in a table.
29. Write down about the requirements ‘Queries Must Return Only One Row’ of SELECT Statements in PL/SQL.
Queries Must Return Only One Row:
SELECT statements within a PL/SQL block fall into the ANSI classification of embedded SQL, for which the
following rule applies: queries must return only one row. A query that returns more than one row or no row
generates an error.
PL/SQL manages these errors by raising standard exceptions, which you can handle in the exception section
of the block with the NO_DATA_FOUND and TOO_MANY_ROWS exceptions.
30. How to Retrieve Multiple Rows from a Table and Operate on the Data?
A SELECT statement with the INTO clause can retrieve only one row at a time. If your requirement is to
retrieve multiple rows and operate on the data, then you can make use of explicit cursors.
31. What is SQL Cursor? And write down the types cursors.
A cursor is a pointer to the private memory area allocated by the Oracle server.
There are two types of cursors:
i. Implicit cursors: Created and managed internally by the Oracle server to process SQL statements
ii. Explicit cursors: Explicitly declared by the programmer
32. Write down the SQL Cursor Attributes for Implicit Cursors.
Using SQL cursor attributes, you can test the outcome of your SQL statements.
SQL%FOUND Boolean attribute that evaluates to TRUE if the most recent SQL statement
returned at least one row.
SQL%NOTFOUND Boolean attribute that evaluates to TRUE if the most recent SQL statement did
not return even one row.

SQL%ROWCOUNT An integer value that represents number of rows affected by the most recent
SQL statement.
Chapter: five
33. Write down about IF Statement.
The structure of the PL/SQL IF statement is similar to the structure of IF statements in other procedural
languages. It allows PL/SQL to perform actions selectively based on conditions.
Syntax:
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;]
END IF;
In the syntax:
Condition Is a Boolean variable or expression that returns TRUE,
FALSE, or NULL
THEN Introduces a clause that associates the Boolean expression with the sequence of statements that
follows it
Statements Can be one or more PL/SQL or SQL statements.
The statements in the THEN clause are executed only if the condition in the associated IF clause
evaluates to TRUE.
ELSIF Is a keyword that introduces a Boolean expression
ELSE Introduces the default clause that is executed if and only if none of theearlier predicates (introduced
by IF and ELSIF) are TRUE
END IF Marks the end of an IF statement

For example:
a. Simple IF Statement
DECLARE
myage number:=31;
BEGIN
IF myage< 11
THEN
DBMS_OUTPUT.PUT_LINE(' I am a child ');
END IF;
END;
/
b. IF THEN ELSE Statement
DECLARE
myage number:=31;
BEGIN
IF myage< 11
THEN
DBMS_OUTPUT.PUT_LINE(' I am a child ');
ELSE
DBMS_OUTPUT.PUT_LINE(' I am not a child ');
END IF;
END;
/
c. IF ELSIF ELSE Clause
DECLARE
myage number:=31;
BEGIN
IF myage< 11
THEN
DBMS_OUTPUT.PUT_LINE(' I am a child ');
ELSIF myage< 20
THEN
DBMS_OUTPUT.PUT_LINE(' I am young ');
ELSIF myage< 30
THEN
DBMS_OUTPUT.PUT_LINE(' I am in my twenties');
ELSIF myage< 40
THEN
DBMS_OUTPUT.PUT_LINE(' I am in my thirties');
ELSE
DBMS_OUTPUT.PUT_LINE(' I am always young ');
END IF;
END;
/
34. What is Loops? What are the types of Loops?
Loops repeat a statement or sequence of statements multiple times.
There are three loop types:
• Basic loop that performs repetitive actions without overall conditions
• FOR loops that perform iterative actions based on a count
• WHILE loops that perform iterative actions based on a condition
35. How to you work with Basic Loops?
A basic loop allows execution of its statements at least once, even if the EXIT condition is already met upon
entering the loop. Without the EXIT statement, the loop would be infinite.
36. How to you work with WHILE Loops?
The WHILE loop to repeat statements while a condition is TRUE.
37. How to you work with FOR Loops?
A FOR loops to shortcut the test for the number of iterations.
Do not declare the counter; it is declared implicitly.
Chapter: Six
38. What is Composite Data Types?
Can hold multiple values (unlike scalar types)
39. What are the types of Composite Data Types?
Are of two types:
– PL/SQL records
– PL/SQL collections
40. Why Use Composite Data Types?
You have all the related data as a single unit. You can easily access and modify data. Data is easier to
manage, relate, and transport if it is composite. An analogy is having a single bag for all your laptop
components rather than a separate bag for each component.
41. When you can be use PL/SQL Records or Collections?
• Use PL/SQL records when you want to store values of different data types but only one occurrence
at a time.
• Use PL/SQL collections when you want to store values of the same data type.
42. What is PL/SQL Records?
• Must contain one or more components (called fields) of any scalar, RECORD, or INDEX BY table data
type
• Are user-defined and can be a subset of a row in a table
• Treat a collection of fields as a logical unit
• Are convenient for fetching a row of data from a table for processing
43. Write down about %ROWTYPEAttribute.
• Declare a variable according to a collection of columns in a database table or view.
• Prefix %ROWTYPE with the database table or view.
• Fields in the record take their names and data types from the columns of the table or view.
44. Write down advantages of using the %ROWTYPEAttribute.
Advantages of Using the %ROWTYPE Attribute:
• The number and data types of the underlying database columns need not be known—and, in fact, might
change at run time.
• The %ROWTYPE attribute is useful when you want to retrieve a row with:
– The SELECT*statement
– Row-level INSERT and UPDATE statements
45. What is PL/SQL collections? What are the types of PL/SQL collections?
PL/SQL collections: Collections are used to treat data as a single unit.
Collections are of three types:
- Associative array
- Nested table
- VARRAY
46. What is Associative Arrays?
An associative array is a type of PL/SQL collection. It is a composite data type, and is user defined.
Associative arrays are sets of key-value pairs. They can store data using a primary key value as the index,
where the key values are not necessarily sequential. Associative arrays are also known as INDEX BY tables.
47. Write down the Using INDEXBYTable Methods.
Using INDEX BY Table Methods
An INDEX BY table method is a built-in procedure or function that operates on an associative array and is
called by using the dot notation.
Syntax: table_name.method_name [(parameters)]
The following methods make associative arrays easier to use:
• EXISTS
• COUNT
• FIRST
• LAST
• PRIOR
• NEXT
• DELET
48. What is Nested Tables?
A nested table holds a set of values. In other words, it is a table within a table. Nested tables are unbounded;
that is, the size of the table can increase dynamically.
49. What is Varrays?
Variable-size arrays, or varrays, are also collections of homogeneous elements that hold a fixed number of
elements.
Chapter: Seven
50. What are Explicit cursors?
Explicit cursors: declared and managed by the
Programmer
51. Write down the explicit cursor functions.
Explicit cursor functions:
• Can perform row-by-row processing beyond the first row returned by a query
• Keep track of the row that is currently being processed
• Enable the programmer to manually control explicit cursors in the PL/SQL block
52. Write down the lifecycle for Controlling Explicit Cursors.
No

Yes
Empty?
Declare Open Fetch Close

53. Write down about Cursor FORLoops.


• The cursor FOR loop is a shortcut to process explicit cursors.
• Implicit open, fetch, exit, and close occur.
• The record is implicitly declared.
54. Write down the guidelines for using Cursor FORLoops.
Guidelines:
• Do not declare the record that controls the loop; it is declared implicitly.
• Test the cursor attributes during the loop if required.
• Supply the parameters for a cursor, if required, in parentheses following the cursor name in the FOR
statement.
55. Write down the Cursor Attributes for Explicit Cursors.
Use explicit cursor attributes to obtain status information abouta cursor.
Attributes Type Description

%ISOPEN Boolean Evaluates to TRUEif the cursor is open

%NOTFOUND Boolean Evaluates to TRUEif the most recent fetchdoes not return a row

%FOUND Boolean Evaluates to TRUEif the most recent fetchreturns a row; complement of
%NOTFOUND

%ROWCOUNT Number Evaluates to the total number of rows returnedso far

56. What is Cursors with Parameters?


• Pass parameter values to a cursor when the cursor is opened and the query is executed.
• Open an explicit cursor several times with a different active set each time.
57. Write down the use of FORUPDATEClause.
• Use explicit locking to deny access to other sessions for the duration of a transaction.
• Lock the rows before the update or delete.
58. Write down the use of WHERECURRENTOFClause.
• Use cursors to update or delete the current row.
• Include the FOR UPDATE clause in the cursor query to first lock the rows.
• Use the WHERE CURRENT OF clause to reference the current row from an explicit cursor.
Chapter: Eight
59. What is an Exception?
An exception is a PL/SQL error that is raised duringprogram execution.
60. What are the Methods for Raising an Exception?
An exception can be raised:
– Implicitly by the Oracle Server
– Explicitly by the program
61. What are the process of Handling Exceptions
An exception can be handled:
– By trapping it with a handler
– By propagating it to the calling environment
62. What are the types of Exception?
a. Implicitly raised:
Are of two types:
I. Predefined oracle server
II. Non-predefined oracle server
b. Explicitly raised: User defined
63. Write down the Guidelines for Trapping Exceptions.
Guidelines for Trapping Exceptions
• The EXCEPTION keyword starts the exception-handlingsection.
• Several exception handlers are allowed.
• Only one handler is processed before leaving the block.
• WHEN OTHERS is the last clause.
64. How to you trap predefined oracle server errors?
Trapping Predefined Oracle Server Errors
Trap a predefined Oracle Server error by referencing its predefined name within thecorresponding
exception-handling routine.
65. Mention the Sample predefined exceptions, please.
Sample predefined exceptions:
– NO_DATA_FOUND
– TOO_MANY_ROWS
– INVALID_CURSOR
– ZERO_DIVIDE
– DUP_VAL_ON_INDEX
66. Write down the lifecycle for Trapping Non-Predefined Oracle Server Errors.

Declare Associate Reference

Declarative section EXCEPTION section

67. What are the types of Functions for Trapping Exceptions?


• SQLCODE: Returns the numeric value for the error code
• SQLERRM: Returns the message associated with the error number
68. Write down the lifecycle for Trapping User-Defined Exceptions.

Declare Raise Reference

EXCEPTION-Handling
Declarative section Executable section section
69. How PL/SQL Exceptions Propagate
When an exception is raised, if PL/SQL cannot find a handler for it in the current block or subprogram, the
exception propagates. That is, the exception reproduces itself in successive enclosing blocks until a handler
is found or there are no more blocks to search. In the latter case, PL/SQL returns an unhandled exception
error to the host environment.
70. Write down the use of RAISE_APPLICATION_ERROR Procedure.
• You can use this procedure to issue user-defined error messages from stored subprograms.
• You can report errors to your application and avoid returning unhandled exceptions.

Chapter: Nine
71. Write down Differences Between Anonymous Blocks and Subprograms
Anonymous Blocks Subprograms
Unnamed PL/SQL blocks Named PL/SQL blocks
Compiled every time Compiled only once
Not stored in the database Stored in the database
Cannot be invoked by other applications Named and, therefore, can be invoked by other applications
Do not return values If functions, must return values
Cannot take parameters Can take parameters

You might also like