Re: Lookup tables

From: Michał Kłeczek <michal(at)kleczek(dot)org>
To: Rich Shepard <rshepard(at)appl-ecosys(dot)com>
Cc: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Lookup tables
Date: 2025-02-04 16:31:13
Message-ID: C7F5BFD3-929B-46CC-B20C-893189F154D5@kleczek.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgadmin-support pgsql-general

> On 4 Feb 2025, at 15:27, Rich Shepard <rshepard(at)appl-ecosys(dot)com> wrote:
>
> Should lookup tables have a numeric FK column as well as the description column?
>
> If so, how should I add an FK to the two lookup tables in my database?

I’ve read the whole thread and the reasoning for having (numeric) autogenerated surrogate key is:
a) performance
b) no cascading updates

I would like to add another dimension to this discussion: logical consistency.

Imagine the following simplified schema with surrogate PK:

create table restaurant (
restaurant_id int not null primary key generated always as identity,
name text not null unique
);
create table restaurant_visit (
visit_id int not null primary key generated always as identity,
guest_username text not null,
when date not null,
restaurant_id not null foreign key restaurant(restaurant_id),
rating smallint not null
);

Let’s say users on your website register restaurant visits and rate them.

The query to register a visit is:
insert into restaurant_visit
select $user, current_date, restaurant_id, $rating
from restaurant where name = $restaurant_name

It is now completely unclear what it means to change the name of the restaurant for already registered visits.
Is it still the same restaurant with a different name or a different restaurant?

Or let say someone swaps names of two restaurants.
That means a user that goes to the same restaurant every day would register visits to two different restaurants!

Using the name of a restaurant as primary key gets rid of these logical anomalies because
the database model now reflects facts from reality.

Having surrogate keys makes your relational database more like a network/object oriented database where rows don’t represent facts but rather some entities that have identity independent from their attributes.

Thanks,
Michal

In response to

Responses

Browse pgadmin-support by date

  From Date Subject
Next Message Karsten Hilbert 2025-02-04 16:51:14 Re: Lookup tables
Previous Message Adrian Klaver 2025-02-04 16:06:00 Re: Lookup tables

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2025-02-04 16:34:17 Re: Index usage with differing string types
Previous Message Adrian Klaver 2025-02-04 16:29:06 Re: Index usage with differing string types