Data Types


In DB2, data types define the type of data that can be stored in a table's column. They ensure that only valid data is stored in the database and help optimize storage and retrieval. DB2 supports a wide range of data types, grouped into categories such as numeric, string, date/time, and large objects (LOBs).

Numeric Data Types -


Data TypeDescriptionExample
INTEGER (INT)Stores whole numbers (positive or negative)INT column to store Employee_ID = 101
SMALLINTStores smaller whole numbers, requiring less storageSMALLINT column for Age = 25
BIGINTStores very large integersBIGINT for tracking high-value counters like Population = 1000000000
DECIMAL (DEC)Stores fixed-point numbers with precisionDEC(10,2) for Salary = 50000.75
FLOAT/DOUBLEStores approximate floating-point numbersFLOAT for scientific data like Temperature = 36.6

String Data Types -


String data types store text or character data, such as names, addresses, or descriptions.

Data TypeDescriptionExample
CHAR (n)Stores fixed-length textCHAR(5) for storing Country_Code = 'IND'
VARCHAR (n) Stores variable-length textVARCHAR(50) for Name = 'Alice'
CLOB (Character Large Object)Stores large text data (e.g., documents)CLOB for storing articles or descriptions.
GRAPHIC / VARGRAPHICStores fixed/variable-length graphic strings (e.g., for non-English characters)Used for multi-language names.

Date and Time Data Types -


These data types store date, time, and timestamp information. They are useful for tracking events, deadlines, and timestamps.

Data TypeDescriptionExample
DATEStores date in YYYY-MM-DD formatDATE column for Hire_Date = '2024-10-25'
TIMEStores time in HH:MM:SS formatTIME for Start_Time = '09:00:00'
TIMESTAMPStores date and time with fractional secondsTIMESTAMP for Order_Placed = '2024-10-25 10:15:30.123'

Large Object (LOB) Data Types -


LOB types store large amounts of data, such as images, videos, or long text.

Data TypeDescriptionExample
BLOB (Binary Large Object)Stores large binary data (e.g., images)BLOB for storing a product image
CLOB (Character Large Object)Stores large text data (e.g., documents)CLOB for storing reports.
DBCLOB (Double-Byte Character Large Object)Stores double-byte character text (e.g., multi-language content)DBCLOB for Chinese or Japanese text.

Boolean Data Type -


Data TypeDescriptionExample
BOOLEANStores true/false values, often used for flagsTRUE or FALSE

XML Data Type -


Data TypeDescriptionExample
XMLUsed to store XML documents and data in XML formatOrder_Details column stores order information in XML format

User-Defined Data Types (UDTs) -


DB2 allows the creation of custom data types based on existing types. This provides more control over the kind of data stored. Example - The PhoneNumber data type ensures all phone numbers follow a specific format.

CREATE DISTINCT TYPE PhoneNumber AS CHAR(10);
CREATE TABLE Contacts (
    Contact_ID INT PRIMARY KEY,
    Phone PhoneNumber
);

Each data type serves a specific purpose, from storing numbers and text to managing large objects and timestamps. Choosing the right data types ensures data integrity and optimal performance.