Module 00 · lesson 5

Data types: why they matter

The founder wants to work out the total value of goods sitting in stock: price times quantity. But will the multiplication even work? That depends on the data type — and data types are exactly what this lesson is about.

Every column has a type

A data type is a column's rules: what you can store in it and what you can do with it. Here are the types in our products:

TypeWhat it isColumns
TEXTtextname, category
INTEGERwhole numberstock
SERIALwhole number — an auto-number that grows by itselfid
NUMERIC(10,2)decimal number: up to 10 digits total, 2 after the point — for moneyprice
BOOLEANyes/no (true/false)is_active
DATEa datecreated_at

Why it matters

A type determines what you can do with a value:

  • numbers can be added and multiplied: e.g. price * stock gives a product's stock value;
  • text can only be joined together, not calculated;
  • dates can count the days between one another.

If the price were stored as text, multiplying it would be impossible. That's why money is always kept in NUMERIC — precise and safe.

products id name category price stock is_active created_at INTEGER TEXT TEXT NUMERIC(10,2) INTEGER BOOLEAN DATE кожен стовпець має свій типeach column has its own type cada columna tiene su tipo

id has the SERIAL type: the database itself assigns a number to every new product (1, 2, 3…), so you never enter it by hand. It's essentially an INTEGER that assigns itself — more about id in the next lesson.

See for yourself

The task has a query that computes each product's stock value (price * stock). It works only because price and stock are numbers, not text. Run it and look at the result (don't change the query itself, just run it).

We named the new column total_value (that's what the word AS does — we'll come back to it later).

Summary

  • A data type sets a column's rules. You can do math (price * stock) only on numbers; money goes in NUMERIC.
  • A type = which values are allowed + which operations are possible.