# Column Constraints

A `TableIndex` can be created directly from a [`Blueprint`](/8.4.0/schema-builder/creating-table-constraints.md) or from a existing [`Column`](/8.4.0/schema-builder/column-modifiers.md). The `TableIndex` includes methods for further configuring the index which is required when defining foreign keys.

## references

Set the referencing column for a foreign key relationship. For example, `id` for a `country_id` column.

| Argument | Type | Required | Default | Description                                                             |
| -------- | ---- | -------- | ------- | ----------------------------------------------------------------------- |
| columns  | any  | `true`   |         | A column or array of columns that represents the foreign key reference. |

**Example:**

**SchemaBuilder**

```
schema.create( "users", function( table ) {
    table.unsignedInteger( "country_id" ).references( "id" ).onTable( "countries" );
} );
```

**SQL (MySQL)**

```sql
CREATE TABLE `users` (
    `country_id` INTEGER UNSIGNED NOT NULL,
    CONSTRAINT `fk_users_country_id` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
```

## onTable

Sets the referencing table for a foreign key relationship. For example, `countries` for a `country_id` column.

| Argument | Type   | Required | Default | Description                 |
| -------- | ------ | -------- | ------- | --------------------------- |
| table    | string | `true`   |         | The referencing table name. |

**Example:**

**SchemaBuilder**

```
schema.create( "users", function( table ) {
    table.unsignedInteger( "country_id" ).references( "id" ).onTable( "countries" );
} );
```

**SQL (MySQL)**

```sql
CREATE TABLE `users` (
    `country_id` INTEGER UNSIGNED NOT NULL,
    CONSTRAINT `fk_users_country_id` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
```

## onUpdate

Set the strategy for updating foreign keys when the parent key is updated.

| Argument | Type   | Required | Default | Description                                                                                    |
| -------- | ------ | -------- | ------- | ---------------------------------------------------------------------------------------------- |
| option   | string | `true`   |         | The strategy to use. Available values are: RESTRICT, CASCADE, SET NULL, NO ACTION, SET DEFAULT |

**Example:**

**SchemaBuilder**

```
schema.create( "users", function( table ) {
    table.unsignedInteger( "country_id" )
        .references( "id" )
        .onTable( "countries" )
        .onUpdate( "CASCADE" );
} );
```

**SQL (MySQL)**

```sql
CREATE TABLE `users` (
    `country_id` INTEGER UNSIGNED NOT NULL,
    CONSTRAINT `fk_users_country_id` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON UPDATE CASCADE ON DELETE NO ACTION
)
```

## onDelete

Set the strategy for updating foreign keys when the parent key is deleted.

| Argument | Type   | Required | Default | Description                                                                                    |
| -------- | ------ | -------- | ------- | ---------------------------------------------------------------------------------------------- |
| option   | string | `true`   |         | The strategy to use. Available values are: RESTRICT, CASCADE, SET NULL, NO ACTION, SET DEFAULT |

**Example:**

**SchemaBuilder**

```
schema.create( "users", function( table ) {
    table.unsignedInteger( "country_id" )
        .references( "id" )
        .onTable( "countries" )
        .onDelete( "SET NULL" );
} );
```

**SQL (MySQL)**

```sql
CREATE TABLE `users` (
    `country_id` INTEGER UNSIGNED NOT NULL,
    CONSTRAINT `fk_users_country_id` FOREIGN KEY (`country_id`) REFERENCES `countries` (`id`) ON UPDATE NO ACTION ON DELETE SET NULL
)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qb.ortusbooks.com/8.4.0/schema-builder/column-constraints.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
