I want to represent a recursive parent-child relationship in a MySQL database. I want to create a category
-subcategory
relationship. A category can have N subcategories and each one of those can have N subcategories and so on. I was thinking of having a single category
table with a foreign key pointing in it's self. Here is what i mean:
CREATE TABLE `category` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`parent_category` int NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`parent_category`) REFERENCES `category` (`id`)
)
parent_category
can be null if a category is a top-level category.
Is this the correct way of representing a relationship like this? Also are there other things i should take into consideration in my design (performance, queries...)?