PLAIN/TEXT — ISSUE 157/456 COMPUTER SCIENCE · FOUNDATION / DATA / RELATIONSHIPS · ~4 MIN READING DATE: 2027-03-07 ARCHIVE: https://plaintext.click/newsletter/ SUBJECT: How a database refuses an order with no customer How a database refuses an order with no customer ================================================ / START HERE Two customers can share a name, so a shop gives each a different customer number. Their details are stored in a table, a grid with one customer record per horizontal row. A primary key is a column, or set of columns, required to identify each row uniquely and never be missing. A foreign key is a rule requiring a reference in one table to match an allowed identifying value in another. / WHY NOW These rules turn some data mistakes into immediate refusals. An order referring to a customer who does not exist can be stopped before the mistake reaches a delivery report. / 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. / SOURCE TRAIL [1] PostgreSQL — Primary and foreign-key constraints https://www.postgresql.org/docs/18/ddl-constraints.html NEXT: Why warming sealed air raises its pressure --- HARD IDEAS. PLAIN TEXT. You are receiving this because you asked for one clear computer-science or physics idea each day. Unsubscribe: https://plaintext.click/unsubscribe View in browser: https://plaintext.click/newsletter/2027/03/2027-03-07-primary-foreign-keys.html From: Plain/Text / Brickston Consulting · 30 N Gould St Ste N, Sheridan, WY 82801 US · knowledge@plaintext.click