PL/SQL Acceptable Identifiers, Variable & Naming Conventions

โšก Smart Summary

PL/SQL Identifiers are the names given to PL/SQL objects such as variables, constants, cursors, and procedures. They must start with a letter, stay within 30 characters, are case-insensitive, and follow naming conventions that signal a variable’s level and type.

  • ๐Ÿท๏ธ Definition: An identifier names any PL/SQL object, from a variable to a package or label.
  • ๐Ÿ“ Rules: Must begin with a letter, be at most 30 characters, and contain no whitespace.
  • ๐Ÿ”  Allowed Signs: Dollar sign, underscore, and hash are permitted inside an identifier.
  • ๐Ÿงญ Naming Convention: The first letter marks the level (P, L, G) and the second marks the type (C, V, N, R, T).
  • ๐Ÿ“ฆ Variables: A variable is an identifier bound to a storage area, declared with a valid data type.
  • ๐Ÿ“ Declaration: Variables are declared in the declarative section before use.
  • โžก๏ธ Assignment: A value can be set at declaration or later using the := operator.

PL/SQL Identifiers Naming Conventions

What are PL/SQL Identifiers?

Identifiers in PL/SQL are the names given to a PL/SQL object. The object could be a constant, variable, exception, cursor, procedure, function, package, trigger, object type, reserved word, or label. Identifiers contain letters, numerals, signs, underscores, and so on. They are case-insensitive and limited to 30 characters in size.

Properties of PL/SQL Identifiers

Here are the main properties of PL/SQL identifiers:

  • Must start with a letter.
  • The maximum size is limited to 30 characters.
  • Cannot contain whitespace characters.
  • Can contain a dollar sign (‘$’), underscore (‘_’), and hash sign (‘#’).
  • Is case-insensitive.

Naming Conventions in PL/SQL

In a complex program, we may have to include many identifiers, such as variables and cursors. To avoid confusion and increase readability, we need to follow certain naming conventions.

The commonly used naming conventions in PL/SQL are as follows.

The first letter specifies the declared level of the variable:

  • ‘P’ โ€“ variable declared at the parameter level.
  • ‘L’ โ€“ variable declared at the local block.
  • ‘G’ โ€“ variable declared at the global level.

The second letter specifies the type of identifier:

  • ‘C’ โ€“ cursor identifier.
  • ‘V’ โ€“ VARCHAR and CHAR data type.
  • ‘N’ โ€“ NUMBER data type.
  • ‘R’ โ€“ record type.
  • ‘T’ โ€“ table type.

Below are some examples of proper naming conventions in PL/SQL:

  • Lv_name โ€“ local level variable of VARCHAR/CHAR data type.
  • Pc_num โ€“ parameter level cursor identifier.
  • Gn_user_id โ€“ global level variable of numerical data type.

PL/SQL Variables

Variables in PL/SQL are basic identifiers assigned to a storage area that a program can manipulate. Variables are placeholders where the user can store values. These variables need to be associated with a valid PL/SQL data type before use. Data types define the storage and processing methods for these variables.

PL/SQL Variable Declaration

Variables are mainly used to store data during data manipulation or processing. They must be declared before use, inside the declarative section of the PL/SQL blocks.

Declaring a variable is the process of assigning a name to the placeholder and associating it with a valid data type.

Syntax

<variable_name> <datatype>;

The syntax above shows how to declare a variable in the declarative section.

Data Storing in PL/SQL Variables

Once a variable is declared, it is ready to hold data of the defined type. The values can be assigned either in the execution section or at the time of declaration. The value can be a literal or another variable’s value. Once assigned, it is stored in the memory space allocated for that variable.

Syntax

<variable_name> <datatype> := <default_value>;

The syntax above shows how to declare a variable and assign a value in the declarative section.

<variable_name> <datatype>;
<variable_name> := <value>;

The syntax above shows how to assign a value to an already declared variable.

Example 1: In this example, we learn how to declare a variable and assign a value to it. We print ‘GURU99’ in the following program using variables.

PL/SQL variable declaration and assignment example

DECLARE
lv_name VARCHAR2(50);
lv_name_2 VARCHAR2(50) := 'GURU99';
BEGIN
lv_name := lv_name_2;
dbms_output.put_line(lv_name);
END;
/

Code Explanation

  • Code line 2: Declaring the variable ‘lv_name’ of VARCHAR2 with size 50.
  • Code line 3: Declaring the variable ‘lv_name_2’ of VARCHAR2 with size 50 and assigning the default value using the literal ‘GURU99’.
  • Code line 5: The value for variable ‘lv_name’ is assigned from the variable ‘lv_name_2’.
  • Code line 6: Printing the stored value of variable ‘lv_name’.

When the above code is executed, you get the following output.

Output

GURU99

FAQs

It must start with a letter, be at most 30 characters, contain no whitespace, and use only letters, digits, and the signs dollar, underscore, and hash. Identifiers are case-insensitive.

The prefix encodes the scope and type at a glance, so Lv_name reads as a local varchar variable. In a large block this reduces confusion and makes the code easier to maintain.

The := operator assigns a value to a variable. A single = is used for comparison inside conditions. Mixing them is a common beginner error that causes a compile-time failure.

Yes. AI can scan a package, flag identifiers that break the agreed prefixes, and propose renames. It can also generate consistent names for new variables based on their scope and type.

No. lv_name and LV_NAME refer to the same identifier. Only string literals in single quotes are case-sensitive, so a convention exists for readability, not because the compiler requires it.

Summarize this post with: