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 Type | Description | Example |
---|---|---|
INTEGER (INT) | Stores whole numbers (positive or negative) | INT column to store Employee_ID = 101 |
SMALLINT | Stores smaller whole numbers, requiring less storage | SMALLINT column for Age = 25 |
BIGINT | Stores very large integers | BIGINT for tracking high-value counters like Population = 1000000000 |
DECIMAL (DEC) | Stores fixed-point numbers with precision | DEC(10,2) for Salary = 50000.75 |
FLOAT/DOUBLE | Stores approximate floating-point numbers | FLOAT 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 Type | Description | Example |
---|---|---|
CHAR (n) | Stores fixed-length text | CHAR(5) for storing Country_Code = 'IND' |
VARCHAR (n) | Stores variable-length text | VARCHAR(50) for Name = 'Alice' |
CLOB (Character Large Object) | Stores large text data (e.g., documents) | CLOB for storing articles or descriptions. |
GRAPHIC / VARGRAPHIC | Stores 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 Type | Description | Example |
---|---|---|
DATE | Stores date in YYYY-MM-DD format | DATE column for Hire_Date = '2024-10-25' |
TIME | Stores time in HH:MM:SS format | TIME for Start_Time = '09:00:00' |
TIMESTAMP | Stores date and time with fractional seconds | TIMESTAMP 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 Type | Description | Example |
---|---|---|
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 Type | Description | Example |
---|---|---|
BOOLEAN | Stores true/false values, often used for flags | TRUE or FALSE |
XML Data Type -
Data Type | Description | Example |
---|---|---|
XML | Used to store XML documents and data in XML format | Order_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.