Should I use a foreign key to display the tree relationship in SQL

I am trying to model the relation of a tree in a table. For example, there are “categories,” and the categories themselves may be inside the parent category.

My scheme:

id int PRIMARY KEY,
parent_id int,
name

My question is: should I mark parent_id column as foreign key? Foreign ones mean "outside", not self-regulation. Is there any other key for this purpose?

My question is similar to: Limiting self-regulation in MS SQL , but I ask another question, cascading, not being a problem.

+3
source share
4 answers

. . "" , manager_id employee_id .

node , :)

+10

. .

. :

+4

, ... .. parent_id, , id

+3

Source: https://habr.com/ru/post/1711377/


All Articles