Using Identifiers in PL/SQL

Previous
Previous
Next
Next

You use identifiers to name PL/SQL program items and units, such as constants, variables, exceptions, and subprograms. An identifier consists of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs. Characters such as ampersands (&), hyphens (-), slashes (/), and spaces ( ) are not allowed.

You can use uppercase, lowercase, or mixed case to write identifiers. PL/SQL is not case-sensitive except within string and character literals. Every character, including dollar signs, underscores, and number signs, is significant. If the only difference between identifiers is the case of corresponding letters, PL/SQL considers the identifiers the same.

The declaration section in Example: Using Identifiers for Variables in PL/SQL show some PL/SQL identifiers. You can see additional examples of identifiers for variable names in Example: Using Comments in PL/SQL and Example: Declaring Variables in PL/SQL.

Using Identifiers for Variables in PL/SQL

DECLARE
  lastname           VARCHAR2(30); -- valid identifier
  last_name          VARCHAR2(30); -- valid identifier, _ allowed
  last$name          VARCHAR2(30); -- valid identifier, $ allowed
  last#name          VARCHAR2(30); -- valid identifier, # allowed
--  last-name  is invalid, hypen not allowed
--  last/name  is invalid, slash not allowed
--  last name  is invalid, space not allowed
--  LASTNAME is invalid, same as lastname and LastName
--  LastName is invalid, same as lastname and LASTNAME
BEGIN
  NULL; -- NULL statement does nothing, allows this block to executed and tested
END;
/

The size of an identifier cannot exceed 30 characters. Identifiers should be descriptive. When possible, avoid obscure names such as cpm. Instead, use meaningful names such as cost_per_million. You can use prefixes for more clarification. For example, you could begin each variable name with the var_ and each constant name with con_.

Some identifiers, called reserved words or keywords, have a special syntactic meaning to PL/SQL. For example, the words BEGIN and END are reserved. Often, reserved words and keywords are written in upper case for readability. Neither reserved words or keywords should be used as identifiers and the use can cause compilation errors. For a list of PL/SQL reserved words and keywords, see Reserved Words.