mysql - ON DELETE CASCADE in this case? -
i need advice on whether on delete cascade
suitable use in following table
create table category ( id int not null auto_increment primary key, name varchar(255) not null, maincategory int default null, foreign key(maincategory) references category(id) on delete cascade on update cascade ) engine = innodb default charset = utf8;
in case, on delete cascade delete sub categories if main category gets deleted.is recommended approach ?
you trying create tabular representation of tree structure. using adjacency model, storing id of parent in maincategory
field (usually called parent_id).
yes, approach totally valid. using on delete cascade
securing delete anomalies.
i recommend read nested sets - efficient technique representing trees in sql:
http://www.evanpetersen.com/item/nested-sets.html
how represent data tree in sql?
http://en.wikipedia.org/wiki/nested_set_model
and book: joe celko "trees , hierarchies in sql smarties"
Comments
Post a Comment