Sub topic 2
Picking Data Types for Books, Authors, and Dates
This chapter assumes you know basic SELECT, INSERT, UPDATE, and DELETE statements. You also need a running PostgreSQL 12 or newer database and a basic understanding of tables, rows, and columns.
A data type tells PostgreSQL what a column can store. Choosing the type carefully prevents invalid values and makes queries clearer. For a book tracker, the important choices include text, numbers, dates, and a fixed set of reading statuses.
Start with the values the application needs
The database stores different kinds of information:
- Author names and book titles are text.
- ISBNs look numeric, but they are identifiers, not numbers.
- Page counts and publication years are whole numbers.
- Reading dates are calendar dates.
- A reading status must come from a small, known list.
The following table summarizes suitable PostgreSQL types.
| Value | PostgreSQL type | Reason |
|---|---|---|
| Book title | `text` | Titles have variable lengths |
| ISBN-13 | `char(13)` | Every valid ISBN-13 has 13 digits |
| Publication year | `smallint` | Years fit comfortably in a small integer |
| Page count | `integer` | Page counts are whole numbers |
| Started date | `date` | A time of day is not needed |
| Reading status | enum | Only approved status values are allowed |
Use text for names and titles
PostgreSQL's text type stores variable-length character strings. It is a good default for author names and book titles. PostgreSQL does not impose a useful maximum length on text, so add a separate length rule later if your application needs one.
An ISBN should not use an integer type. ISBNs identify books rather than represent quantities. Treating an ISBN as a number could remove leading zeros and makes formatting or validation less clear. The char(13) type reserves space for 13 characters. You can add a check that requires exactly 13 digits.
Use the smallest practical numeric type
smallint stores smaller whole numbers, so it suits publication years. integer is appropriate for page counts. A book with more than 2.1 billion pages is not realistic, so integer provides plenty of room.
Identity columns commonly use bigint. PostgreSQL generates a new number for each row. The generated number identifies the row; it does not describe the book itself.
CREATE TYPE reading_status AS ENUM (
'want_to_read',
'reading',
'finished',
'abandoned'
);
CREATE TABLE authors (
author_id bigint GENERATED ALWAYS AS IDENTITY,
name text
);
CREATE TABLE books (
book_id bigint GENERATED ALWAYS AS IDENTITY,
title text,
isbn13 char(13),
published_year smallint,
page_count integer
);
CREATE TABLE readings (
reading_id bigint GENERATED ALWAYS AS IDENTITY,
book_id bigint,
status reading_status,
started_on date,
finished_on date,
rating smallint
);Choose dates based on meaning
The date type stores a calendar date such as 2026-11-27. It does not store a time zone or time of day, which is correct for “started reading” and “finished reading.”
Use timestamp when an event needs a date and time but does not represent a moment across time zones. Use timestamptz for events that happen at a specific instant, such as an import or a user login. Do not use a timestamp merely because it seems more precise. Extra precision can make comparisons and user-facing output harder.
The type choices form a simple model: books and authors contain descriptive values, while readings contain dates and a controlled status.
Exercise
Why should an ISBN-13 be stored as text-like data instead of an integer?
Show answer
An ISBN is an identifier. Text-like storage preserves leading zeros and makes digit validation and formatting explicit.