How can I manage hierarchical data in MySQL?

How can I manage hierarchical data in MySQL?

There are many ways to manage hierarchical data in MySQL and the adjacency list model may be the simplest solution. Because of its simplicity, the adjacency list model is a very popular choice by developers and database administrators. In the adjacency list model, each node has a pointer that points to its parent. The top node has no parent.

Why do we need adjacency list in MySQL?

To resume, the adjacency list model is maybe a simple aproach, based on intern relationship between database’s table rows. It’s quite simple to use it tree constructions with programming languages. But when stored data is supposed to change frequently by making new relationships, it could be better to think about another solution.

Which is the top node in the adjacency list?

In the adjacency list model, each node has a pointer that points to its parent. The top node has no parent. See the following categories of electronics products: There are some terms that you should be familiar with before you work with the adjacency list model: Electronics is a top node or root node.

How to move subtrees in MySQL using adjacency model?

To move a subtree, just update the parent_id of the top node of the subtree. For example, to move the Cameras & photo as the children of Phone and Accessories, you use the following statement: In this tutorial, you have learned how to use the adjacency list model to manage hierarchical data in MySQL.

Which is the root node in the adjacency list?

There are some terms that you should be familiar with before you work with the adjacency list model: Electronics is a top node or root node. Laptops, Cameras & photo, Phones & Accessories nodes are the children of the Electronics node. And vice versa Electronics node is the parent of Laptops, Cameras & photo, Phones & Accessories nodes.

How to get children of root node in MySQL?

The following query gets the immediate children of the root node: The leaf nodes are the nodes that have no children. SELECT c1.id, c1.title FROM category c1 LEFT JOIN category c2 ON c2.parent_id = c1.id WHERE c2.id IS NULL ; The following recursive common table expression (CTE) retrieves the whole category tree.

How to set Parent ID in category table?

The parent_id column is the foreign key of the category table itself. It acts like a pointer that points to the id column. The root node of the tree has no parent, therefore, the parent_id is set to NULL. The other nodes must have one and only one parent.

Do you have to query hierarchical data in SQL?

(Hierarchical data in SQL is an anti-pattern, but don’t be disheartened – we all run into this one) But if I had like 10 categories under Toys, then I’d have to do this join and query 10 times.

How to create a category tree in MySQL?

SELECT c1.id, c1.title FROM category c1 LEFT JOIN category c2 ON c2.parent_id = c1.id WHERE c2.id IS NULL ; The following recursive common table expression (CTE) retrieves the whole category tree. Notice that the CTE feature has been available since MySQL 8.0