/ THE IDEA
A primary key must be unique and not missing. It identifies the customer record even when a name changes or two customers have identical names. A foreign key on orders.customer_id checks the connection to the customers table; the repeated customer number is a reference, not a second independent customer identity. The rules also matter when deleting records. If an existing order refers to customer 7, the database needs an explicit policy for removing customer 7. It might reject the deletion, remove dependent orders too, or clear the reference where permitted. Those choices have different business meanings.
THE FORMAL IDEA
orders.customer_id REFERENCES customers.id
| orders and customers name two tables. customer_id and id name columns containing customer numbers. | | REFERENCES declares the relationship that the database must check. The referenced column must have an appropriate uniqueness guarantee; the foreign-key column need not itself be unique. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Customers contain primary-key values 7 and 8. Insert order 101 with customer_id = 7: accepted. Insert order 102 with customer_id = 9: rejected, because customer 9 is absent. Change the input by adding a valid customer row with id = 9. Retry order 102: accepted. Orders 101 and 103 may both refer to customer 7 without violating this foreign key.
|
One customer can have several orders, but each non-missing reference must point to an existing allowed customer value. The rule protects the relationship regardless of which application or import process tries to write the order. It is stronger than a warning shown only by one screen.
/ SO WHAT?
This helps separate identity from description. A customer number says which stored customer is intended; a name or email describes that customer and might change. Choosing a stable identifier reduces the number of dependent records that must be rewritten after an ordinary profile edit. It also makes accidental ambiguity easier to detect.
ONE CAVEAT |
| A foreign key does not prove that the chosen customer is the correct real person; number 8 can exist and still be the wrong selection. Also, a nullable foreign-key field may permit no reference at all, so require NOT NULL separately when every order must name a customer. |
KEEP THIS
Use identity rules to reject impossible references, while remembering that an existing reference can still describe the wrong real-world choice.
|
NEXT: Why warming sealed air raises its pressure
|