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

  • Home
  • SEARCH
  • 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 6112957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:48:14+00:00 2026-05-23T14:48:14+00:00

I have a database with four tables as follows: Addressbook ——————– id more fields

  • 0

I have a database with four tables as follows:

Addressbook
——————–
id
more fields

Contact
———————
id
addressbook id
more fields

Group
———————
id
addressbook id
more fields

Group to Contact
———————
Composite key
Group id
Contact id

My relationships are one to many for addressbook > contact, one to many for addressbook > group and many to many between contact and groups.

So in summary, I have an addressbook. Contacts and groups can be stored within it and they cannot be stored in more than one addressbook. Furthermore as many contacts that are needed can be added to as many groups as are needed.

My question now poses as follows. I wish to add the constraint that a contact can only be a member of a group if both of them have the same addressbook id.

As I am not a database person this is boggling my brain. Does this mean I have designed my table structure wrong? Or does this mean that I have to add a check somewhere before inserting into the group to contact table? This seems wrong to me because I would want it to be impossible for SQL queries to link contacts to groups if they do not have the same id.

  • 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-23T14:48:15+00:00Added an answer on May 23, 2026 at 2:48 pm

    You should be able to accomplish this by adding a addressbook_id column to your Group to Contact bridge table, then using a compound foreign key to both the Contacts and Groups tables.

    In PostgreSQL (but easily adaptable to any DB, or at least any DB that supports compound FKs):

    CREATE TABLE group_to_contact (
        contact_id          INT,
        group_id            INT,
        addressbook_id      INT,
        CONSTRAINT contact_fk FOREIGN KEY (contact_id,addressbook_id)
            REFERENCES contacts(id,addressbook_id),
        CONSTRAINT groups_fk FOREIGN KEY (group_id,addressbook_id)
            REFERENCES groups(id,addressbook_id)
    )
    

    By using the same addressbook_id column in both constraints, you are of course enforcing that they are the same in both referenced tables.

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

Sidebar

Related Questions

I have a secondhand sale car database and four tables with relationships. Same column
I have a Database named CarsType.accdb there are four fields in the data base
I have four tables in my database and i want to join that's and
I have four tables in a MySQL database and I am trying to create
I have four DB tables in an Oracle database that need to be rewritten/refreshed
I have several tables in my database that have read-only fields that get set
I have four tables: groups, users, votes, follows. The structures of these tables are
I have four Database Tables like these: Book ID_Book |ID_Company|Description BookExtension ID_BookExtension | ID_Book|
I have four search fields that are used to search a database for book
This is my database structure I have four tables. Table LocalArea and Lanungaue have

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.