Foreign Key Configuration
Foreign keys are important constraints in PostgreSQL databases used to establish and maintain relationships between two tables. Foreign key constraints ensure referential integrity, preventing invalid data from being inserted or updated.
Configure Foreign Keys
When creating a new column in a PostgreSQL table, you can configure a foreign key constraint for that column.
Configuration Steps
- Go to the CloudBase Console / PostgreSQL Database / Tables management page
- Select the target table
- Click "New Column" or edit an existing column
- Check "Set as Foreign Key" in the column configuration
- Configure the foreign key parameters
Foreign Key Parameters
| Parameter | Description | Required | Example |
|---|---|---|---|
| Foreign Key Name | Name of the foreign key constraint | Yes | fk_user_department |
| Referenced Table | Name of the parent table referenced by the foreign key | Yes | departments |
| Referenced Column | Name of the referenced column in the parent table | Yes | id |
| Delete Rule | Action taken when a parent record is deleted | Yes | Cascade |
| Update Rule | Action taken when the referenced field in the parent table is updated | Yes | No Action |
Delete and Update Rules
| Rule | Identifier | Description |
|---|---|---|
| No Action | NO ACTION | Rejects the operation if referenced records exist in the child table |
| Restrict | RESTRICT | Similar to NO ACTION, immediately checks and rejects the operation |
| Cascade | CASCADE | Automatically propagates the operation to all referencing records in the child table |
| Set Null | SET NULL | Sets the foreign key field in the child table to NULL |
| Set Default | SET DEFAULT | Sets the foreign key field in the child table to its default value |
tip
For more details and advanced usage of foreign key constraints, refer to the PostgreSQL Official Documentation - Foreign Keys.