pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Achilleas Mantzios <achill@matrix.gatewaynet.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: primary key and unique index
Date: Fri, 23 Mar 2018 11:23:13 +0200
Message-ID: <c3e4185d-19c5-623a-d00f-37f2069b5b5c@matrix.gatewaynet.com> (raw)
In-Reply-To: <CAN_ctnhdQAe3n7NgCvHUTAb1MQzZnHoNjDSPwNThWk_b9N4wZQ@mail.gmail.com>
References: <CAN_ctnhdQAe3n7NgCvHUTAb1MQzZnHoNjDSPwNThWk_b9N4wZQ@mail.gmail.com>

On 23/03/2018 09:55, Thomas Poty wrote:
> Hi all,
>
> I am migrating fromMySQL to Postgresql 9.6.
>
> In MySQL a  "show create table" gives me :
>  ...
>   PRIMARY KEY (`ID`,`CountryCode`,`LanguageCode`),
>   UNIQUE KEY `unique_my table_4` (`ID`,`CountryCode`,`LanguageCode`),
> ...
>
> So, In PostgreSQL, does it make sense to create a primary key AND a unique index based on the same columns?
> Is PostgreSQL smart enough to use the unique index created for the primary key.
>

This is redundant. However, IMO it is always beneficial to have an bigint PK, set implicitly via a sequence.
So you could have smth like :

pkid BIGSERIAL PRIMARY KEY,
.......
UNIQUE KEY unique_my table_4 ("ID","CountryCode","LanguageCode"),
...
This way, you get the artificial bigint PK (pkid), and the explicit natural unique key which enforces your business integrity.

> I know PostgreSQL can be based on a unique index to create a primary key but I also know it is possible to create several indexes on the same columns with the same order.
>
> Thanks
>
> Thomas


-- 
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt





view thread (5+ messages)  latest in thread

Message-ID: <c3e4185d-19c5-623a-d00f-37f2069b5b5c@matrix.gatewaynet.com>
Permalink:  ../c3e4185d-19c5-623a-d00f-37f2069b5b5c@matrix.gatewaynet.com/
Also on:    postgresql.org/message-id/c3e4185d-19c5-623a-d00f-37f2069b5b5c@matrix.gatewaynet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: achill@matrix.gatewaynet.com
  Subject: Re: primary key and unique index
  In-Reply-To: <c3e4185d-19c5-623a-d00f-37f2069b5b5c@matrix.gatewaynet.com>
  References: <CAN_ctnhdQAe3n7NgCvHUTAb1MQzZnHoNjDSPwNThWk_b9N4wZQ@mail.gmail.com> <c3e4185d-19c5-623a-d00f-37f2069b5b5c@matrix.gatewaynet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox