What are the best practices for designing database schemas in PHP applications to handle hierarchical data like automotive categories?
When designing database schemas in PHP applications to handle hierarchical data like automotive categories, it is best to use a nested set model or adjacency list model. This allows for efficient querying and manipulation of hierarchical data. Additionally, using foreign key constraints and indexes can help maintain data integrity and improve performance.
CREATE TABLE categories (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES categories(id)
);
CREATE INDEX parent_id_index ON categories(parent_id);