列出MySQL中分类法中每个元素的所有属性

列出MySQL中分类法中每个元素的所有属性,mysql,sql,hierarchical-data,Mysql,Sql,Hierarchical Data,我有一个MySQL数据库,它需要提供与分类相关的属性。我希望有一个所有属性的列表,包括分类法中给定元素的继承属性。我不是一个SQL专家&我从这个网站学到了很多东西,也剽窃了很多东西。我在这里学到的一件事是使用闭包表来表示我的层次结构。我需要为数据库做的是将大量属性与层次结构中的元素相关联。但是,我似乎不知道如何获取与给定节点及其所有父节点关联的所有属性。为了回答这个问题,我创建了下面的示例数据库。请随意评论关于这个模式的任何内容,假数据只是噪音 我的示例MySQL数据库结构如下所示: -

我有一个MySQL数据库,它需要提供与分类相关的属性。我希望有一个所有属性的列表,包括分类法中给定元素的继承属性。我不是一个SQL专家&我从这个网站学到了很多东西,也剽窃了很多东西。我在这里学到的一件事是使用闭包表来表示我的层次结构。我需要为数据库做的是将大量属性与层次结构中的元素相关联。但是,我似乎不知道如何获取与给定节点及其所有父节点关联的所有属性。为了回答这个问题,我创建了下面的示例数据库。请随意评论关于这个模式的任何内容,假数据只是噪音

