Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd go with firstly setting up the myGroup and person tables.</p> <p>Secondly, I'd set up a myGroupGroup table with columns myGroupId, parentMyGroupId. This will allow you to relate group rows to child group rows i.e. "this group has these groups within it". If a group has no rows in this table then it has no child groups within it.</p> <p>Thirdly, I'd set up a personGroup table with columns personId, myGroupId. This will allow you to relate person rows to a given group. If a group has no rows in this table then it has no persons within it.</p> <pre><code>CREATE TABLE person( id INT UNSIGNED PRIMARY KEY, name VARCHAR(100) ) ENGINE=innodb; CREATE TABLE myGroup( id INT UNSIGNED PRIMARY KEY, groupName VARCHAR(100) ) ENGINE=innodb; -- Holds groups within groups CREATE TABLE myGroupGroup( id INT UNSIGNED PRIMARY KEY, myGroupId INT UNSIGNED, parentMyGroupId INT UNSIGNED DEFAULT NULL, CONSTRAINT `fk_myGroupGroup_group1` FOREIGN KEY (`parentMyGroupId`) REFERENCES `myGroup` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_myGroupGroup_group2` FOREIGN KEY (`myGroupId`) REFERENCES `myGroup` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=innodb; -- Holds persons within a group CREATE TABLE personGroup( id INT, personId int UNSIGNED NOT NULL, myGroupId int UNSIGNED NOT NULL, CONSTRAINT `fk_personGroup_group1` FOREIGN KEY (`myGroupId`) REFERENCES `myGroup` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_personGroup_person1` FOREIGN KEY (`personId`) REFERENCES `person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=innodb; </code></pre> <p>I've tweaked your SQL a bit:</p> <p>1) Replaced <code>TYPE</code> with <code>ENGINE</code></p> <p>2) Replaced table name <code>group</code> with <code>myGroup</code> (<code>GROUP</code> is a <a href="http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html" rel="nofollow">reserved word</a>)</p> <p>Good luck!</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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