CockroachDB supports various signed integer data types.
Names and Aliases
Name | Allowed Width | Aliases |
---|---|---|
INT |
64-bit | INTEGER INT8 INT64 BIGINT |
INT4 |
32-bit | None |
INT2 |
16-bit | SMALLINT |
BIT |
1-bit | None |
BIT(n) |
n-bit | None |
Syntax
A constant value of type INT
can be entered as a numeric literal.
For example: 42
, -1234
, or 0xCAFE
.
Size
The different integer types place different constraints on the range of allowable values, but all integers are stored in the same way regardless of type. Smaller values take up less space than larger ones (based on the numeric value, not the data type).
You can use the BIT(n)
type, with n
from 1 to 64, to constrain integers based on their corresponding binary values. For example, BIT(5)
would allow 31
because it corresponds to the five-digit binary integer 11111
, but would not allow 32
because it corresponds to the six-digit binary integer 100000
, which is 1 bit too long. See the example below for a demonstration.
BIT
values are input and displayed in decimal format by default like all other integers, not in binary format. Also note that BIT
is equivalent to BIT(1)
.Examples
> CREATE TABLE ints (a INT PRIMARY KEY, b SMALLINT, c BIT(5));
> SHOW COLUMNS FROM ints;
+-------+----------+-------+---------+-------------+
| Field | Type | Null | Default | Indices |
+-------+----------+-------+---------+-------------+
| a | INT | false | NULL | {"primary"} |
| b | SMALLINT | true | NULL | {} |
| c | BIT(5) | true | NULL | {} |
+-------+----------+-------+---------+-------------+
(3 rows)
> INSERT INTO ints VALUES (1, 32, 32);
pq: bit string too long for type BIT(5) (column "c")
> INSERT INTO ints VALUES (1, 32, 31);
INSERT 1
> SELECT * FROM ints;
+---+----+----+
| a | b | c |
+---+----+----+
| 1 | 32 | 31 |
+---+----+----+
(1 row)
Supported Casting & Conversion
INT
values can be cast to any of the following data types:
Type | Details |
---|---|
DECIMAL |
–– |
FLOAT |
Loses precision if the INT value is larger than 2^53 in magnitude |
BOOL |
0 converts to false ; all other values convert to true |
DATE |
Converts to days since the Unix epoch (Jan. 1, 1970) |
TIMESTAMP |
Converts to seconds since the Unix epoch (Jan. 1, 1970) |
INTERVAL |
Converts to microseconds |
STRING |
–– |