Sub topic 6
Choosing Scalar Data Types
Assume you know basic SQL, Oracle types such as NUMBER, VARCHAR2, and DATE, and how to run an anonymous PL/SQL block in SQL*Plus, SQLcl, or SQL Developer. This chapter focuses on scalar types, which hold one value rather than a collection or record.
What a scalar type does
A scalar data type defines what a variable can store and how Oracle treats that value. The type affects precision, storage, comparisons, arithmetic, and whether the value can be passed directly to SQL.
Common PL/SQL scalar types include:
| Type | Stores | Typical use |
|---|---|---|
| `NUMBER` | Exact numbers with optional precision and scale | Prices, rates, balances |
| `PLS_INTEGER` | Whole numbers | Counters and indexes |
| `VARCHAR2` | Variable-length text | Names, messages, codes |
| `CHAR` | Fixed-length text | Fixed-size codes |
| `DATE` | Date and time to the second | Business dates and timestamps |
| `TIMESTAMP` | Date and time with fractional seconds | Event timestamps |
| `BOOLEAN` | `TRUE`, `FALSE`, or `NULL` | Conditions and flags |
Oracle SQL and PL/SQL do not have identical type rules. For example, PL/SQL supports BOOLEAN variables, but a BOOLEAN value cannot be selected directly from an ordinary SQL query in many Oracle versions.
Choosing numeric types
Use NUMBER when a value may contain a fraction or must have a defined decimal precision. NUMBER(10,2) allows up to ten digits, with two digits after the decimal point.
Use PLS_INTEGER for whole-number calculations such as counters. It is efficient for values within its supported integer range. Do not use it for money or values that may contain decimals.
SET SERVEROUTPUT ON
DECLARE
v_price NUMBER(10,2) := 19.95;
v_quantity PLS_INTEGER := 3;
v_subtotal NUMBER(12,2);
BEGIN
v_subtotal := v_price * v_quantity;
DBMS_OUTPUT.PUT_LINE('Subtotal: ' || v_subtotal);
END;
/The multiplication produces a numeric result. The destination variable controls the declared precision of the stored result. If a value exceeds the declared precision, Oracle raises a numeric error instead of silently storing an inaccurate value.
Choosing character types
VARCHAR2 stores text using only the length needed for the current value, up to its declared limit. Use it for most names, descriptions, and messages.
CHAR always has a fixed length. Oracle pads shorter values with spaces. That behavior can make comparisons and output surprising, so reserve CHAR for values that truly have a fixed size, such as a two-character country code.
A declaration such as VARCHAR2(30) limits the value to 30 characters in the relevant PL/SQL character semantics. Assigning a longer value raises an error.
Dates, timestamps, and Boolean values
DATE stores a calendar date and time through seconds. It does not store fractional seconds. TIMESTAMP supports fractional seconds and is useful when event order matters closely.
Date output depends on the session's NLS_DATE_FORMAT. Do not assume that a displayed date format is the stored value.
BOOLEAN is useful inside procedural logic. It can also be NULL, which means unknown. Therefore, a condition is not true merely because it is not false.
SET SERVEROUTPUT ON
DECLARE
v_created_at TIMESTAMP := SYSTIMESTAMP;
v_enabled BOOLEAN := TRUE;
BEGIN
IF v_enabled THEN
DBMS_OUTPUT.PUT_LINE('Enabled at ' || v_created_at);
ELSE
DBMS_OUTPUT.PUT_LINE('Disabled');
END IF;
END;
/Common type errors
- Using
PLS_INTEGERfor currency can discard the intended decimal design. - Using
CHARfor ordinary text introduces padding. - Declaring too small a
VARCHAR2variable causesVALUE_ERRORwhen an assignment is too long. - Comparing a nullable
BOOLEANwith= TRUEdoes not handle theNULLcase explicitly. - Relying on displayed date text can hide session-format differences.
The following model helps connect a value to a suitable type:
Exercise
Which type is the better default for a product price of 125.50, and which type is suitable for a loop counter?
Show answer
Use NUMBER, commonly NUMBER(10,2), for the price and PLS_INTEGER for the loop counter.