Data types (NUMBER, CHAR, VARCHAR, VARCHAR2, CLOB, NCLOB, LONG, DATE, RAW, LONGROW)

Choosing the right database data type ensures your storage is memory-optimized, lightning-fast, and safe from silent padding bugs or space overflows.

12 min read · 13 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The Memory Spill Disaster

Imagine setting up an Excel sheet to keep track of student names, roll numbers, admission dates, and profile pictures. Excel doesn't care if you put a long paragraph inside a cell meant for a short name. But in an enterprise production database hosting millions of records, if you allocate massive, fixed memory chunks for small 10-letter words, your hard drive will bloat and slow to a crawl. Conversely, if you allocate a standard short container and a student attempts to upload a deep-dive 5,000-word internship statement of purpose, the system will instantly crash with an overflow error. How do we precisely define our storage containers so they adapt perfectly to strings, numbers, binary blobs, and timelines without wasting a single byte?

Theory

The Fixed-Slot Egg Carton vs. The Expandable Elastic Pouch

Think of database data types like shipping containers. Using CHAR is like a rigid wooden box built with pre-molded slots for exactly 20 items. If you place a tiny 4-letter word inside it, the box doesn't shrink; it fills up the remaining 16 slots with blank space weights (spaces). Using VARCHAR2 is like a flexible, elastic drawstring pouch. If you put 4 characters in it, it wraps tightly around those 4 characters, consuming only 4 bytes of disk space. For mega-sized loads like text novels or video files, we upgrade to giant storage tanks called CLOB and BLOB that can hold gigabytes of data outside the main table layout.

Theory

The Core Architecture of Oracle Data Types

In Relational Databases (specifically Oracle SQL), tables require strict structural data definitions. Data types dictate how data is validated, indexed, and stored on physical disk sectors. We classify them into four foundational categories: Character Strings, Numeric Values, Large Objects (LOBs), and Temporal Datetime blocks.

At a glance

Table 1: Technical specifications and strategic application domains for Oracle SQL data types.

Data Type NameStorage Mechanism & LimitsIdeal Practical Use Case
CHAR(size)Fixed-length character string up to 2000 bytes. Pads trailing spaces automatically if shorter.Fixed codes like State Abbreviations ('DL', 'MH') or Status Flags ('Y', 'N').
VARCHAR2(size)Variable-length character string up to 4000 bytes. Uses only the actual character length + overhead.Highly unpredictable text fields like Names, Emails, and Addresses.
NUMBER(p, s)Variable-length numeric value. 'p' is total precision digits; 's' is scale decimal points.Financial fields like Semester Fees or fractional Grade Point Averages (GPA).
DATEFixed 7-byte layout storing century, year, month, day, hour, minute, and second values.Timestamps like Exam Submission Times or Birth Dates.
CLOB / NCLOBCharacter Large Object holding up to 4 Gigabytes of text data. NCLOB handles National UTF-16 sets.Massive textual elements like Resume Overviews or Course Syllabi.
RAW / LONG RAWRaw binary byte streams (RAW up to 2000 bytes; LONG RAW up to 2GB). Deprecated in favor of BLOB.Legacy storage for small icons, compiled cryptographic keys, or digital signatures.

Theory

Deep Dive: Precision Mechanics and Large Objects

University examination papers frequently test the exact mathematical boundary calculations of the NUMBER(p, s) type, along with legacy vs. modern storage limits (like LONG vs. CLOB). The syntax NUMBER(5, 2) means the number has a precision of 5 total digits, out of which a scale of 2 digits is strictly reserved after the decimal point. This means the largest value it can hold is 999.99. If you try to store 1000.50, the database throws an arithmetic overflow error because it requires 6 digits in total!

Watch out

The Legacy Trap: Avoid LONG and LONG RAW

In old textbooks, you might see LONG used for long text blocks and LONG RAW for files. In modern industry systems, these are highly restricted. A table can only have one single LONG column, and it cannot be used in GROUP BY or DISTINCT queries. Always use CLOB (for text) or BLOB (for binaries) in your lab assignments instead!

Theory

Worked Example: Blueprinting the Student Profile Schema

Let us build a real-world table for a college portal that demonstrates proper, industry-grade type choices, preventing structural bugs and space allocation waste.

Practical

Constructing an Optimized Schema Definition

