Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezIuv-0006aL-Vn for pgsql-general@arkaria.postgresql.org; Fri, 23 Mar 2018 09:23:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ezIuu-0000Q4-UJ for pgsql-general@arkaria.postgresql.org; Fri, 23 Mar 2018 09:23:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezIuu-0000Pq-M2 for pgsql-general@lists.postgresql.org; Fri, 23 Mar 2018 09:23:20 +0000 Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=smadev.internal.net) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ezIur-0001xw-GO for pgsql-general@lists.postgresql.org; Fri, 23 Mar 2018 09:23:20 +0000 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id w2N9NDdA060273 for ; Fri, 23 Mar 2018 11:23:14 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) From: Achilleas Mantzios Subject: Re: primary key and unique index To: pgsql-general@lists.postgresql.org References: Message-ID: Date: Fri, 23 Mar 2018 11:23:13 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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