docs/versioned_docs/version-3.0.0-LTS/tooljet-db/constraints/foreign-key.md
A foreign key relation refers to linking one column or set of columns of the current table with one column or set of columns in an existing table. This relationship establishes a connection between the two tables, enabling the current source table to reference the existing target table. While creating a Foreign Key relationship, you can select the desired action to be performed on the source row when the referenced(target) row is updated or deleted.
<div style={{paddingTop:'24px'}}>While creating/editing a table(target), you will be able to add one or more than one Foreign Keys referencing the column(s) of other existing(source) tables. To create a Foreign Key relationship, follow these steps:
+ Add Relation button under the Foreign key relation section.Create button to create the Foreign Key relationship.When creating a foreign key relationship, ToolJet Database lets you choose from several actions to be performed on the source row when the referenced row in the target table is updated or deleted.
| Option | Description |
|---|---|
| Restrict (default) | Restrict any updates on target table if any referenced row is being updated. |
| Cascade | Any updates in referenced row in target table will show up in the source table as well. |
| Set NULL | Any updates in referenced row in target table will set it's instances in the source table as NULL. |
| Set to Default | Any updates referenced row in target table will set it's instances in the source table as default value of foreign key column of source table. |
| Option | Description |
|---|---|
| Restrict (default) | Restrict any deletion on target table if any referenced row is being updated. |
| Cascade | Any deletion of referenced row in target table will delete the row having it's instance in the source table as well. |
| Set NULL | Any deletion of referenced row in target table will set it's instances in the source table as NULL. |
| Set to Default | Any deletion of referenced row in target table will set it's instances in the source table as default value of foreign key column of source table. |
The foreign key constraint ensures referential integrity between the source and target tables. This constraint enforces that the foreign key column in the source table has one of the unique values present in the foreign key column in the target table.
When creating a new row in the source table the column with the foreign key relation will have a dropdown with the unique values present in the target table. This ensures that the data in the source table is always consistent with the data in the target table.
On the bottom of the dropdown, there is a button to Open referenced table which will take you to the target table.
When editing the value of a foreign key cell in an existing row of the source table, the dropdown will show the unique values present in the target table. This ensures that even when the data in the source table is being updated, it is always consistent with the data in the target table.
Let's consider an example where we want to create a foreign key relationship between the Orders and Customers tables in an e-commerce application.
First, create the following two tables in the ToolJet Database:
Customers
| Column Name | Data Type | Primary Key | Not Null | Unique |
|---|---|---|---|---|
| customer_id | int | ✅ | ✅ | ✅ |
| name | varchar | ❌ | ✅ | ❌ |
| varchar | ❌ | ✅ | ✅ |
Orders
| Column Name | Data Type | Primary Key | Not Null | Unique |
|---|---|---|---|---|
| order_id | int | ✅ | ✅ | ✅ |
| customer_id | int | ❌ | ✅ | ❌ |
| order_date | varchar | ❌ | ✅ | ❌ |
| total_amount | float | ❌ | ✅ | ❌ |
We want to create a foreign key relationship between the customer_id column in the Orders table and the customer_id column in the Customers table.
Define the Foreign Key Relationship
Orders table.customer_id column.Customers table and the customer_id column.Save Changes: Click the Save Changes button to create the foreign key relationship.
Now, whenever you try to insert or update a record in the Orders table, the customer_id value must correspond to an existing customer_id value in the Customers table. This is also prevent you from deleting a customer that has associated orders. This ensures that orders are always associated with a valid customer, maintaining data integrity and consistency.