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 7523899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:56:03+00:00 2026-05-30T02:56:03+00:00

Firstly, I apologise if this is a dupe – I suspect it may be

  • 0

Firstly, I apologise if this is a dupe – I suspect it may be but I can’t find it.

Say I have a table of companies:

 id | company_name
----+--------------
  1 | Someone
  2 | Someone else

…and a table of contacts:

 id | company_id | contact_name | is_primary
----+------------+--------------+------------
  1 |     1      | Tom          |      1
  2 |     2      | Dick         |      1
  3 |     1      | Harry        |      0
  4 |     1      | Bob          |      0

Is it possible to set up the contacts table in such a way that it requires that one and only one record has the is_primary flag set for each common company_id?

So if I tried to do:

UPDATE contacts
SET is_primary = 1
WHERE id = 4

…the query would fail, because Tom (id = 1) is already flagged as the primary contact for company_id = 1. Or even better, would it be possible to construct a trigger so that the query would succeed, but Tom‘s is_primary flag would be cleared by the same operation?

I am not too bothered about checking whether company_id exists in the companies table, my PHP code would already have performed this check before I got to this stage (although if there is a way to do this in the same operation it would be nice, I suppose).

When I initially thought about this I thought “that will be easy, I’ll just add a unique index across the company_id and is_primary columns” but obviously that won’t work as it would restrict me to one primary and one non-primary contact – any attempt to add a third contact would fail. But I can’t help feeling there would be a way to configure a unique index that gives me the minimum functionality I require – to reject an attempt to add a second primary contact, or reject an attempt to leave a company with no primary contact.

I am aware that I could just add a primary_contact field to the companies table with an FK to the contacts table but it feels messy. I don’t like the idea of both tables having an FK to the other – it seems to me that the one table should rely on the other, not both tables relying on each other. I guess I just think that over time there is more chance of something going wrong.

To sum up:

  • How can I restrict the contacts table so that one and only one record with a given company_id has the is_primary flag set?
  • Anyone have any thoughts on whether two tables having FKs to each other is a good/bad idea?
  • 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-30T02:56:05+00:00Added an answer on May 30, 2026 at 2:56 am

    Circular refenences between tables are indeed messy. See this (decade old) article: SQL By Design: The Circular Reference

    The cleanest way to make such a constraint is to add another table:

    Company_PrimaryContact
    ----------------------
    company_id
    contact_id
    PRIMARY KEY (company_id)
    FOREIGN KEY (company_id, contact_id)
      REFERENCES Contact (company_id, id)
    

    This will also require a UNIQUE constraint in table Contact on (company_id, id)

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

Sidebar

Related Questions

Firstly apologies if this is really simple but I have spent hours trying and
Firstly: I am totally a newbie for this kind of work. I have a
Firstly, I do not have any malicious intent out of this question. I would
Frstly my apologies if this is a duplicate question. I have tried to find
Firstly, let me say that I've been on this issue for pretty much 2
Firstly, I'm new to Android (so I apologize if this question is ignorant) but
Firstly, please forgive it is already asked one or can be find easily with
Firstly, let me set out what I'd like to do. Assume I have three
I am new to this, so I apologise if I am not completely clear...
Firstly, apologies for the vague title, but I'm not sure exactly what I'm asking

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.