M
M
Meridian3122014-02-10 16:21:00
MySQL
Meridian312, 2014-02-10 16:21:00

How can SQL tables be organized as a containing container?

Need to represent an organizational hierarchy in the database, i.e.
Group 0 { Group 1 { Group 1.1 ... Group 1.N }, Group 2 { Group 2.1 ... Group 2.N } ... Group N }

CREATE TABLE "main"."groups" (
"id" INTEGER,
"name"  TEXT NOT NULL,
PRIMARY KEY ("id" ASC)
);

CREATE TABLE "main"."groups_in_groups" (
"id"  INTEGER NOT NULL,
"group_one_id"  INTEGER NOT NULL,
"group_two_id"  INTEGER NOT NULL,
PRIMARY KEY ("id"),
CONSTRAINT "fkey0" FOREIGN KEY ("group_one_id") REFERENCES "groups" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "fkey1" FOREIGN KEY ("group_two_id") REFERENCES "groups" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

Can this be considered a suitable solution?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2014-02-10
@melkij

I know 4 popular structures, each has its own troubles:
habrahabr.ru/post/46659
habrahabr.ru/post/193166

G
gleb_kudr, 2014-02-15
@gleb_kudr

I made a base table of type name, id, parent_id + added denormalization there in the form of tree calculation results (for quick selections). There were also path fields (of type 1.2.3), as well as nested_left + nested_right + level (nesting level).
In fact, path remained unused (although it is convenient for interface things). In general, I liked this approach, you just need to strictly follow the consistency.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question