Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tfLpi-001iAW-Gc for pgsql-general@arkaria.postgresql.org; Tue, 04 Feb 2025 16:31:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tfLph-005L98-CD for pgsql-general@arkaria.postgresql.org; Tue, 04 Feb 2025 16:31:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tfLph-005L90-03 for pgsql-general@lists.postgresql.org; Tue, 04 Feb 2025 16:31:29 +0000 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tfLpe-003GPJ-0t for pgsql-general@postgresql.org; Tue, 04 Feb 2025 16:31:28 +0000 Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-ab6ed8a5a04so964444566b.3 for ; Tue, 04 Feb 2025 08:31:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kleczek.org; s=google; t=1738686686; x=1739291486; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=MG7swinlh3ixvBIU/TcIgoKdQpOvvTnEFsFjUUzLV7g=; b=g/r49eCCJDW/R+n3CGasfvlsA9ypoG/VuUl8tFrZpvVK6TynN3r9/ShBGMDbFPPD9O Hm8zlNR8S8ek1YrdPBp2RFUkUjUhkbJLHdqln+zORG05dgogDCxlVr/tsBLq1eSfJR0I wJlY+U5Nlp/4lVSHm50b4LbBP/9eFvtAsq8nQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738686686; x=1739291486; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MG7swinlh3ixvBIU/TcIgoKdQpOvvTnEFsFjUUzLV7g=; b=Kj0SG1NbE7PVOifTfK+DMUnw8PrZZxEPD7I0TNS5eLxnKO3VQXCgDYX/NaUcZOcsWS c6dmGkq6IIR7VAq2TYHki7T48OsYFZW5ljSh7+Sei+iTAD9dPW3SkMSEEsfZvHVq0f9w lJGeJnxKYFT7iNSVRrnC1rLiL6nX+eMbrBEFogyTjQdN/vV0YXb3Isn0bLTS+4wG++m6 vheyPLQls1SXOyZanUHWTm5rle6jzOM2SBuBE5a6e9kKpG9GdYrKYeqICIDemkPFHUx1 XZVTogZ3aE7W3yP5OpppVFsLwsy8isyrv7cGL0djSY/GLvfdRQYz0hTOB8A6HR7wfr/i Ez/Q== X-Gm-Message-State: AOJu0YyC/6p00LbwN6yNDXYS/TBYvL6r44RABLz68XChw/1z+9oZa1e5 Y8zjupV7PxcSYxJ//8cijjPxiNpdHRJVDZVlrVIg+sdkO8eHngjnuoY1ajJfQuM= X-Gm-Gg: ASbGncvZFnXp5bU8nElcrvH9ygRbXcy/xStcictp/ItRXnbFBwQnTtxPSx3WQ0B23NS PbZGvsVimlvZMubub+URdkpB0BUYpwxDEOBPq7FXenbC6/aywnSPDHONR8hL+QFZlMXGOUIfdim tHScz5zvTOFT5M2hFsFHKjqlcS+pgMfrGk6AeWCCMykP4SvVOl/lSWtpcRLx5HKgKKsqDcwkt3r /eW8UjuFA8L0xLO5bGr3+2kQqBUJNiYAaZg4Arw/hrvcEj0+BjZyAt/PUZp4HU7DCZrCHR5Y6+8 R5zi0zir3A0oITdVDDayzd9qK+KuzGtf2lEJXFPXITU= X-Google-Smtp-Source: AGHT+IGa8yaC8EZQf46IYEuCXgPFhQbk6r7DZnlUqUKqywIhw8ePabAhsuazcTnz0K3bVw16jffu1A== X-Received: by 2002:a17:907:8688:b0:ab6:dd6c:e30c with SMTP id a640c23a62f3a-ab6dd6ce41cmr2868911466b.45.1738686685299; Tue, 04 Feb 2025 08:31:25 -0800 (PST) Received: from smtpclient.apple (b15.kleczek.org. [185.20.172.182]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab6e47cf29fsm948982966b.38.2025.02.04.08.31.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Feb 2025 08:31:24 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.300.87.4.3\)) Subject: Re: Lookup tables From: =?utf-8?Q?Micha=C5=82_K=C5=82eczek?= In-Reply-To: <4e7a338-c7d3-e944-20bd-a6e346e175c7@appl-ecosys.com> Date: Tue, 4 Feb 2025 17:31:13 +0100 Cc: PG-General Mailing List Content-Transfer-Encoding: quoted-printable Message-Id: References: <4e7a338-c7d3-e944-20bd-a6e346e175c7@appl-ecosys.com> To: Rich Shepard X-Mailer: Apple Mail (2.3826.300.87.4.3) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 4 Feb 2025, at 15:27, Rich Shepard = wrote: >=20 > Should lookup tables have a numeric FK column as well as the = description column? >=20 > If so, how should I add an FK to the two lookup tables in my database? I=E2=80=99ve 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=E2=80=99s 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 =3D $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=E2=80=99t represent = facts but rather some entities that have identity independent from their = attributes. Thanks, Michal