X
X
xolnimda2014-10-04 01:43:59
nested set
xolnimda, 2014-10-04 01:43:59

Storing trees in Mysql what are the ways?

I need to store a tree in the database, and it is necessary that the sequence can be changed.
those. sorting was possible.
Something like this

Математика
 - Теория вероятностей
 - Логика
Юриспруденция
- Административное право
- Трудовое право
Язык
- Русский
- Английский
- Испанский

for example, you can swap Mathematics, Law, Language
Well, you could also change the internal structure, for example, swap the order of Russian, English, Spanish.
There may be more attachments.
What do you advise?
Maybe there is some ready-made class in php for all occasions for working with trees.
Speed ​​is very important.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Andrey Ezhgurov, 2014-10-04
@xolnimda

There are 4 main ways to store trees in a relational database:
Adjacency List - described by @Satanpit .
Matherialized Path - the full path to the node is stored as a string like 0035.0993.7324.1256 (i.e. the top-level node contains the string 0035, the second-level node contains the string 0035.0993, etc.)
Nested Set - 2 numbers are stored for each node: the left and right indices. Moreover, the left index of the node is less than the left index of any descendant of this node, and the right index is correspondingly greater.
Closure Table - store data separately, separate hierarchy.
There are also combinations of these methods.
For all these methods in RuNet (including Habré) there is a large amount of information.

A
Alexander Khirenko, 2014-10-04
@Satanpit

The database structure is something like this:
To select in PHP, use recursion.
Examples in the network shaft

P
Philipp, 2014-10-04
@zoonman

Joe Celko has an excellent book . In general, he is the author of many very good books on SQL.

P
Pavel Gogolinsky, 2014-10-04
@gogolinsky

Read towards the Nested Set. Here is a small article on Habré
habrahabr.ru/post/153861
I studied this www.getinfo.ru/article610.html
You can look for ready-made extensions if you write in a framework. Here is a nice one for Yii
https://github.com/yiiext/nested-set-behavior

L
Lemfidavr, 2014-10-04
@Lemfidavr

Many-to-many relationship.
habrahabr.ru/post/193380

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question