0

I've heard that we should avoid using

CREATE UNIQUE INDEX indx ON tbl(clmn)

because in fact it creates a UNIQUE constraint without naming it so in a contraints table. But What is the different? If we use

ALTER TABLE tbl ADD CONSTRAINT un_cn UNIQUE(clmn)

we create a consraint, but it also creates an index on clmn we don't explicitly name.

So why is the second form preffered?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 1
    This one should give you a better explanation: http://stackoverflow.com/questions/9066972/how-does-postgresql-enforce-the-unique-constraint-what-type-of-index-does-it-u/9067108#9067108 – Erwin Brandstetter Oct 19 '15 at 07:33
  • Actually I _prefer_ `create unique index` because it is more flexible than a unique constraint –  Oct 19 '15 at 07:33
  • @a_horse_with_no_name Indeed, as it's explained in the dupe I think that it's all about personal perference. The implementation of UNIQUE may get changed in future release. – St.Antario Oct 19 '15 at 07:42

0 Answers0