Re: Create unique index or constraint on part of a column

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Create unique index or constraint on part of a column
Date: 2011-03-07 23:35:53
Message-ID: il3q4n$n85$1@dough.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ruben Blanco wrote on 08.03.2011 00:30:
> Hi:
>
> Is there anyway to create a unique index or constraint on part of a column?
>
> Something like this, but something that works ;-)
>
> ALTER TABLE invoices
> ADD CONSTRAINT cons UNIQUE (EXTRACT(YEAR FROM invoice_date), innvoice_number);
>
> Thanks for any help.
> Ruben,

CREATE UNIQUE INDEX idx_cons ON invoices (EXTRACT(YEAR FROM invoice_date), innvoice_number);

The only difference to a unique constraint is, that it cannot be used as the target of a foreign key constraint.

Regards
Thomas

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Davis 2011-03-07 23:38:03 Re: Create unique index or constraint on part of a column
Previous Message Ruben Blanco 2011-03-07 23:30:14 Create unique index or constraint on part of a column