Skip to content

Numeric types: INTEGER, BIGINT, NUMERIC, DOUBLE

View as Markdown

CoraDB supports the following numeric data types for integers and real numbers.

Type Bytes Min Max Exact / approximate
SHORT, SMALLINT 2 -32,768 32,767 Exact
INTEGER, INT 4 -2,147,483,648 2,147,483,647 Exact
BIGINT 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 Exact
NUMERIC, DECIMAL, DEC 16 Precision 1, scale 0 Precision 38, scale 38 Exact
FLOAT, REAL 4 -3.402823466E+38 3.402823466E+38 Approximate, 7 significant digits
DOUBLE, DOUBLE PRECISION 8 -1.7976931348623157E+308 1.7976931348623157E+308 Approximate, 15 significant digits
  • Exact types (SMALLINT, INT, BIGINT, NUMERIC) store the value as entered (after rounding to the declared scale). Use them for money, quantities and anything that must add up exactly.
  • Approximate types (FLOAT, DOUBLE) follow IEEE 754 and keep only a fixed number of significant digits. Use them for measurements and scientific values, not for amounts that are compared for equality.
  • CoraDB has no UNSIGNED modifier. INT UNSIGNED is a syntax error.
  • Names that share a row in the table are synonyms. SHOW COLUMNS always reports the first name, and a NUMERIC column always with its precision and scale:
CREATE TABLE num_types (s SMALLINT, r REAL, f10 FLOAT(10), d DOUBLE PRECISION, p DEC(6,2), q DECIMAL);
SHOW COLUMNS FROM num_types;
Field Type
s SHORT
r FLOAT
f10 DOUBLE
d DOUBLE
p NUMERIC(6,2)
q NUMERIC(15,0)

In the e-commerce sample database, product.id is INTEGER, product.unit_price is NUMERIC(10,2) and product.units_in_stock is SHORT.

The precision of a numeric type is its number of significant digits. The scale is the number of digits after the decimal point and only applies to NUMERIC.

  • SMALLINT, INTEGER and BIGINT have scale 0 and a precision fixed by the system (5, 10 and 19).
  • NUMERIC has a precision from 1 to 38 and a scale from 0 up to the precision.
  • FLOAT has 7 significant digits, DOUBLE has 15.

A numeric literal can carry a sign (+ or -), a decimal point and an exponent (1e3, 2.5E-4). Its type follows from how it is written:

Literal Type
Integer that fits in INTEGER (100) INTEGER
Larger integer (2147483648) BIGINT
Number with a decimal point (12.5) NUMERIC with the digits as written, here numeric (3, 1)
Number with an exponent (1e3) DOUBLE
SELECT TYPEOF(100) AS a, TYPEOF(2147483648) AS b, TYPEOF(12.5) AS c, TYPEOF(1e3) AS d;
a b c d
integer bigint Numeric (3, 1) double

The maximum precision of a numeric literal is 255 digits.

All numeric values can be compared and combined with each other. When the operands of an expression have different numeric types, CoraDB converts them to a common type first. In general the result takes the type that can hold both operands: integer types widen to a larger integer type, then to NUMERIC, and any FLOAT or DOUBLE operand makes the result approximate. Use CAST to pick the type yourself.

The value of a product's stock is unit_price * units_in_stock, a NUMERIC(10,2) times a SHORT, so the result is an exact NUMERIC with two decimals. Dividing by 3 keeps NUMERIC with a scale of 9; converting the price to DOUBLE first gives an approximate floating-point result:

SELECT id, unit_price, units_in_stock,
unit_price * units_in_stock AS stock_value,
TYPEOF(unit_price * units_in_stock) AS value_type,
unit_price / 3 AS third_numeric,
CAST(unit_price AS DOUBLE) / 3 AS third_double
FROM product
WHERE id IN (1618, 1620)
ORDER BY id;
id unit_price units_in_stock stock_value value_type third_numeric third_double
1618 29.00 22 638.00 Numeric (26, 2) 9.666666667 9.666666666666666
1620 49.00 4 196.00 Numeric (26, 2) 16.333333333 16.333333333333332

How the precision and scale of a NUMERIC result are computed for each operator is described in Arithmetic operations and type casting of numeric data types.

The product of two INTEGER values is an INTEGER, so a result above 2,147,483,647 is an error even though it would fit in a BIGINT:

