Subject: Re: Lookup tables

From: Thiemo Kellner <[email-masked]>
Date: Tue, 4 Feb 2025 18:27:28 +0100 (GMT+01:00)

Unless the lookup table is actually a check constraint one can use to populate dropdown boxes in an interface.

Cheers

Thiemo

---

From: Michał Kłeczek <[email-masked]>
Date: Tue, 4 Feb 2025 18:30:53 +0100

> On 4 Feb 2025, at 18:27, Thiemo Kellner <[email-masked]> wrote:
> 

That is even worse because it ceases being transactional and users might select something different than what they see on the screen.

—
Michał

---

From: Karsten Hilbert <[email-masked]>
Date: Tue, 4 Feb 2025 18:52:34 +0100

> That is even worse because it ceases being transactional and users might select something different than what they see on the screen.

I never thought of that, thanks.

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B

---

From: "David G. Johnston" <[email-masked]>
Date: Tue, 4 Feb 2025 11:20:53 -0700

On Tuesday, February 4, 2025, Michał Kłeczek <[email-masked]> wrote:

> That is even worse because it ceases being transactional and users might
> select something different than what they see on the screen.
>

What is the process flow for this happening?  How big a bug needs to exist
in the system for something bad to happen as opposed to some kind of “value
not found” error and a refresh with the correct matching values showing up
in the UI?

David J.

---

From: Thiemo Kellner <[email-masked]>
Date: Tue, 4 Feb 2025 20:03:27 +0100 (GMT+01:00)

> That is even worse because it ceases being transactional and users might select something different than what they see on the screen.

In how far is a real check constraint less transactional? And in how far is it more advisable to have a real check constraint and fill your dropdown boxes from another source and having to keep that source on sync with the real check constraint?

---

From: Thiemo Kellner <[email-masked]>
Date: Tue, 4 Feb 2025 22:41:38 +0100 (GMT+01:00)

> That is even worse because it ceases being transactional and users might select something different than what they see on the screen.

I might see what you want to point out. E.g. the table is COLOURS. The rec with id 1 is RED, the one with id 2 is BLUE, 3 is GREE and so on. Now you load these values into the dropdown box that sports RED, BLUE, GREE and so on. While someone selects GREE, there is a maintenance release changing GREE to YELLOW. So when that someone sends the selection by id to the backend, not GREE is selected but YELLOW.

A) Your release changed the sementics of the record 3. It's meaning changed. I cannot recommend doing that.
B) If you absolutely must change the semantic, put your application into maintenance mode in which noone can select anything beforehand.

If the maintenance would just correct the typo from GREE to GREEN, nothing would happen. Yor customer still ordered the lavishly green E-Bike her hear ever desired.

---

From: Karsten Hilbert <[email-masked]>
Date: Tue, 4 Feb 2025 22:44:19 +0100

> I might see what you want to point out. E.g. the table is COLOURS. The rec with id 1 is RED, the one with id 2 is BLUE, 3 is GREE and so on. Now you load these values into the dropdown box that spo…

Yep, that's what I meant and which I never thought of before.

Karsten

---

From: Michał Kłeczek <[email-masked]>
Date: Wed, 5 Feb 2025 13:55:51 +0100

> On 4 Feb 2025, at 22:41, Thiemo Kellner <[email-masked]> wrote:
> 

That’s what using natural keys and FK’s restricting their changes guarantee: no (accidental) changes to meaning of data.
Even with cascading updates you still have transactional semantics (ie. the user selects what’s on the screen or gets an error).

> B) If you absolutely must change the semantic, put your application into maintenance mode in which noone can select anything beforehand.

All this error prone hassle and downtime can be avoided with natural keys and guarantees that DBMS gives you.

> If the maintenance would just correct the typo from GREE to GREEN, nothing would happen. Yor customer still ordered the lavishly green E-Bike her hear ever desired.

The question is: how do you _ensure_ that?

—
Michal

---

From: Thiemo Kellner <[email-masked]>
Date: Wed, 5 Feb 2025 21:33:38 +0100