我的示例MySQL数据库结构如下所示:

    -- Simple Sample
    SET FOREIGN_KEY_CHECKS=0;
    DROP TRIGGER IF EXISTS inheritance_tree_insert;
    DROP TRIGGER IF EXISTS inheritance_tree_update;
    DROP TABLE IF EXISTS inheritance_paths;
    DROP TABLE IF EXISTS inheritance_tree;
    DROP TABLE IF EXISTS attributes;
    SET FOREIGN_KEY_CHECKS=1;


    CREATE TABLE `inheritance_tree` (
        `it_id`       INT NOT NULL,           -- PK
        `parent`      INT,                    -- Parent id & FK
        `child_order` INT,                    -- Oder of siblings 
        `name`        VARCHAR(500) NOT NULL,  -- Name for the entry
        PRIMARY KEY (`it_id`),
        FOREIGN KEY (`parent`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE,
        INDEX(`name`)
    ) ENGINE = INNODB;

    -- Trigger to update the paths table for new entries
    DELIMITER //

    CREATE TRIGGER `inheritance_tree_insert` AFTER INSERT ON `inheritance_tree` FOR EACH ROW 
    BEGIN
        INSERT INTO `inheritance_paths` (`ancestor`, `descendant`, `len`)
            SELECT `ancestor`, NEW.`it_id`, len + 1 FROM `inheritance_paths`
                WHERE `descendant` = NEW.`parent`
                UNION ALL SELECT NEW.`it_id`, NEW.`it_id`, 0;
    END; //
    DELIMITER ;


    DELIMITER //
    CREATE TRIGGER `inheritance_tree_update` BEFORE UPDATE ON `inheritance_tree` FOR EACH ROW 
    BEGIN
        -- From http://www.mysqlperformanceblog.com/2011/02/14/moving-subtrees-in-closure-table/
        IF OLD.`parent` != NEW.`parent` THEN
            -- Remove the node from its current parent
            DELETE a FROM `inheritance_paths` AS a
            JOIN `inheritance_paths` AS d ON a.`descendant` = d.`descendant`
            LEFT JOIN `inheritance_paths` AS x
            ON x.`ancestor` = d.`ancestor` AND x.`descendant` = a.`ancestor`
            WHERE d.`ancestor` = OLD.`it_id` AND x.`ancestor` IS NULL;

            -- Add the node to its new parent
            INSERT `inheritance_paths` (`ancestor`, `descendant`, `len`)
            SELECT supertree.`ancestor`, subtree.`descendant`, supertree.`len`+subtree.`len`+1
            FROM `inheritance_paths` AS supertree JOIN `inheritance_paths` AS subtree
            WHERE subtree.`ancestor` = OLD.`it_id`
            AND supertree.`descendant` = NEW.`parent`;
        END IF;
    END; //
    DELIMITER ;


    CREATE TABLE `inheritance_paths` (
        `ancestor`      INT NOT NULL,
        `descendant`    INT NOT NULL,
        `len`           INT NOT NULL,
        PRIMARY KEY (`ancestor`, `descendant`),
        FOREIGN KEY (`ancestor`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE,
        FOREIGN KEY (`descendant`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE
    ) ENGINE = INNODB;

    INSERT INTO `inheritance_tree` VALUES(1, NULL, NULL, 'Animal');
    INSERT INTO `inheritance_tree` VALUES(2, 1, 1, 'Mammal');
    INSERT INTO `inheritance_tree` VALUES(3, 1, 2, 'Bird');
    INSERT INTO `inheritance_tree` VALUES(4, 1, 3, 'Reptile');
    INSERT INTO `inheritance_tree` VALUES(5, 2, 2, 'Feline');
    INSERT INTO `inheritance_tree` VALUES(6, 2, 1, 'Bovine');
    INSERT INTO `inheritance_tree` VALUES(7, 1, 3, 'Fish');
    INSERT INTO `inheritance_tree` VALUES(8, 4, 1, 'Snake');
    INSERT INTO `inheritance_tree` VALUES(9, 4, 2, 'Lizard');
    INSERT INTO `inheritance_tree` VALUES(10, NULL, NULL, 'Machine');
    INSERT INTO `inheritance_tree` VALUES(11, 10, 1, 'Automobile');
    INSERT INTO `inheritance_tree` VALUES(12, 10, 2, 'OfficeMachine');
    INSERT INTO `inheritance_tree` VALUES(13, 10, 3, 'Robot');

    DELIMITER ;

    CREATE TABLE `attributes` (
      `a_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'the unique identifier',
      `attribute_name` varchar(255) DEFAULT NULL,
      `attribute_type` int(11) NOT NULL,
      PRIMARY KEY (`a_id`),
      KEY `fk_attributes_attribute_type1_idx` (`attribute_type`),
      CONSTRAINT `fk_attributes_attribute_type1_idx` FOREIGN KEY (`attribute_type`) REFERENCES `inheritance_tree` (`it_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
    ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;

    INSERT INTO `attributes` VALUES(1, 'IsAlive', 1);                   -- Animal
    INSERT INTO `attributes` VALUES(2, 'HasMaximumLifeSpan', 1);        -- Animal
    INSERT INTO `attributes` VALUES(3, 'IsNotAlive', 10);               -- Machine
    INSERT INTO `attributes` VALUES(4, 'HasIndeterminantLifeSpan', 10); -- Machine
    INSERT INTO `attributes` VALUES(5, 'BreathesAir', 2);               -- Mammal
    INSERT INTO `attributes` VALUES(6, 'CanFly', 3);                    -- Bird
    INSERT INTO `attributes` VALUES(7, 'HasFeathers', 3);               -- Bird
    INSERT INTO `attributes` VALUES(8, 'ColdBlooded', 4);               -- Reptile
    INSERT INTO `attributes` VALUES(9, 'HasFourLegs', 5);               -- Feline
    INSERT INTO `attributes` VALUES(10, 'HasFourLegs', 6);              -- Bovine
    INSERT INTO `attributes` VALUES(11, 'ColdBlooded', 7);              -- Fish
    INSERT INTO `attributes` VALUES(12, 'HasNoLegs', 8);                -- Snake
    INSERT INTO `attributes` VALUES(13, 'HasFourLegs', 9);              -- Lizard
    INSERT INTO `attributes` VALUES(14, 'ConsumesGasoline', 11);        -- Automobile
    INSERT INTO `attributes` VALUES(15, 'ConsumesElectricity', 12);     -- OfficeMachine
    INSERT INTO `attributes` VALUES(16, 'ConsumesElectricity', 13);     -- Robot
    INSERT INTO `attributes` VALUES(17, 'WarmBlooded', 2);              -- Mammal
    INSERT INTO `attributes` VALUES(18, 'WarmBlooded', 3);              -- Bird
    INSERT INTO `attributes` VALUES(19, 'BreathesWater', 7);            -- Fish
    INSERT INTO `attributes` VALUES(20, 'HasScales', 8);                -- Snake
    INSERT INTO `attributes` VALUES(21, 'HasSkin', 9);                  -- Lizard
假设:

1.分类法中的所有实体都是唯一命名的。这意味着在任何分类树中都有一个且只有一个子类型称为“Fish”

2.属性不是唯一的,可能重复

目标:

给定输入类型“Lizard”,我想要一个返回以下属性记录列表的查询:

1,我活着,1

2、寿命最长,1

5,呼吸空气,2

8,冷血,4

13岁,有四条腿,9岁

21岁,哈斯金,9岁

SELECT a.*
FROM   inheritance_tree  t
  JOIN inheritance_paths p ON p.descendant     = t.it_id
  JOIN attributes        a ON a.attribute_type = p.ancestor
WHERE  t.name = 'Lizard'
在电视上看


请注意,您的示例输出包括一个\u id=5呼吸空气,这是它的一个属性\u id=2哺乳动物,它不在蜥蜴的祖先中。

您如何将一个\u id与一个属性相关联以表示继承树?我可以想象,您将需要某种关联,以便可以级联属性。也许这正是你想要解决的问题。如果是这样,则将属性表用作查找表,并从中绑定一个Inheritance树记录,这意味着您将添加一个额外的列或拥有一个Inheritance树属性表来进行关联。有意义吗?属性表字段“attribute\u type”指向层次继承树表中的单个节点。下面是一个仅检索没有继承属性的蜥蜴的属性的示例:SELECT*FROM attributes WHERE attribute\u type in SELECT it\u id FROM heritation\u tree WHERE name Lizard这将导致2返回的记录:13,HasFourLegs,9 21,HasSkin,9此处似乎存在语法错误:UNION ALL SELECT NEW.it_id,NEW.it_id,0;以上内容将毫无错误地加载到my DB中。但我承认我不太明白触发器是怎么工作的。。。但他们似乎为我的DB工作。有关更多详细信息,请参见Shey,您是如何处理DDL的?我试着运行脚本,但一直失败…@RobertoNavarro:sqlfiddle无法识别作为客户端指令的分隔符命令;您需要使用输入框下方的设置更改语句分隔符,然后在整个过程中使用该分隔符。太棒了!与我编写的复杂SQL相比,它是如此简单。我需要进一步研究JOIN语句,以便了解这里发生了什么。对不起,我呼吸错了空气。。。我用手做的有点太快了。谢谢