Note that there are some explanatory texts on larger screens.

plurals
  1. POmySQL many to many relation with all members of a group having a common attribute
    primarykey
    data
    text
    <p>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?</p> <p>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.</p> <p>--EDIT--</p> <p>This is the simplified schema. I'm using mySQL workbench, but I think this is right:</p> <pre><code>-- ----------------------------------------------------- -- 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) ; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload