PL/SQL Fundamentals Exercises: PL/SQL Variable Declarations
PL/SQL Fundamentals: Exercise-7 with Solution
Write PL/SQL blocks to show the declaration of variables.
The following PL/SQL block shows how to declare variables with scalar data type.
PL/SQL Code:
Declaration of constant with scalar data type.
PL/SQL Code:
Declaration of variable with initial value
PL/SQL Code:
Initialize NULL by default to variable.
PL/SQL Code:
Declaration of variable with NOT NULL Constraint
PL/SQL Code:
Declaration of variable as same column type
In the following example the variable first_name inherits the data type and size of the column employees.first_name, which has a NOT NULL constraint and this declaration does not need an initial value.
PL/SQL Code:
Sample Output:
First Name = Statement processed. 0.07 seconds
Flowchart:
Declaration of variables with scalar data type

Declaration of constant with scalar data type

Declaration of variable with initial value

Initialize NULL by default to variable.

Declaration of variable with NOT NULL Constraint

Declaration of variable as same column type

Declaration of variable as same type of another variable
In the following example the variable last_name inherits the data type, size, and NOT NULL constraint of the variable first_name. Because last_name does not inherit the initial value of first_name and its declaration needs an initial value (which cannot exceed 25 characters).
PL/SQL Code:
Sample Output:
First Name = Harold Last Name = Jordon Statement processed. 0.00 seconds
Flowchart:
Declaration of variable as same type of another variable

Improve this sample solution and post your code through Disqus
Previous: Write a PL/SQL block to show single and multiline comments.
Next: Write PL/SQL blocks to show the scope and visibility of local and global identifiers.
What is the difficulty level of this exercise?