> Even with cascading updates you still have transactional semantics (ie. the user selects what’s on the screen or gets an error).
Sorry, that is utter nonsense. You cannot ever guarantee an update does 
not mess up the semantics on the updated field, change the meaning. You 
would need a check constraint which in it turn needs to get set up where 
one can mess up things.
> All this error prone hassle and downtime can be avoided with natural keys and guarantees that DBMS gives you.
And I thought you would have denied the need of changing semantics 
above. And no, changing your natural keys semantically ALWAYS requires 
downtime to make sure you do not run into the race condition described 
above.
> The question is: how do you _ensure_ that?
Ensure, the update goes from GREE to GREEN? You cannot, simple as that. 
You just can minimize the risk by testing, testing, testing. But that 
holds equally true for the business key of a surrogate key table as 
natural key table. That's why the surrogate key is such an elegant 
construct. You can change business key of the record with id 3 from GREE 
to GREEN, VERT, GRÜN, VERDE or ASéLDHK()*NSLDFHP)(*Z . It keeps its 
meaning of the perception of the human eye of electromagnetic waves of 
the wavelength roughly between 495-570 nm (according to Wikipedia).

---

From: Michał Kłeczek <[email-masked]>
Date: Wed, 5 Feb 2025 22:19:30 +0100

> On 5 Feb 2025, at 21:33, Thiemo Kellner <[email-masked]> wrote:
> 
> 
> Sorry, that is utter nonsense. You cannot ever guarantee an update does not mess up the semantics on the updated field, change the meaning. Y

But you can guarantee that if you change the value of the key after the user displays it - the user will get an error on submission (whereas with the surrogate key it would happily proceed without user noticing).

> ou would need a check constraint which in it turn needs to get set up where one can mess up things.
> And I thought you would have denied the need of changing semantics above. And no, changing your natural keys semantically ALWAYS requires downtime to make sure you do not run into the race condition described above.

How so? The user is going to get FK violation - you do not need any downtime to make sure users don’t submit wrong values.

> Ensure, the update goes from GREE to GREEN? You cannot, simple as that. You just can minimize the risk by testing, testing, testing.

You can also simply disallow updates with FK constraint eliminating risk.

> But that holds equally true for the business key of a surrogate key table as natural key table. That's why the surrogate key is such an elegant construct. You can change business key of the record with id 3 from GREE to GREEN, VERT, GRÜN, VERDE or ASéLDHK()*NSLDFHP)(*Z . It keeps its meaning of the perception of the human eye of electromagnetic waves of the wavelength roughly between 495-570 nm (according to Wikipedia).

And why do you think unconstrained updating of business key is a good thing?
You must implement rules governing what can and what cannot be changed *somewhere* - not doing it in the database means you have to do it in applications.

Anyway - let’s agree to disagree :)

—
Michal

---

From: Thiemo Kellner <[email-masked]>
Date: Wed, 5 Feb 2025 23:36:10 +0100

> But you can guarantee that if you change the value of the key after the user displays it - the user will get an error on submission (whereas with the surrogate key it would happily proceed without…
As you very rightly say happily proceed, because that is, what you 
actually want. The user chose GREE meaning green, which is updated in 
the meantime to GREEN. All good. Your solution throws an error for nothing.
> How so? The user is going to get FK violation - you do not need any downtime to make sure users don’t submit wrong values.
Sorry, I was not aware, your aim is to bother the customer with FK 
violation messages, if you can avoid it.
> You can also simply disallow updates with FK constraint eliminating risk.
This is nothing that is specific to surrogate or natural keys. If one 
disallow updates, one has to live with typos and everything.
> And why do you think unconstrained updating of business key is a good thing?
I am not sure what is your take on "unconstrained". As already 
mentioned, testing is the only constraint you can set up. And I am 
neither sure where your problem is with updating unless you have an 
update orgy because your ref-constraint goes on natural keys.
> You must implement rules governing what can and what cannot be changed *somewhere* - not doing it in the database means you have to do it in applications.
That sounds adventurous. Maybe I am not seeing what you mean, but is 
sound to me that you build a shadow database where you map your GREE to 
GREEN in the application layer so the application can display the 
correct value in the GUI, but no one has to update the core database. 
And if there is a typo in the shadow data base you build another mapping 
database on top of it?
> Anyway - let’s agree to disagree :)
Agreed :-)

