FOREIGN KEY constraint: rules and methods
The FOREIGN KEY constraint creates and enforces a reference link between 2 columns. It ensures that a referencing column (the child) can only contain values already present in its referenced column (the parent).
A FOREIGN KEY is defined within the child table, its REFERENCES keyword points to the parent table and its specific columns. Multiple child rows can reference the same parent column value.
//The child column must match the parent data type
//A PRIMARY KEY parent column avoids any duplicate column value and NULL for the child
CREATE TABLE products (
chiave integer PRIMARY KEY, name text, price numeric
);
insert into products(chiave, name, price) values (15, 'brio',550), (10, 'malbo',200);
//Any existing constraint still applies within the child table
//If the parent column is omitted, it will use its own column name for the reference.
CREATE TABLE orders (
order integer, //UNIQUE can limit the child columns values
chiavetta integer REFERENCES products (chiave) //Will use chiavetta if omitted
);
//The FOREIGN KEY only allows values form the specified column, not the entire table.
insert into orders(order, chiavetta) values
(3, 15),
(10, 10), //The parent reference value can be repeated within the row
(3, 15), //Allowed if teh order child columns isn't UNIQUE
(3, 200); //Error, it violates the foreign key constraintA child FOREIGN KEY must reference UNIQUE or PRIMARY KEY parent columns. The JOIN operation combines table columns data using the FOREIGN KEY relations.
Referencing multiple parent column using the composite FOREIGN KEY constraint
A composite FOREIGN KEY references a unique set of parent columns. All its child columns must be referenced from a single, explicitly defined composite constraint.
The FOREIGN KEY clause can omit the parent columns list if they reference a PRIMARY KEY constraint; the database will implicitly map the child columns by position. While the UNIQUE parent columns must always be explicitly listed.
Invalid FOREIGN KEY constraints:
A FOREIGN KEY can't reference a subset of columns from a composite parent constraint. A unique composite set can contain repeated individual column values (like (1, 10) and (1, 20)), making the database unable to identify parent rows based on a single referenced column.
A composite FOREIGN KEY cannot reference multiple independent parent columns. It must reference a single composite constraint that contains all the parent columns within its unique set.
We can't directly modify a foreign key; we must first DROP and ADD a named new one within the child table. Any new foreign key added must reference existing constraints in the parent table.
Self-referencing tables and the FOREING KEY relationships types.
A column that references the PRIMARY KEY of a row within the same table is called self-referencing. It creates a hierarchical structure where rows are organized into branches based on the PRIMARY KEY they reference, with rows containing NULL foreign key values serving as the root nodes that originate the structure. It enabled the query to retrieve and order data based on the relations between the table rows.
A query involving self-referencing columns uses multiple instances of the same table within its FROM clause. The first instance (t1) represents the current row being processed, while the second instance (t2) is used in the matching operation. This comparison links the foreign key value from the first instance to its referenced parent primary key, found using the second instance.
The LEFT JOIN clause includes all rows from the current table instance (the left operand) in the query's output. It appends the result columns from the matching operation, assigning NULL to unmatched rows. It effectively flattens the output, presenting both child and parent information within the same row.
There are multipe types of tables relationships, based on their cardinality:
It requires an extra linking table that references the parent primary keys in its child columns. The composite primary key combines the foreign key columns, with each column referencing a different table.
Parent column values are defined once but can be referenced many times in the linking table. Any update will only affect the parent table it's declared in, with no need to change its corresponding reference in the linking table.
Each parent PRIMARY KEY column can be referenced by multiple child columns. The FOREIGN KEY can accept repeated values that reference the same parent.
Each parent PRIMARY KEY column is referenced by a single unique FOREIGN KEY. A specific child column value can be related to a speific parent only once.
Referential Actions and Event Specifiers for Referential Integrity
The database restricts any change made to parent columns currently being referenced. The FOREIGN KEYS must follow the referencial integrity rule, which states that all non-NULL child values must references an existing parent column.
The event specifiers set the parent row actions that will trigger a child's column response. The referential actions then define how the foreign key values will be modified to maintain the referential integrity after parent changes.
The CASCADE option, with the ON DELETE event specifier, ensures that deleting a parent row also deletes all its related child rows, preventing any unassigned non-NULL child values.
The RESTRICT option, set by default, prevents the deletion of any parent row while it's being referenced by a child row.
The NO ACTION option prevents parent operations, symilar to RESTRICT. It's the only referential action that can be combined with the DEFERRABLE clause, which enables it to delay the referential integrity check until the end of its transaction. It allows subsequent operations within the same transaction to resolve any potential integrity violations before the final commit.
The DEFERRABLE clause, by itself, results in an INITIALLY IMMEDIATE integrity check. We need to explicitly specify the INITIALLY DEFERRED clause to enable the delayed referential check.
Many database editors group multiple operations into a single transaction, allowing a DEFERRED clause to place its referential integrity check at the transaction's final commit.
We can change the FOREIGN KEY's current DEFERRABLE setting using the SET CONSTRAINTS command.
The SET CONSTRAINTS command can dynamically change the DEFERRABLE behavior of a named NO ACTION constraint. The command will be applied only to the specific set of operations it's included in.
The SET NULL and SET DEFAULT options maintain referential integrity by changing the foreign key column values within the child rows.
The SET NULL option, applied ON DELETE, sets the foreign key columns in the child row to NULL when its referenced parent row gets deleted. The NULL value, following the referential integrity rule, implicitly removes the parent-child reference in its FOREIGN KEY column.
The SET DEFAULT option sets the foreign key columns to their predefined default values when its referencing parent row is deleted. The referential integrity is maintained only if its non-NULL default values reference an existing parent column.
The event specifiers must still satisfy any pre-existing constraints on their foreign key child columns.
Identifying Foreign Keys used as a child Primary Key
Both event specifiers and referential actions can't be directly applied to PRIMARY KEY columns.
We call identifying foreign keys the foreign keys columns that are used as PRIMARY KEYS values. They will be implicitly set as NOT NULL and UNIQUE, while also needing to reference existing parent column values. This dual role limits the types of referential actions that can be applied to them.
We can add a SET NULL option to an identifying FOREING KEY, but the database won't execute it. If a referenced parent row is deleted, the foreign key columns cannot be updated to NULL because of their implicit NOT NULL constraint when used as primary keys.
The CASCADE option will delete the entire child row of the identifying foreign key. It implicitly satisfies the primary key's NOT NULL constraint by removing the entire row, and it will delete all other columns included in the primary key, regardless of their individual options.
1
1
1
The SET DEFAULT option usually doesn't work with an identifying foreign key. The default value must satisfy the UNIQUE constraint among the existing identifying foreign key values.
The NO ACTION DEFERRABLE initially DEFERRED option can be applied to an identifying foreign key. It allows for multiple operations in a single transaction involving the child column value.
Defining a Foreign Key Hierarchy in a Child Table.
We can create a hierarchy of multiple foreign keys within the same child table, where each is defined by the specific referential action triggered by its parent table event.
We define the main foreign key column as part of the composite primary key. The foreign key's CASCADE referential action establishes the referenced table as the main parent, and makes the specific foreign key a necessary component in the child table.
We define the secondary child column as part of a composite foreign key referencing another table. The foreign key's SET NULL referencial action defines the parent columns's values as non-essential for the child table. We can remove the reference link by deleting its specified foreign key columns values, while maintaining the rest of the child row and preserving the specified identifying foreign key.
Foreign keys ON UPDATE actions and the MATCH FULL option.
The ON UPDATE event specifier implements its referential actions differently from ON DELETE. Because the parent row still exists after the update, the database must either maintain the reference to the updated parent row or delete the foreign key's reference without deleting the entire child row.
The CASCADE option maintains the referential integrity by updating the child column values to match the new parent column.
The SET NULL option updates all the child columns related to the updated parent row to NULL. It maintains the referential integrity by using NULL to indicate a removed foreign key reference.
The MATCH SIMPLE option, set by default, considers a foreign key not part of any reference if any of its columns are NULL. The MATCH FULL option requires all foreign key columns to be NULL for it to be considered unreferenced. It blocks any operation that might result in a partially NULL foreign key, such as SET NULL with specified columns, or SET DEFAULT where some default values are NULL.
The values of the PRIMARY KEY and UNIQUE parent columns are implicitly stored in an index structure. It validates the child columns that reference the parent's values, but it doesn't store any data about the relation.
Referential actions like CASCADE, SET NULL, and SET DEFAULT involve locating child column values after parent row operations. We use an additional index on the foreign key columns to optimize the retrieval.
The foreign key constraint specifies the rules for column values, but it is not a column definition that can directly include an index statement.
Last updated