Before we continue, let's review what we already know about numbers. Integers are expressed in the following way: 1
, 2
, 43
, -27
, etc. Fractions are expressed with a dot (.
): 12.45
, -401.238
, etc. Please note that in some languages, you use a comma instead of a dot, but not in SQL! For example, 12,45
is incorrect.
In PostgreSQL (and in most programming languages) there are three kinds of numbers:
- Integer (
integer
): stores integers (e.g., -5, 0, 5, 42).
- Floating-point (
real
, double precision
): stores fractions by approximating them in binary.
- Decimal (
numeric
): stores fractions in decimal format.
In this part, we'll discuss some of the differences between PostgreSQL's number data types.
Of course, you can use the four basic mathematical operations (add +
, subtract -
, multiply *
, and divide /
) with actual numbers in PostgreSQL. You can also use columns and constant numbers together. Take a look at the example below:
SELECT hp / 4 AS quarter_hp
FROM character;