Sub topic 2
Choosing Data Types for Columns
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and that you understand tables, rows, and columns. This chapter uses PostgreSQL-compatible SQL. Other database systems use many of the same types, but their exact names and behavior can differ.
A column's data type defines which values the database accepts and how it stores, compares, and calculates those values. Choosing the narrowest suitable type helps prevent invalid data and makes queries clearer.
Match the type to the meaning
Use integer types for whole-number quantities, such as counts or identifiers. Use fixed-precision numeric types for exact values, such as prices. Do not use floating-point types for money because binary rounding can produce unexpected results.
Use character types for text. VARCHAR(n) limits the number of characters, while TEXT allows longer text. A length limit is useful when it expresses a real business rule, such as a two-letter country code. Otherwise, an artificial limit can reject valid future data.
Use DATE when only a calendar date matters. Use TIMESTAMP when both date and time matter. Time-zone requirements need an explicit design decision. For an event that represents one instant worldwide, use a time-zone-aware timestamp where your database supports it.
| Data type | Good use | Example |
|---|---|---|
| `INTEGER` | Whole numbers | `quantity INTEGER` |
| `NUMERIC(10, 2)` | Exact decimal values | `price NUMERIC(10, 2)` |
| `VARCHAR(20)` | Short text with a real limit | `status VARCHAR(20)` |
| `DATE` | Calendar dates | `birth_date DATE` |
| `BOOLEAN` | True or false state | `is_active BOOLEAN` |
Define columns with compatible types
The following table stores products. The CHECK constraints belong to later chapters, but they show how a data type and a business rule work together.
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price NUMERIC(10, 2) NOT NULL CHECK (price >= 0),
quantity INTEGER NOT NULL CHECK (quantity >= 0),
released_on DATE,
is_active BOOLEAN NOT NULL DEFAULT TRUE
);NUMERIC(10, 2) allows up to ten total digits, with two digits after the decimal point. Its largest positive value is 99,999,999.99. Choose precision and scale based on the domain, not merely on today's sample data.
A type does not always enforce the full meaning of a value. An INTEGER accepts negative quantities, and a VARCHAR(20) accepts any twenty-character string. Add NOT NULL, CHECK, UNIQUE, or foreign-key constraints when the data requires additional rules.
Avoid types that hide meaning
Storing a number as text prevents reliable arithmetic and numeric ordering. Storing a date as text makes date comparisons and date functions harder to use. Split values when they have independent meaning. For example, store a quantity and a unit price in separate numeric columns instead of storing "3 × $12.50" in one text column.
Follow the value through the schema
Related columns should use compatible types. A foreign-key column should normally have the same type as the referenced primary key. If one side is INTEGER and the other is text, joins and inserts become error-prone.
Exercise
Which type is the better default for a product price: FLOAT or NUMERIC(10, 2), and why?
Show answer
NUMERIC(10, 2), because it stores decimal values exactly to the declared precision and scale. FLOAT can introduce binary rounding errors.