transitive-closure-table

hierarchical data in a database: recursive query vs. closure tables vs. graph database

爱⌒轻易说出口 提交于 2019-12-04 01:50:29
I'm starting on a new project that has some hierarchical data and I'm looking at all the options for storing that in a database at the moment. I am using PostgreSQL, which does allow recursive querying. I also looked into design patterns for relational databases, such as closure tables and I had a look at graph database solutions such as neo4j. I'm finding it difficult to decide between those options. For example: given that my RDBMS allows recursive queries, would it still make sense to use closure tables and how does that compare to graph database solutions in terms of maintainability and

MySQL hierarchical data help - Closure Table Method

自古美人都是妖i 提交于 2019-12-03 14:37:24
I'm trying to implement a system in MySQL to store hierarchical data. I've decided to go with the system implemented here as described by Bill Karwin starting on slide number 40. I'm trying to setup the database so the EntryPaths table is maintained automatically. Update: I've updated the database create SQL a bit. I've got things 1/2 working for an update, I think. After running the database create SQL try the following First see how this entry looks -- Example query to return a full library entry (0x02 is the entry iD) SELECT `Library`.* FROM `Library` LEFT JOIN `EntryPaths` ON `Library`.`iD

How to traverse a hierarchical tree-structure structure backwards using recursive queries

左心房为你撑大大i 提交于 2019-12-03 01:57:49
I'm using PostgreSQL 9.1 to query hierarchical tree-structured data, consisting of edges (or elements) with connections to nodes. The data are actually for stream networks, but I've abstracted the problem to simple data types. Consider the example tree table. Each edge has length and area attributes, which are used to determine some useful metrics from the network. CREATE TEMP TABLE tree ( edge text PRIMARY KEY, from_node integer UNIQUE NOT NULL, -- can also act as PK to_node integer REFERENCES tree (from_node), mode character varying(5), -- redundant, but illustrative length numeric NOT NULL,

Depth in MYSQL and Closure Table Trees

匆匆过客 提交于 2019-12-03 00:29:15
How would I go about populating a closure table's depth/length column when inserting a new node to the tree? The values in ancestor and descendant are IDs from another table that represent pages to be arranged in a tree structure. Closure Table: ancestor descendant depth 1 1 0 1 2 1 1 3 1 1 4 1 2 2 0 3 3 0 4 4 0 This will insert the ancestor and descendants properly but I'm not sure how to populate the depth column Insert Query: INSERT INTO closure_tree_path (ancestor, descendant) SELECT ancestor, '{$node_id}' FROM closure_tree_path WHERE descendant = '{$parent_id}' UNION ALL SELECT '{$node_id

codeigniter: closure table - add descendant

99封情书 提交于 2019-12-02 10:25:52
I'm trying to add a new descendant, but having difficulties achieving it, it displays some error, Would be grateful if you could take time to review what I've done thus far. Here's Controller public function index() { $this->load->view('closure_view'); } public function add() { $add_new = array( 'ancestor' => $this->input->post('ancestor'), 'descendant' => $this->input->post('descendant'), 'lvl' => $this->input->post('lvl'), 'id' => $this->input->post('id') ); $cust_id = $this->closure->add($add_new); redirect('http://localhost/kgmerchant/index.php/welcome'); } Model public $table; public

Recursive query challenge - simple parent/child example

强颜欢笑 提交于 2019-11-30 03:54:29
Note: with help from RhodiumToad on #postgresql, I've arrived at a solution, which I posted as answer. If anyone can improve on this, please chime in! I have not been able to adapt a previous recursive query solution to the following directed acyclic graph that includes multiple "root" (ancestor-less) nodes. I'm trying to write a query whose output is what is commonly known as a closure table: a many-to-many table that stores every path from each node to each of its descendants and to itself: 1 2 11 8 4 5 7 \/ | | \ | / 3 | \ 6 \ | \ / 9 | 10 \/ / 12 13 \ / 14 CREATE TABLE node ( id SERIAL

Recursive query challenge - simple parent/child example

只愿长相守 提交于 2019-11-29 01:26:13
问题 Note: with help from RhodiumToad on #postgresql, I've arrived at a solution, which I posted as answer. If anyone can improve on this, please chime in! I have not been able to adapt a previous recursive query solution to the following directed acyclic graph that includes multiple "root" (ancestor-less) nodes. I'm trying to write a query whose output is what is commonly known as a closure table: a many-to-many table that stores every path from each node to each of its descendants and to itself:

Recursive query used for transitive closure

烂漫一生 提交于 2019-11-28 12:08:37
I've created a simple example to illustrate transitive closure using recursive queries in PostgreSQL. However, something is off with my recursive query. I'm not familiar with the syntax yet so this request may be entirely noobish of me, and for that I apologize in advance. If you run the query, you will see that node 1 repeats itself in the path results. Can someone please help me figure out how to tweak the SQL? /* 1 / \ 2 3 / \ / 4 5 6 / 7 / \ 8 9 */ create table account( acct_id INT, parent_id INT REFERENCES account(acct_id), acct_name VARCHAR(100), PRIMARY KEY(acct_id) ); insert into

PostgreSQL pass data from recursive CTE onto function

孤街浪徒 提交于 2019-11-28 00:29:22
I have the following problem: I am trying to discover all possible paths from source node ( node_s ) to target node ( node_t ). The format of the original table with graph edges is simple: | node_x | node_y | strength | , where "node_x" -> "node_y" is a direct edge with strength of the edge being "weight". The idea is if at any point during the exploration of the paths we discover that a node among its children has target node_t , we record this path and stop exploring paths from this node, otherwise continue exploration. The simple solution was to use PostgreSQL's recursive CTE which

How to get the parent given a child in SQL SERVER 2005

送分小仙女□ 提交于 2019-11-28 00:28:19
I have a table like this childid parentid ------------------------ 1 0 2 1 3 2 4 2 5 3 6 4 7 0 8 7 9 8 10 1 If I give a childid as 5, the parentid will be 1(output) If I give a childid as 9, the parentid will be 7.(output) i.e. the root parentid is 0 and the query should stop there. How to solve such a query? Please help. I think you should rename your child_id to node, your parent_id to child_of. Your column naming is a bit confusing create table stack_overflow ( node int, child_of int ); insert into stack_overflow(node, child_of) values (1,0), (2,1), (3,2), (4,2), (5,3), (6,4), (7,0), (8,7),