1

I need to create unique index for table with two fields (email, alternative_email). It means one email address can be mentioned only one time in two columns. Also alternative email can be empty.

CREATE TABLE customers (
   id serial PRIMARY KEY,
   email VARCHAR (255) NOT NULL,
   alternative_email VARCHAR (255) NOT NULL default ''
);

Data:

insert into customers (email, alternative_email) 
values ('[email protected]', ''); - ok

insert into customers (email, alternative_email) 
values ('[email protected]', '[email protected]');

Second row should not be inserted because alternative_email = '[email protected]' already mentioned as email in first row.

How to create index to do this ?

5
  • What about the alternative_email? Should that also be unique across all rows? Commented Oct 28, 2019 at 11:00
  • Unrelated, but: NOT NULL default '' seems like an odd choice if you don't require an alternative email Commented Oct 28, 2019 at 11:06
  • Something like this maybe? create unique index on customers ( (coalesce(nullif(alternative_email, ''), email))); Commented Oct 28, 2019 at 11:28
  • seems it works. Thanks Commented Oct 28, 2019 at 11:50
  • No, it doesn't. I managed insert row. id | email | alternative_email ----+-------------------+------------------- 1 | [email protected] | [email protected] 2 | [email protected] | [email protected] Commented Oct 28, 2019 at 11:57

1 Answer 1

2

You cannot create an index across two columns like that. What you need to do is change your design such that emails are all in one column in a different table.

So you could have a table called emails like:

CREATE TABLE emails (
   id serial PRIMARY KEY,
   email VARCHAR (255) NOT NULL UNIQUE,
);

Then have a many-to-many table that maps the customers to emails:

CREATE TABLE customer_emails (
   customer_id INTEGER NOT NULL REFERENCES customers(id),
   email_id INTEGER NOT NULL UNIQUE REFERENCES emails(id),
   alternative boolean NOT NULL DEFAULT FALSE,
   PRIMARY KEY (customer_id, email_id)
);

CREATE UNIQUE INDEX customer_email_idx ON customer_emails(customer_id, alternative);

The unique on email_id enforces that no 2 different customers can use the same email address, and the primary key further enforces that the same email address cannot be used for both primary and alternative email addresses of the same customer (a bit redundant given the first constraint, but just in case you want to drop the first one).

The composite unique index ensures that a customer can have at most one primary and one alternative email address.

Sign up to request clarification or add additional context in comments.

3 Comments

If those were integers you can actually create such a constraint: stackoverflow.com/a/40935314 but unfortunately that doesn't work with varchar arrays
Yes, on integer arrays to be precise.
Good to know about it. So if the OP decided to put email addresses in a separate table, the foreign keys would then be integer IDs which then could be put in an integer array with your technique too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.