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

# Default Value 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 `DEFAULT` value <InternalLink path="constraints">constraint</InternalLink> specifies a value to write into the constrained column if one is not defined in an `INSERT` statement. The value may be either a hard-coded literal or an expression that is evaluated at the time the row is created.

## Details

* The <InternalLink path="data-types">data type</InternalLink> of the `DEFAULT` value must be the same as the data type of the column.
* The `DEFAULT` value constraint only applies if the column does not have a value specified in the <InternalLink path="insert">`INSERT`</InternalLink> statement. You can still insert a `NULL` into an optional (nullable) column by explicitly inserting `NULL`. For example, `INSERT INTO foo VALUES (1, NULL);`.

## Syntax

<img src="https://mintcdn.com/cockroachlabs/Nqtj0HvOrM_ugxgN/images/sql-diagrams/v26.2/default_value_column_level.svg?fit=max&auto=format&n=Nqtj0HvOrM_ugxgN&q=85&s=d30042d04989b5d5e09298f8bb31855d" alt="default_value_column_level syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="717" height="249" data-path="images/sql-diagrams/v26.2/default_value_column_level.svg" />

You can only apply the `DEFAULT` value constraint to individual columns.

<Note>
  You can also add the `DEFAULT` value constraint to an existing table through <InternalLink path="alter-table#set-or-change-a-default-value">`ALTER COLUMN`</InternalLink>.
</Note>

| 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>.                                                                      |
| `default_value`      | The value you want to insert by default, which must evaluate to the same <InternalLink path="data-types">data type</InternalLink> as the `column_type`. |
| `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.                                                          |

## Example

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE TABLE inventories (
    product_id        INT,
    warehouse_id      INT,
    quantity_on_hand  INT DEFAULT 100,
    PRIMARY KEY (product_id, warehouse_id)
  );
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> INSERT INTO inventories (product_id, warehouse_id) VALUES (1,20);
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> INSERT INTO inventories (product_id, warehouse_id, quantity_on_hand) VALUES (2,30, NULL);
```

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

```
+------------+--------------+------------------+
| product_id | warehouse_id | quantity_on_hand |
+------------+--------------+------------------+
|          1 |           20 |              100 |
|          2 |           30 | NULL             |
+------------+--------------+------------------+
```

If the `DEFAULT` value constraint is not specified and an explicit value is not given, a value of `NULL` is assigned to the column.

## See also

* <InternalLink path="constraints">Constraints</InternalLink>
* <InternalLink path="alter-table#alter-column">`ALTER COLUMN`</InternalLink>
* <InternalLink path="check">`CHECK` constraint</InternalLink>
* <InternalLink path="foreign-key">`REFERENCES` constraint (Foreign Key)</InternalLink>
* <InternalLink path="not-null">`NOT NULL` constraint</InternalLink>
* <InternalLink path="primary-key">`PRIMARY KEY` constraint</InternalLink>
* <InternalLink path="unique">`UNIQUE` constraint</InternalLink>
* <InternalLink path="show-constraints">`SHOW CONSTRAINTS`</InternalLink>