---

From: "Peter J. Holzer" <[email-masked]>
Date: Thu, 6 Feb 2025 22:03:18 +0100

On 2025-02-04 22:41:38 +0100, Thiemo Kellner wrote:
> >>  Unless the lookup table is actually a check constraint one
> >> can use to populate dropdown boxes in an interface.
> > 
> > That is even worse because it ceases being transactional and users
> > might select something different than what they see on the screen.
> 
> I might see what you want to point out. E.g. the table is COLOURS. The
> rec with id 1 is RED, the one with id 2 is BLUE, 3 is GREE and so on.
> Now you load these values into the dropdown box that sports RED, BLUE,
> GREE and so on. While someone selects GREE, there is a maintenance
> release changing GREE to YELLOW. So when that someone sends the
> selection by id to the backend, not GREE is selected but YELLOW.

I fail to see why use of a surrogate key is the problem here.

Either changing the color from GREE to YELLOW makes sense or it doesn't.

If it doesn't make sense, then it's release which is faulty, not the
model.

if it does make sense (I'm a bit at a loss when that might be the case,
maybe the "color" is just a code word, or maybe they are colors in a
design which are arbitrary but must be consistent), then the experience
that the user has is exactly the same as if the maintenance release was
applied just after they selected the color. Which might be a bit
confusing but is almost certainly what is wanted.

> changed. I cannot recommend doing that.

If the release changed the semantics of an existing record the release
was almost certainly wrong.

> B) If you absolutely must change the semantic, put your application
> into maintenance mode in which noone can select anything beforehand.
> 
> If the maintenance would just correct the typo from GREE to GREEN,
> nothing would happen. Yor customer still ordered the lavishly green
> E-Bike her hear ever desired.

Yeah, that's a good example where changing the color from GREE to YELLOW
doesn't make sense. Presumably that ID 3 is used as a foreign key in
lots of places, e,g. in an inventory table. Your bikes in stock won't
just magically change color just because you changed some text in the
database. So that change simply doesn't make sense and shouldn't be done
as part of a maintenance release. Confusing a few people who just happen
to open the dropdown in the wrong second is the least of your problems.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | [email-masked]         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

---

From: Michał Kłeczek <[email-masked]>
Date: Fri, 7 Feb 2025 09:22:13 +0100

> On 6 Feb 2025, at 22:03, Peter J. Holzer <[email-masked]> wrote:
> 

This is simply saying “what is implemented is certainly wanted, so what’s the point”.

The discussion is about *defining* what is wanted and using DBMS to *enforce* that.

> was almost certainly wrong.

Is it possible to minimize the risk of “wrong releases” using mechanisms that DBMS provides?

> to open the dropdown in the wrong second is the least of your problems.

We are in agreement here. What I am also saying is:
having color name as PK and all FKs referencing it *prevents* these unwanted updates.

—
Michal

---

From: "Peter J. Holzer" <[email-masked]>
Date: Fri, 7 Feb 2025 19:42:08 +0100

On 2025-02-07 09:22:13 +0100, Michał Kłeczek wrote:
> This is simply saying “what is implemented is certainly wanted,

Not at all. It may or may not be what is wanted but we can't tell unless
we know what is wanted. We have a single table without any context. What
does this table represent? What is the identity here? If it's the color
itself, changing green to yellow doesn't make any sense ("it's the same
color, but now it's yellow instead of green" is self-contradictory). If
the identity has some application-defined semantics, it may make sense.

> so what’s the point”.

Indeed. Without knowing the semantics discussing the data model or the
advisability of certain updates is indeed rather pointless.
Disagreements will be more likely to be due to unstated assumptions
about the application than because of general principles.

> Is it possible to minimize the risk of “wrong releases” using mechanisms that

Maybe. But the person who thought that

    update colors set name = 'YELLOW' where id = 3; -- YELLOW is the new GREE

was a good idea might also think that 

    update doodads set color = 'YELLOW' where color = 'GREE';

is a good idea.

Is it? I don't know. Maybe it is. Maybe it isn't. There is no way to
know unless we know the requirements.

> having color name as PK and all FKs referencing it *prevents* these unwanted

I'm not convinced.

        hp