pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
primary key and unique index
5+ messages / 4 participants
[nested] [flat]

* primary key and unique index
@ 2018-03-23 07:55  Thomas Poty <thomas.poty@gmail.com>
  0 siblings, 3 replies; 5+ messages in thread

From: Thomas Poty @ 2018-03-23 07:55 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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.

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

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: primary key and unique index
@ 2018-03-23 08:10  David Rowley <david.rowley@2ndquadrant.com>
  parent: Thomas Poty <thomas.poty@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: David Rowley @ 2018-03-23 08:10 UTC (permalink / raw)
  To: Thomas Poty <thomas.poty@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

On 23 March 2018 at 20:55, Thomas Poty <thomas.poty@gmail.com> wrote:
> 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.

Doing this makes no sense in PostgreSQL.  I'm struggling to imagine
why it would in MySQL.


-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: primary key and unique index
@ 2018-03-23 09:23  Achilleas Mantzios <achill@matrix.gatewaynet.com>
  parent: Thomas Poty <thomas.poty@gmail.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Achilleas Mantzios @ 2018-03-23 09:23 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* RE: primary key and unique index
@ 2018-03-24 08:53  HORDER Phil <Phil.Horder@uk.thalesgroup.com>
  parent: Thomas Poty <thomas.poty@gmail.com>
  2 siblings, 1 reply; 5+ messages in thread

From: HORDER Phil @ 2018-03-24 08:53 UTC (permalink / raw)
  To: Thomas Poty <thomas.poty@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Some databases will create a unique index for you when you create a primary key.
Oracle will create one, but only if you haven’t already done that.

Postgres will ALWAYS create a unique index based on the primary key – so you should never do that as well, or the db will have to maintain two identical indexes.

(When we migrated our db design from Oracle we ended up with lots of duplicate indexes).

SQL> Select * from pg_indexes order by schemaname, tablename;

Phil Horder
Database Mechanic

Thales
Land and Air Systems
Horizon House, Throop Road, Templecombe, Somerset, BA8 0DH, UK
www.thalesgroup.com/uk

Tel: +44 (0) 1963 37 2041
Email: phil.horder@uk.thalesgroup.com<mailto:phil.horder@uk.thalesgroup.com>

Please consider the environment before printing a hard copy of this email.

The information contained in this e-mail is confidential. It is intended only for the stated addressee(s) and access to it by any other person is unauthorised. If you are not an addressee, you must not disclose, copy, circulate or in any other way use or rely on the information contained in this e-mail. Such unauthorised use may be unlawful. If you have received this e-mail in error, please inform us immediately on +44 (0)1963 370511 and delete it and all copies from your system.

Thales UK Limited. A company registered in England and Wales. Registered Office: 350 Longwater Avenue, Green Park, Reading, RG2 6GF. Registered Number: 868273

From: Thomas Poty [mailto:thomas.poty@gmail.com]
Sent: 23 March 2018 07:56
To: pgsql-general@lists.postgresql.org
Subject: primary key and unique index

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.
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


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: primary key and unique index
@ 2018-03-24 10:22  Thomas Poty <thomas.poty@gmail.com>
  parent: HORDER Phil <Phil.Horder@uk.thalesgroup.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Thomas Poty @ 2018-03-24 10:22 UTC (permalink / raw)
  To: HORDER Phil <Phil.Horder@uk.thalesgroup.com>; +Cc: pgsql-general@lists.postgresql.org

Many thanks Phil for complementary information .

Le sam. 24 mars 2018 à 09:53, HORDER Phil <Phil.Horder@uk.thalesgroup.com>
a écrit :

> Some databases will create a unique index for you when you create a
> primary key.
>
> Oracle will create one, but only if you haven’t already done that.
>
>
>
> Postgres will ALWAYS create a unique index based on the primary key – so
> you should never do that as well, or the db will have to maintain two
> identical indexes.
>
>
>
> (When we migrated our db design from Oracle we ended up with lots of
> duplicate indexes).
>
>
>
> SQL> Select * from pg_indexes order by schemaname, tablename;
>
>
>
> Phil Horder
>
> Database Mechanic
>
>
>
> Thales
>
> Land and Air Systems
>
> Horizon House, Throop Road, Templecombe, Somerset, BA8 0DH, UK
>
> www.thalesgroup.com/uk
>
>
>
> Tel: +44 (0) 1963 37 2041
>
> Email: phil.horder@uk.thalesgroup.com
>
>
>
> *Please consider the environment before printing a hard copy of this
> email.*
>
>
>
> The information contained in this e-mail is confidential. It is intended
> only for the stated addressee(s) and access to it by any other person is
> unauthorised. If you are not an addressee, you must not disclose, copy,
> circulate or in any other way use or rely on the information contained in
> this e-mail. Such unauthorised use may be unlawful. If you have received
> this e-mail in error, please inform us immediately on +44 (0)1963 370511
> and delete it and all copies from your system.
>
>
>
> *Thales UK Limited. A company registered in England and Wales. Registered
> Office: 350 Longwater Avenue, Green Park, Reading, RG2 6GF. Registered
> Number: 868273*
>
>
>
> *From:* Thomas Poty [mailto:thomas.poty@gmail.com]
> *Sent:* 23 March 2018 07:56
> *To:* pgsql-general@lists.postgresql.org
> *Subject:* primary key and unique index
>
>
>
> 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.
>
> 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
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-03-24 10:22 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-03-23 07:55 primary key and unique index Thomas Poty <thomas.poty@gmail.com>
2018-03-23 08:10 ` David Rowley <david.rowley@2ndquadrant.com>
2018-03-23 09:23 ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
2018-03-24 08:53 ` HORDER Phil <Phil.Horder@uk.thalesgroup.com>
2018-03-24 10:22   ` Thomas Poty <thomas.poty@gmail.com>

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