INT

On this page Carat arrow pointing down
Warning:
CockroachDB v2.0 is no longer supported as of October 4, 2019. For more details, refer to the Release Support Policy.

CockroachDB supports various signed integer data types.

Note:

For instructions showing how to auto-generate integer values (e.g., to auto-number rows in a table), see this FAQ entry.

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.

Note:
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

icon/buttons/copy
> CREATE TABLE ints (a INT PRIMARY KEY, b SMALLINT, c BIT(5));
icon/buttons/copy
> 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)
icon/buttons/copy
> INSERT INTO ints VALUES (1, 32, 32);
pq: bit string too long for type BIT(5) (column "c")
icon/buttons/copy
> INSERT INTO ints VALUES (1, 32, 31);
INSERT 1
icon/buttons/copy
> 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). This is a CockroachDB experimental feature which may be changed without notice.
TIMESTAMP Converts to seconds since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice.
INTERVAL Converts to microseconds
STRING ––

See Also

Data Types


Yes No
On this page

Yes No