Foreign Keys as References Between Tables
A foreign key checks that a referenced value has a corresponding key under the declared relational constraint, with rules for nulls and changes.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace an order row
An order carrying customer ID 4 can reference an existing customer row. A supplied ID with no match fails the relevant constraint at its checking time. Compare in-memory keyed joins for a lookup over two collections.
Choose deletion behavior
If a customer row is removed, policy can restrict the action or define how related rows change. List this effect with the relationship contract and verify a customer with dependent rows.[1][2]
Try one existing target and one absent target
A booking's room ID references the room table. Inserting a booking for room 8 is valid if room 8 exists. Inserting a booking for room 19 triggers a foreign-key failure under the stated constraint when no such row exists. See deleting a referenced parent.
rooms: 8, 12
booking with room_id 8 → reference exists
booking with room_id 19 → rejected
nullable room_id follows its declared rule
Define whether a reference is required
In common SQL semantics a nullable single-column foreign key permits null. Pair it with a presence constraint if a booking must always have a room. Test both null and a non-null absent target.