> ## 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.

# NOT NULL constraint

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>;
};

The `NOT NULL` <InternalLink path="constraints">constraint</InternalLink> specifies a column may not contain <InternalLink path="null-handling">`NULL`</InternalLink> values.

## Details

* `INSERT` or `UPDATE` statements containing `NULL` values are rejected. This includes `INSERT` statements that do not include values for any columns that do not have a <InternalLink path="default-value">`DEFAULT` value constraint</InternalLink>.

For example, if the table `foo` has columns `a` and `b` (and `b` *does not* have a `DEFAULT VALUE`), when you run the following command:

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

CockroachDB tries to write a `NULL` value into column `b`. If that column has the `NOT NULL` constraint, the `INSERT` statement is rejected.

* To add the `NOT NULL` constraint to an existing table column, use the <InternalLink path="alter-table#set-not-null-constraint">`ALTER COLUMN`</InternalLink> statement.
* For more information about `NULL`, see <InternalLink path="null-handling">NULL handling</InternalLink>.

## Syntax

You can only apply the `NOT NULL` constraint to individual columns.

<img src="https://mintcdn.com/cockroachlabs/gtjMfbHiKZTUwAsm/images/sql-diagrams/v25.2/not_null_column_level.svg?fit=max&auto=format&n=gtjMfbHiKZTUwAsm&q=85&s=af164f2a61a43b8e5b8193c69807bc09" alt="not_null_column_level syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="745" height="217" data-path="images/sql-diagrams/v25.2/not_null_column_level.svg" />

| Parameter             | Description                                                                                                          |
| --------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `table\_name`         | The name of the table you're creating.                                                                               |
| `column\_name`        | The name of the constrained column.                                                                                  |
| `column\_type`        | The constrained column's <InternalLink path="data-types">data type</InternalLink>.                                   |
| `column\_constraints` | Any other column-level <InternalLink path="constraints">constraints</InternalLink> you want to apply to this column. |
| `column\_def`         | Definitions for any other columns in the table.                                                                      |
| `table\_constraints`  | Any table-level <InternalLink path="constraints">constraints</InternalLink> you want to apply.                       |

## Usage example

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE TABLE IF NOT EXISTS customers (
    customer_id INT         PRIMARY KEY,
    cust_name   STRING(30)  NULL,
    cust_email  STRING(100) NOT NULL
  );
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> INSERT INTO customers (customer_id, cust_name, cust_email) VALUES (1, 'Smith', NULL);
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
pq: null value in column "cust_email" violates not-null constraint
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> INSERT INTO customers (customer_id, cust_name) VALUES (1, 'Smith');
```

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
pq: null value in column "cust_email" violates not-null constraint
```

## See also

* <InternalLink path="constraints">Constraints</InternalLink>
* <InternalLink path="alter-table#drop-constraint">`DROP CONSTRAINT`</InternalLink>
* <InternalLink path="check">`CHECK` constraint</InternalLink>
* <InternalLink path="default-value">`DEFAULT` constraint</InternalLink>
* <InternalLink path="foreign-key">`REFERENCES` constraint (Foreign Key)</InternalLink>
* <InternalLink path="primary-key">`PRIMARY KEY` constraint</InternalLink>
* <InternalLink path="unique">`UNIQUE` constraint</InternalLink>
* <InternalLink path="show-constraints">`SHOW CONSTRAINTS`</InternalLink>
* <InternalLink path="null-handling">`NULL HANDLING`</InternalLink>
