SMALLINT
: A 2-byte integer.
INTEGER
(or INT
): A 4-byte integer.
BIGINT
: An 8-byte integer.
SERIAL
, BIGSERIAL
, SMALLSERIAL
: Auto-incrementing integer types.
SERIAL
: 4-byte integer with auto-increment.
BIGSERIAL
: 8-byte integer with auto-increment.
SMALLINT
(2 bytes), INTEGER
(4 bytes), BIGINT
(8 bytes). Choosing the right type can save storage space.SMALLINT
for large values will result in an overflow error.SERIAL
types are convenient for primary keys, but be aware of the maximum limits for each type (SERIAL
for INTEGER
, BIGSERIAL
for BIGINT
).SMALLINT
: For small-range integer values to save space (e.g., age, count).INTEGER
: For general-purpose whole numbers (e.g., IDs, quantities).BIGINT
: For large-range integer values (e.g., financial transactions, large datasets).SERIAL
: For auto-incrementing primary keys with INTEGER
range.BIGSERIAL
: For auto-incrementing primary keys with BIGINT
range.REAL
: A 4-byte single-precision floating-point number.
DOUBLE PRECISION
: An 8-byte double-precision floating-point number.
REAL
has less precision than DOUBLE PRECISION
. Use DOUBLE PRECISION
for calculations requiring high precision.REAL
(4 bytes), DOUBLE PRECISION
(8 bytes).NUMERIC
.REAL
: For approximate values where precision is not critical and storage space is a concern (e.g., scientific measurements).DOUBLE PRECISION
: For scientific calculations requiring higher precision (e.g., financial models, simulations).NUMERIC
(or DECIMAL
): Stores exact numbers with an arbitrary precision.
NUMERIC(p, s)
where p
is the total number of digits and s
is the number of digits to the right of the decimal point.NUMERIC(p, s)
allows you to define the precision (p
total digits) and scale (s
digits after the decimal point). This makes it suitable for financial calculations where exact values are crucial.NUMERIC
: For financial and monetary data requiring exact precision (e.g., currency values, financial calculations).SMALLINT
: Small-range integers, 2 bytes.INTEGER
: General-purpose integers, 4 bytes.BIGINT
: Large-range integers, 8 bytes.SERIAL
: Auto-incrementing integers, 4 bytes.BIGSERIAL
: Auto-incrementing large integers, 8 bytes.REAL
: Single-precision floating-point, 4 bytes.DOUBLE PRECISION
: Double-precision floating-point, 8 bytes.NUMERIC
(or DECIMAL
): Exact numbers with arbitrary precision.