组或元素的嵌套

发布于 2024-12-08 21:37:39 字数 667 浏览 0 评论 0原文

我对 Mysql 相当陌生,但我有无法解决的问题。我将给你一个例子来证明它。请注意,我知道(对于当前的示例)还有其他更简单、更有效的方法来解决它......但只是将其作为所需过程的示例。

  • 首先是数据:数据是一个人的名字。
CREATE TABLE person(
          id INT,
          name VARCHAR(100)
        ) TYPE=innodb;
  • 第二:组创建...所以这相当简单...并且可以使用带有人员外键的表“group”轻松完成。这些组可以是任意的,包含任意数量的人,重复...或不...(这很简单!!)
  • 第三:我真正的问题---我也想要有组以其他组作为元素(而不是 persons)。这就是真正陷入困境的地方,因为我知道如何创建一组,一组(具有自引用外键)......但是我不知道如何创建一个可能有人员的组。

我很感激任何解决这个问题的建议。

非常感谢您的评论。 问候

ACombo

I'm fairly new in Mysql, but I have problem that I cannot solve. I will give you an example to demonstrate it. Please note that I know that (for current example) there are other simpler and more efficient ways to solve it... but just take it as an example of the required procedure.

  • First the data: The data would be the name of a Person.
CREATE TABLE person(
          id INT,
          name VARCHAR(100)
        ) TYPE=innodb;
  • Second: Group Creation... So this is fairly simple... and could easily done using a table 'group' with a foreignkey to person. These groups could be arbitrary, containing any number of persons, duplicated... or not... (that is simple!!)
  • Third: MY REAL PROBLEM--- I also would like to have Groups that have other Groups as elements (instead of persons). This is where a really get stuck, because I know how to create a groups of persons, a group of groups (having a self-referencing foreign key)... but I don't know how to create a group that MAY HAVE persons AND Groups.

I appreciate any suggestion to solve this issue.

Thank you very much for your comments.
Regards

ACombo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2024-12-15 21:37:39

我会首先设置 myGroup 和 person 表。

其次,我设置了一个 myGroupGroup 表,其中包含 myGroupId、parentMyGroupId 列。这将允许您将组行与子组行相关联,即“该组中包含这些组”。如果某个组在此表中没有行,则它内没有子组。

第三,我设置了一个 personGroup 表,其中包含 personId、myGroupId 列。这将允许您将人员行与给定组相关联。如果某个组在此表中没有行,则该组中没有人员。

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;

我对你的 SQL 做了一些调整:

1) 将 TYPE 替换为 ENGINE

2) 将表名 group 替换为 myGroup > (GROUP保留字< /a>)

祝你好运!

I'd go with firstly setting up the myGroup and person tables.

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.

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.

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;

I've tweaked your SQL a bit:

1) Replaced TYPE with ENGINE

2) Replaced table name group with myGroup (GROUP is a reserved word)

Good luck!

尹雨沫 2024-12-15 21:37:39

选择:

CREATE TABLE Entity
        ( EntityId INT                   --- this id could be AUTO_INCREMENT
        , PRIMARY KEY (EntityId)
        ) ENGINE = InnoDB ;

CREATE TABLE Person
        ( PersonId INT                   --- but not this id
        , PersonName VARCHAR(100)
        , PRIMARY KEY (PersonId)
        , FOREIGN KEY (PersonId) 
            REFERENCES Entity(EntityId)        
        ) ENGINE = InnoDB ;

CREATE TABLE Grouping
        ( GroupingId INT                 --- and neither this id
        , GroupingName VARCHAR(100)
        , PRIMARY KEY (GroupingId)
        , FOREIGN KEY (GroupingId) 
            REFERENCES Entity(EntityId)            
        ) ENGINE = InnoDB ;

CREATE TABLE Belongs
        ( EntityId INT
        , GroupingID INT
        , PRIMARY KEY (EntityId, GroupingId)
        , FOREIGN KEY (EntityId) 
            REFERENCES Entity(EntityId) 
        , FOREIGN KEY (GroupingID) 
            REFERENCES Grouping(GroupingId)            
        ) ENGINE = InnoDB ;

Alternative:

CREATE TABLE Entity
        ( EntityId INT                   --- this id could be AUTO_INCREMENT
        , PRIMARY KEY (EntityId)
        ) ENGINE = InnoDB ;

CREATE TABLE Person
        ( PersonId INT                   --- but not this id
        , PersonName VARCHAR(100)
        , PRIMARY KEY (PersonId)
        , FOREIGN KEY (PersonId) 
            REFERENCES Entity(EntityId)        
        ) ENGINE = InnoDB ;

CREATE TABLE Grouping
        ( GroupingId INT                 --- and neither this id
        , GroupingName VARCHAR(100)
        , PRIMARY KEY (GroupingId)
        , FOREIGN KEY (GroupingId) 
            REFERENCES Entity(EntityId)            
        ) ENGINE = InnoDB ;

CREATE TABLE Belongs
        ( EntityId INT
        , GroupingID INT
        , PRIMARY KEY (EntityId, GroupingId)
        , FOREIGN KEY (EntityId) 
            REFERENCES Entity(EntityId) 
        , FOREIGN KEY (GroupingID) 
            REFERENCES Grouping(GroupingId)            
        ) ENGINE = InnoDB ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文