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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:01:41+00:00 2026-06-05T15:01:41+00:00

Is it possible to have a many to many relationship between two tables, and

  • 0

Is it possible to have a many to many relationship between two tables, and enforce that all the members of a group are to have a particular attribute in common?

For example, a worker can be in several groups, and a group can have several workers, but all the workers in a group must be on the same site. There are enough workers and sites that I can’t make a new table for each site.

–EDIT–

This is the simplified schema. I’m using mySQL workbench, but I think this is right:

-- -----------------------------------------------------
-- Table `DB`.`Worker`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `DB`.`Worker` (
  `workerID` INT NOT NULL ,
  `site` VARCHAR(45) ,
  PRIMARY KEY (`workerID`) ;

-- -----------------------------------------------------
-- Table `DB`.`Group`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `DB`.`Group` (
  `groupID` INT NOT NULL ,
  PRIMARY KEY (`groupID`) ;

-- -----------------------------------------------------
-- Table `DB`.`Worker_Group`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `DB`.`workerGroup` (
  `workerID` INT NOT NULL ,
  `groupID` INT NOT NULL ,
  PRIMARY KEY (`workerID`, `groupID`) ,
  INDEX `fk_Group` (`groupID` ASC) ,
  CONSTRAINT `fk_Worker`
    FOREIGN KEY (`workerID` )
    REFERENCES `DB`.`Worker` (`workerID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Group`
    FOREIGN KEY (`groupID`)
    REFERENCES `DB`.`Group` (`groupID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION) ;
  • 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-06-05T15:01:45+00:00Added an answer on June 5, 2026 at 3:01 pm

    You do that by using composite foreign keys.

    Instead of foreign keying on just one field, you use a foreign key made out of several (two in this case) fields.

    CREATE TABLE site (
      id         INT AUTO_INCREMENT,
      name       VARCHAR(256),
      PRIMARY KEY (id)
    )
    
    CREATE TABLE worker (
      id         INT AUTO_INCREMENT,
      name       VARCHAR(256),
      site_id    INT,
      PRIMARY KEY (id, site_id),
      FOREIGN KEY (site_id) REFERENCES site (id)
    )
    
    CREATE TABLE group (
      id         INT AUTO_INCREMENT,
      name       VARCHAR(256),
      site_id    INT,
      PRIMARY KEY (id, site_id),
      FOREIGN KEY (site_id) REFERENCES site (id)
    )
    
    CREATE TABLE map_worker_group (
      worker_id  INT,
      site_id    INT,
      group_id   INT,
      PRIMARY KEY (worker_id, group_id, site_id),
      FOREIGN KEY (group_id,  site_id) REFERENCES group  (id, site_id),
      FOREIGN KEY (worker_id, site_id) REFERENCES worker (id, site_id)
    )
    

    Now a group is specific to a site, as is a worker. And when populating the worker:group mapping table you reference the sites of both. This means that a worker can only be in a group with the same site_id.

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

Sidebar

Related Questions

Let's say I have two objects: Articles and Categories with a many-many relationship between
I have a many-to-many relationship between the entities Groups and Contacts. One group can
I have two tables in my rails application Categories and Projects. The relationship between
So essentially I have two tables, containing URLS and TAGS, with a has-and-belongs-to-many relationship
Let´s say I have two tables, Garden and Flowers. There is a 1:n-relationship between
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
What is the best way to create a one to many relationship between two
I have entities Post and Tag , where there is a many-to-many relationship between
I have a simple has many relationship between 2 entities in Core Data: Team

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.