Answer the question
In order to leave comments, you need to log in
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
);
Answer the question
In order to leave comments, you need to log in
I know 4 popular structures, each has its own troubles:
habrahabr.ru/post/46659
habrahabr.ru/post/193166
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 questionAsk a Question
731 491 924 answers to any question