SELECT 100000000 * 1000000;
ERROR: Data overflow on data type integer.

Cast one operand to BIGINT and the whole expression is computed as BIGINT:

SELECT CAST(100000000 AS BIGINT) * 1000000 AS total;
total
100000000000000

The same applies to aggregates over small integer columns: if a total may be large, cast the column to a wider type inside the aggregate, for example SUM(CAST(units_in_stock AS BIGINT)).

INTEGER stores whole numbers from -2,147,483,648 to 2,147,483,647. INTEGER and INT are synonyms, and a DEFAULT value can be declared for a column of this type.

A value with a fractional part is rounded half away from zero to the nearest integer when it is stored. A value outside the range is rejected.

Value stored in an INT column Result
8934 8934
89.8 90
-89.5 -90
3458901122 Error: Cannot coerce 3458901122 to type integer.

SMALLINT stores whole numbers from -32,768 to 32,767. SMALLINT and SHORT are synonyms, and a DEFAULT value can be declared for a column of this type. Fractional values are rounded like INTEGER.

Value stored in a SMALLINT column Result
8934 8934
34.5 35
-34.5 -35
89354 Error: Cannot coerce 89354 to type smallint.

BIGINT stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. A DEFAULT value can be declared for a column of this type. Fractional values are rounded like INTEGER.

Value stored in a BIGINT column Result
8934 8934
89.1 89
89.8 90
3458901122 3458901122

By range and precision, the exact types are ordered SMALLINT < INTEGER < BIGINT < NUMERIC.

NUMERIC stores fixed-point numbers with a declared precision p (total number of digits) and scale s (digits after the decimal point). NUMERIC, DECIMAL and DEC are synonyms.

NUMERIC [(p [, s])]
  • p is between 1 and 38. When omitted it is 15.
  • s is between 0 and p. When omitted it is 0, so NUMERIC without arguments is NUMERIC(15,0) and stores whole numbers.
  • The integer part of a value can have at most p - s digits. Extra decimals are rounded half away from zero to s places.
  • A DEFAULT value can be declared for a column of this type.
Declaration and value Result
12345.6789 in NUMERIC 12346
12345.6789 in NUMERIC(8,2) 12345.68
12345.6789 in NUMERIC(4) Error: Cannot coerce 12345.6789 to type numeric.
0.12345678 in NUMERIC(4,4) 0.1235
-0.123456789 in NUMERIC(4,4) -0.1235
Declaring NUMERIC(3, 4) Error: Invalid combination of precision (3) and scale (4).

The same rounding applies to CAST:

SELECT CAST(12345.6789 AS NUMERIC(8,2)) AS a, CAST(-0.123456789 AS NUMERIC(4,4)) AS b;
a b
12345.68 -0.1235

FLOAT (or REAL) is a single-precision IEEE 754 floating-point number with 7 significant digits. Normalized values range from ±1.175494351E-38 to ±3.402823466E+38, plus 0; values closer to 0 are stored as denormalized numbers.

FLOAT [(p)]
  • p is between 1 and 38. When p is omitted or is 7 or less, the column is single-precision FLOAT.
  • When p is between 8 and 38, the column is created as DOUBLE (15 significant digits).
  • FLOAT and REAL are synonyms, and a DEFAULT value can be declared for a column of this type.

Digits beyond the seventh are lost, so do not use FLOAT for values that must be exact, and avoid comparing FLOAT values for equality.

SELECT CAST(16777217 AS FLOAT) AS a, CAST(16777.217 AS FLOAT) AS b, CAST(16777.217 AS DOUBLE) AS c;
a b c
1.677721e+7 16777.21 16777.217

DOUBLE is a double-precision IEEE 754 floating-point number with 15 significant digits. Normalized values range from ±2.2250738585072014E-308 to ±1.7976931348623157E+308, plus 0; values closer to 0 are stored as denormalized numbers. DOUBLE takes no precision argument.

  • DOUBLE and DOUBLE PRECISION are synonyms, and a DEFAULT value can be declared for a column of this type.
  • Like FLOAT, DOUBLE stores an approximation: integers above 2^53 can no longer all be represented exactly.
SELECT CAST(1234.56789 AS DOUBLE) AS a, CAST(9007199254740993 AS DOUBLE) AS b;
a b
1234.56789 9007199254740992