> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# INT

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

CockroachDB supports various signed integer <InternalLink path="data-types">data types</InternalLink>.

<Note>
  For instructions showing how to auto-generate integer values (e.g., to auto-number rows in a table), see <InternalLink path="sql-faqs#how-do-i-auto-generate-unique-row-ids-in-cockroachdb">this FAQ entry</InternalLink>.
</Note>

## Names and Aliases

| Name   | Allowed Width | Aliases                                          | Range                                        |
| ------ | ------------- | ------------------------------------------------ | -------------------------------------------- |
| `INT`  | 64-bit        | `INTEGER`<br />`INT8`<br />`INT64`<br />`BIGINT` | -9223372036854775807 to +9223372036854775807 |
| `INT2` | 16-bit        | `SMALLINT`                                       | -32768 to +32767                             |
| `INT4` | 32-bit        | None                                             | -2147483648 to +2147483647                   |
| `INT8` | 64-bit        | `INT`                                            | -9223372036854775807 to +9223372036854775807 |

## Syntax

A constant value of type `INT` can be entered as a <InternalLink path="sql-constants#numeric-literals">numeric literal</InternalLink>.
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).

### Considerations for 64-bit signed integers

By default, `INT` is an alias for `INT8`, which creates 64-bit signed integers. This differs from the PostgreSQL default for `INT`, [which is 32 bits](https://www.postgresql.org/docs/9.6/datatype-numeric.html), and may cause issues for your application if it is not written to handle 64-bit integers, whether due to the language your application is written in, or the ORM/framework it uses to generate SQL (if any).

For example, JavaScript language runtimes represent numbers as 64-bit floats, which means that the JS runtime [can only represent 53 bits of numeric accuracy](http://2ality.com/2012/04/number-encoding.html) and thus has a max safe value of 2<sup>53</sup>, or 9007199254740992.  This means that the maximum size of a default `INT` in CockroachDB is much larger than JavaScript can represent as an integer. Visually, the size difference is as follows:

```
9223372036854775807 # INT default max value
   9007199254740991 # JS integer max value
```

Given the above, if a table contains a column with a default-sized `INT` value, and you are reading from it or writing to it via JavaScript, you will not be able to read and write values to that column correctly. This issue can pop up in a surprising way if you are using a framework that autogenerates both frontend and backend code (such as [twirp](https://github.com/twitchtv/twirp)). In such cases, you may find that your backend code can handle 64-bit signed integers, but the generated client/frontend code cannot.

If your application needs to use an integer size that is different than the CockroachDB default (for these or other reasons), you can change one or both of the settings below. For example, you can set either of the below to `4` to cause `INT` and `SERIAL` to become aliases for `INT4` and `SERIAL4`, which use 32-bit integers.

1. The `default_int_size` <InternalLink path="set-vars">session variable</InternalLink>.
2. The `sql.defaults.default_int_size` <InternalLink path="cluster-settings">cluster setting</InternalLink>.

<Tip>
  If your application requires arbitrary precision numbers, use the <InternalLink path="decimal">`DECIMAL`</InternalLink> data type.
</Tip>

<Note>
  Use <InternalLink path="alter-role#set-default-session-variable-values-for-all-users">`ALTER ROLE ALL SET {sessionvar} = {val}`</InternalLink> instead of the `sql.defaults.*` <InternalLink path="cluster-settings">cluster settings</InternalLink>. This allows you to set a default value for all users for any <InternalLink path="set-vars">session variable</InternalLink> that applies during login, making the `sql.defaults.*` cluster settings redundant.
</Note>

## Examples

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE TABLE ints (a INT PRIMARY KEY, b SMALLINT);
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SHOW COLUMNS FROM ints;
```

```
  column_name | data_type | is_nullable | column_default | generation_expression |  indices  | is_hidden
--------------+-----------+-------------+----------------+-----------------------+-----------+------------
  a           | INT8      |    false    | NULL           |                       | {primary} |   false
  b           | INT2      |    true     | NULL           |                       | {primary} |   false
(2 rows)
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> INSERT INTO ints VALUES (1, 32);
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM ints;
```

```
  a | b
----+-----
  1 | 32
(1 row)
```

## Supported casting and conversion

`INT` values can be <InternalLink path="data-types#data-type-conversions-and-casts">cast</InternalLink> to any of the following data types:

| Type        | Details                                                                                                                                                    |
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `DECIMAL`   | ––                                                                                                                                                         |
| `FLOAT`     | Loses precision if the `INT` value is larger than 2^53 in magnitude.                                                                                       |
| `BIT`       | Converts to the binary representation of the integer value. If the value is negative, the sign bit is replicated on the left to fill the entire bit array. |
| `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 seconds.                                                                                                                                       |
| `STRING`    | ––                                                                                                                                                         |

<a id="cost-based-optimizer-kl" />

## Known limitations

* When setting the `default_int_size` <InternalLink path="set-vars">session variable</InternalLink> in a batch of statements such as `SET default_int_size='int4'; SELECT 1::INT`, the `default_int_size` variable will not take effect until the next statement. Statement parsing is asynchronous with statement execution. As a workaround, set `default_int_size` via your database driver, or ensure that `SET default_int_size` is in its own statement.

## See also

* <InternalLink path="data-types">Data Types</InternalLink>
* <InternalLink path="float">`FLOAT`</InternalLink>
* <InternalLink path="decimal">`DECIMAL`</InternalLink>
