Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYYNi-000462-VU for pgsql-general@arkaria.postgresql.org; Tue, 12 May 2020 17:07:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jYYNh-000296-BD for pgsql-general@arkaria.postgresql.org; Tue, 12 May 2020 17:07:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYYNh-00028z-3x for pgsql-general@lists.postgresql.org; Tue, 12 May 2020 17:07:49 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYYNc-0001Ge-CH for pgsql-general@lists.postgresql.org; Tue, 12 May 2020 17:07:48 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 9A8EC3037; Tue, 12 May 2020 19:07:41 +0200 (CEST) Date: Tue, 12 May 2020 19:07:41 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Enforcing uniqueness on [real estate/postal] addresses Message-ID: <20200512170741.GA11324@hjp.at> References: <87o8qu0ygd.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="qDbXVdCdHGoSgWSk" Content-Disposition: inline In-Reply-To: <87o8qu0ygd.fsf@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --qDbXVdCdHGoSgWSk Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-05-12 10:49:22 +1000, Tim Cross wrote: > Peter Devoy writes: > > I need to store addresses for properties (as in real estate) so in my > > naivety I created a unique constraint like this: > > > > ALTER TABLE properties > > ADD CONSTRAINT is_unique_address > > UNIQUE ( > > description, --e.g. Land north of Foo Cottage > > address_identifier_general, > > street, > > postcode > > ); > > > > Of course, if any of the fields are NULL (which they often are) I end > > up with duplicates. [...] >=20 > Personally, I don't like the idea of using empty strings just to avoid > having nulls. This is probably a personal preference, but for me null > and '' are quite different. A null indicates an unknown - we don't know > what the value is. An empty string i.e. '' means there is no value (i.e. > we know it has no value). The difference is quite subtle and may not > seem relevant. It may not be or it may be or it may become relevant in > the future. General rule of thumb for me is that my model should reflect > the known information and should always avoid any data transformation or > mapping which reduces the known information. >=20 > I would step back a bit and think about why/what constraint you really > need and what needs to be unique. The first field which jumps out for me > is description. Is this really a unique value? As the coonstraint stands, it isn't. Only description, address_identifier_general, street and postcode together are unique. I'd therefore ask the question in the other direction: Is is possible to have two entries which have the same address_identifier_general, street and postcode, but different descriptions? What does that mean? To different properties which happen to be at the same place or two descriptions for the same property?=20 (What is an address_identifier_general, btw?) I agree with the rest of posting. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --qDbXVdCdHGoSgWSk Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl6619kACgkQ8g5IURL+ KF088w/9EZOE7vzmGhBDl2RkNwd7FsAynfukm7OSqiHSNfpmqHrg4vm7rahL+f/V BuMYpN5la9soW8SfTZeLk/8sr7t/TXEB9fDQZnYuKBGGN/w0nHoajHP9bPYWPx1x aUPjmMpA9qXI6DHdHNaoCEbxVulza2E7nbYn/BSmi91er8bDAl6mVWzZIRgLqhQu lA1JmislrTFR1jk8f+HAiE7aOMQLz3mNhxOSu1OPJR3jSNjAPvRkWjl2sEoOIBlK eXCV+YNzUnlSu6PzhcnDtlt+yOxK1f7yX9bdCUuA0/nO3sb5zWMgO0ZetC7DSJpx U2KANq7MwhqHmvuFwNzeZwRjJsrF8/wEd75U60lAZjN4DydusWRMsxOJ9I7icSRa pmUJd/FrJBWZZjWN+hZTQhCi21SwlIjsnoZiodE9Wl+ZmW6Tz8wTPjA/Uv56xq4O fjFh0Ilve+SyBiVfbCaXri+8NDtH2iCEa5F/f4o6LJTmvZvKaZ8FccTI8Sp/X9Dl G0vDEmfwBOYVBZiTkwp7e5cMAdRaqZ+sBlquaan+MMd7gyZOl0NL/3dGQaZap0y7 syTvSfmqAhcTQiR4PSuL+rGasmKfWzqpc6QDscEe3ADL5qVy+4r8/5XFztv/i5/d 3ZE8WvNLfFKJr7EXuQOeNTQrogWVJ02qwEdMGYB0pI+6/VkRUkM= =aeld -----END PGP SIGNATURE----- --qDbXVdCdHGoSgWSk--