Using foreign key in the same table

user11594895

I created a table inside my database named categories:

CREATE TABLE IF NOT EXISTS `category` 
( `categoryName` varchar(128)  ,
 `subcategoryName` varchar(128) ,
 PRIMARY KEY (`categoryName`) )
 ENGINE=InnoDB DEFAULT CHARSET=latin1

A category in this table may be a subcategory of another category in this field.So,subcategory is a FK to categories.Initialiasiation:

    ALTER TABLE `category` ADD FOREIGN KEY (`subcategoryName`) 
REFERENCES `category`(`categoryName`) ON DELETE RESTRICT ON UPDATE RESTRICT;

So i tried inserting values in this table like this:

INSERT INTO `category` (`categoryName`, `subcategoryName`) VALUES
( 'Literature','Satire'),
( 'Science','Mathematics'),
( 'Science','Physics');

but i get this error:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`php_beginner_crud_level_1`.`category`, CONSTRAINT `category_ibfk_1` FOREIGN KEY (`subcategoryName`) REFERENCES `category` (`categoryName`))

I think this means that i can't isent a subcategory if the category does not already exist.So what i did is insert first single categories:

INSERT INTO `category` (`categoryName`) VALUES
( 'Literature'),
( 'Mathematics'),
( 'Programming');

Hw can i insert subcategories for these categories now? for example if i want a subcategory: Java for Programming how can i insert it into the table?

Barmar

You have the foreign key relationship backwards. Subcategories reference categories, so it should be:

ALTER TABLE `category` ADD FOREIGN KEY (`categoryName`) 
REFERENCES `category`(`subcategoryName`) ON DELETE RESTRICT ON UPDATE RESTRICT;

And the primary key should be subcategoryName, not categoryName.

You need to create subcategories with no category for the top-level categories in the hierarchy.

INSERT INTO `category` (`categoryName`, `subcategoryName`) VALUES
(NULL, 'Literature'),
(NULL, 'Science'),
( 'Literature','Satire'),
( 'Science','Mathematics'),
( 'Physics','Science');

Or rename things to make more sense.

CREATE TABLE IF NOT EXISTS `category` 
( `categoryName` varchar(128)  NOT NULL,
 `supercategoryName` varchar(128) ,
 PRIMARY KEY (`categoryName`) )
 ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `category` ADD FOREIGN KEY (`supercategoryName`) 
REFERENCES `category`(`categoryName`) ON DELETE RESTRICT ON UPDATE RESTRICT;

INSERT INTO `category` (`categoryName`, `subcategoryName`) VALUES
(NULL, 'Literature'),
(NULL, 'Science'),
('Literature', NULL),
('Science', NULL),
( 'Satire','Literature'),
( 'Mathematics','Science'),
( 'Science'',Physics');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Foreign key on same table

MYSQL foreign key to the same table?

Eloquent Foreign Key to Same Table

MySQL Using Same Foreign key for two different table columns

Could a foreign key be a candidate key in the same table?

Two columns in same table and same foreign key

CRUD operations on a table with foreign key on same table

EF Code First Foreign Key Same Table

Multiple Foreign Key from the same table

Django multiple foreign key to a same table

Django model foreign key in the same table

MySQL two foreign key from same table

How to create a foreign key on the same table with Sequelize?

foreign key constraint is incorrectly formed [same table]

Oracle SQL foreign key from the same table

Django - using multiple foreign key to the same model

Count number of rown against each id in other table contains the same id as foreign key using active records

EF multiple foreign key relationship on same primary key ApplicationUser table

MySQL - foreign key constrained by primary key in same table, error #1452

Multiple column foreign key referencing on the same key from a table

Risks with Foreign Key that references the primary key of the same table

Foreign key referring to primary key in the same table Mysql

reference a foreign key to a primary key within the same table

Foreign Key Refers the Primary Key Columns of Same Table

SQLAlchemy - Multiple Foreign key pointing to same table same attribute

Relationships with same foreign key to same table Laravel 5

Laravel Eloquent Relationship has the same foreign key on the same table

MYSQL partitioning existing table using a foreign key

Issue with using a foreign key within one table