Create unique index or constraint on part of a column

Started by Ruben Blancoabout 15 years ago3 messagesgeneral
Jump to latest
#1Ruben Blanco
rubenblan@gmail.com

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,

#2Thomas Kellerer
spam_eater@gmx.net
In reply to: Ruben Blanco (#1)
Re: Create unique index or constraint on part of a column

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

#3Jeff Davis
pgsql@j-davis.com
In reply to: Ruben Blanco (#1)
Re: Create unique index or constraint on part of a column

On Mon, 2011-03-07 at 23:30 +0000, Ruben Blanco wrote:

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);

CREATE UNIQUE INDEX invoices_constraint_idx ON invoices
(EXTRACT(YEAR FROM invoice_date), invoice_number);

Regards,
Jeff Davis