I have the following table:
Create Table if not exists Categories(
category_id int (10) primary key NOT NULL AUTO_INCREMENT,
category_name varchar (20) NOT NULL,
parent_category_id int (10) NOT NULL,
FOREIGN KEY (parent_category_id) REFERENCES Categories(category_id) ON DELETE CASCADE)
The table is holding every category I have on my site - and each category have a parent category (for example 'computer' is the parent category of 'programming') I have a few top categories which don't have any parent category => parent_category_id =0
My question is how to insert the data for the top categories. when i'm trying to do:
INSERT INTO `databaseproject`.`categories` (`category_id` ,`category_name` ,`parent_category_id`)
VALUES (NULL , 'computers', '0')
I'm getting the error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`databaseproject`.`categories`, CONSTRAINT `categories_ibfk_1`
FOREIGN KEY (`parent_category_id`) REFERENCES `categories` (`category_id`) ON DELETE CASCADE)
what can I do to insert those categories?