-- Step 1: Create a highly robust student registry profile
CREATE TABLE student_profile (
    roll_id NUMBER(6,0) PRIMARY KEY,       -- Allows integers up to 999999
    gender CHAR(1),                        -- Always exactly 1 byte ('M'/'F')
    student_name VARCHAR2(50),             -- Shrinks dynamically to fit exact names
    cgpa NUMBER(3,2),                      -- Maximum value 9.99 (Perfect for GPA)
    date_of_joining DATE,                  -- Tracks both date and time values
    semester_bio CLOB                      -- Unlimited rich text for biographies
);

-- Step 2: Test automatic rounding mechanics on scale variables
INSERT INTO student_profile VALUES 
(100001, 'M', 'Amit Das', 8.576, SYSDATE, 'Enrolled via scholarship path.');

-- Step 3: Check how the system stores the CGPA decimal
SELECT roll_id, student_name, cgpa FROM student_profile;

Copy and open Oracle FreeSQL
Oracle FreeSQL is a free online editor for Oracle SQL. The code is copied first: paste it there and run it.

Think first

Trace the Rounding and Scale Evaluation

Look at Step 2. We inserted a CGPA value of 8.576 into a column defined as NUMBER(3,2). Will this insert fail? If it passes, what exact value will be saved in the database?

Show the answer

The statement will execute successfully without any errors! Because the scale is set to 2, the RDBMS automatically inspects the third decimal place and rounds the number up to fit the coordinate rule. Therefore, the value stored permanently inside the column will be 8.58.

Quiz

If you define a column as CHAR(10) and insert the string 'BCA', how many bytes of physical database storage will that specific row cell consume?

  1. 3 bytes, because VARCHAR/CHAR types always shrink down to eliminate overhead values.
  2. 10 bytes, because it appends 7 invisible trailing space characters to fulfill the fixed-length contract.
  3. 13 bytes, combining the input characters with the target length size parameter.
  4. It triggers a DataWidthExceededException validation error.
Show the answer

10 bytes, because it appends 7 invisible trailing space characters to fulfill the fixed-length contract.

The CHAR data type enforces a strict fixed-length rule. If the input string is shorter than the declared size, the system automatically pads the remaining block with blank spaces, consuming the full size limit (10 bytes) on disk.

Quiz

What is the key functional difference between VARCHAR and VARCHAR2 in Oracle SQL systems?

  1. VARCHAR holds only numbers, while VARCHAR2 holds alpha-numeric character values.
  2. VARCHAR is a legacy type that is currently identical to VARCHAR2, but VARCHAR2 is guaranteed to remain backward compatible and variable-optimized in future updates.
  3. VARCHAR2 can only store binary image data streams up to 2 Gigabytes.
  4. VARCHAR supports multilingual global characters, while VARCHAR2 completely blocks them.
Show the answer

VARCHAR is a legacy type that is currently identical to VARCHAR2, but VARCHAR2 is guaranteed to remain backward compatible and variable-optimized in future updates.

While they behave similarly right now, Oracle specifically mandates the use of VARCHAR2 over VARCHAR. VARCHAR is technically reserved for future SQL standard updates where its behavior might change, whereas VARCHAR2 is guaranteed to remain a dynamic, space-saving variable text field.

Theory

Connecting Data Types to Semester 3 PL/SQL Engineering

When you move to Semester 3 advanced database scripting (BCA301), choosing correct types saves you from data translation errors. You will learn to use the dynamic anchoring attribute %TYPE. This tells your backend code to automatically copy the data types of your table columns (v_name student_profile.student_name%TYPE), making your programs highly maintainable even if table widths change later.

Summary

Key takeaways

  • Data types define the explicit validation rules and physical storage profiles of table columns.
  • CHAR enforces fixed-width layouts and pads unused blocks with empty spaces, causing storage waste if misused.
  • VARCHAR2 scales dynamically to match the length of the string, optimizing storage usage.
  • NUMBER(p,s) offers precise decimal point configuration, preventing arithmetic distortion in financial computations.
  • CLOB and NCLOB handle heavy textual data up to 4GB, replacing restrictive, legacy LONG architectures.
  • Memory Hook: Fixed codes use CHAR, variable text requires VARCHAR2, and always track precision rules for numbers!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Advanced SQL

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati