Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6089323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:05:12+00:00 2026-05-23T12:05:12+00:00

I have a ‘users’ table with two columns, ’email’ and ‘new_email’. I need: A

  • 0

I have a ‘users’ table with two columns, ’email’ and ‘new_email’. I need:

  • A case-insensitive uniqueness constraint covering both columns – i.e., if “Bob@Example.com” appears in one row’s ’email’ column, then inserting “bob@example.com” into another row’s (or even the same row’s) ‘new_email’ column should fail.

  • Fast case-insensitive searching for a given email address in either the ’email’ or ‘new_email’ fields – i.e. find the row where the new_email OR email is “Bob@example.com”, case-insensitive.

I know that I could do this more easily by creating a related ’emails’ table, but I’m expecting to be looking up users in this table (by primary key) from several applications, and I’d like to avoid duplicating the join logic in various places to also retrieve their emails. So I think some kind of expression index would be best, if that’s possible.

If this isn’t possible, I suppose my next best option would be to create a view that the other applications could use to easily fetch a user’s emails along with their other information, but I’m not sure how to do that either.

I’m using Postgres 8.4. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:05:13+00:00Added an answer on May 23, 2026 at 12:05 pm

    I think you’ll have to use a trigger to enforce your cross-column uniqueness constraint. If you add unique indexes on each column and then a trigger something like this (untested off the top of my head code):

    CREATE FUNCTION no_dups_allowed() RETURNS trigger AS $$
    DECLARE
        r ROW;
    BEGIN
        SELECT 1 INTO r
        FROM users
        WHERE LOWER(email)     = LOWER(NEW.email_new)
           OR LOWER(email_new) = LOWER(NEW.email);
        IF FOUND THEN
            -- Found a duplicate so it is time for a hissy fit!
            RAISE 'Duplicate email address found' USING ERRCODE = 'unique_violation';
        END;
        RETURN NEW;
    END;
    $$ LANGUAGE plpgsql;
    

    You’d want something like that as a BEFORE INSERT and BEFORE UPDATE trigger. That trigger would take care of catching cross-column duplicates and the unique indexes would take care of in-column duplicates.

    Some useful references:

    • FOUND
    • RAISE
    • Triggers
    • Trigger Procedures

    You’ll want the individual indexes for your queries anyway and using the uniqueness half of the indexes simplifies your trigger by leaving it to only deal with the cross-column part; if you try to do it all in the trigger, then you’ll have to watch out for updating a row without really changing the email or email_new columns.

    For the querying half, you could create a view that used a UNION to combine the two columns. You could also create a function to merge the user’s email addresses into one list. Hard to say which would be best without know more details of these other queries but I suspect that fixing all the other queries to know about email and email_new would be the best approach; you’ll have to update all the other queries to use the view or function anyway so why build a view or function at all?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have a table that dynamically generates text boxes in run time. I want to
Have a build process that can't be edited and need to pack another war
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
Have a n-tire web application and search often times out after 30 secs. How
Have you managed to get Aptana Studio debugging to work? I tried following this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.