mysql - groups and sub-group up to 5 level database structure -
i wondering best structure table in mysql, structure needs have:
parent (level 0) -region (inside regions are)(level 1) -countrys (inside countrys are)(level 2) -districts (level 3) so need store info in same table, clues?? if need know building app on cakephp.
thanks in advance.
i've impulsively made eer diagram situation.
please tell me if works (and if agree), i'll awaiting feedback.
set @old_unique_checks=@@unique_checks, unique_checks=0; set @old_foreign_key_checks=@@foreign_key_checks, foreign_key_checks=0; set @old_sql_mode=@@sql_mode, sql_mode='traditional,allow_invalid_dates'; drop schema if exists `hierarchy` ; create schema if not exists `hierarchy` default character set utf8 collate utf8_general_ci ; use `hierarchy` ; -- ----------------------------------------------------- -- table `hierarchy`.`level` -- ----------------------------------------------------- create table if not exists `hierarchy`.`level` ( `id` tinyint unsigned not null auto_increment , `name` varchar(20) not null , primary key (`id`) , unique index `name_unique` (`name` asc) ) engine = innodb; -- ----------------------------------------------------- -- table `hierarchy`.`location` -- ----------------------------------------------------- create table if not exists `hierarchy`.`location` ( `id` int unsigned not null auto_increment , `parent` int unsigned null , `name` varchar(50) not null , `level` tinyint unsigned null , primary key (`id`) , index `fk_location_location_idx` (`parent` asc) , index `fk_location_level1_idx` (`level` asc) , index `index_name` (`name` asc) , constraint `fk_location_location` foreign key (`parent` ) references `hierarchy`.`location` (`id` ) on delete no action on update no action, constraint `fk_location_level1` foreign key (`level` ) references `hierarchy`.`level` (`id` ) on delete no action on update no action) engine = innodb; use `hierarchy` ; -- ----------------------------------------------------- -- placeholder table view `hierarchy`.`details_location` -- ----------------------------------------------------- create table if not exists `hierarchy`.`details_location` (`location_id` int, `location_name` int, `parent_id` int, `level_id` int, `level_name` int, `children` int); -- ----------------------------------------------------- -- view `hierarchy`.`details_location` -- ----------------------------------------------------- drop table if exists `hierarchy`.`details_location`; use `hierarchy`; create or replace view `hierarchy`.`details_location` select x.`id` `location_id`, x.`name` `location_name`, ifnull(x.`parent`, 0) `parent_id`, y.`id` `level_id`, y.`name` `level_name`, (select count(*) location l l.parent = x.id) `children` location `x` inner join `level` `y` on (x.`level` = y.id); use `hierarchy`; delimiter $$ use `hierarchy`$$ create trigger `insert_location` before insert on location each row begin declare x int unsigned default 1; if new.parent not null select `level`+1 x location id = new.parent; end if; set new.`level` = x; end$$ delimiter ; set sql_mode=@old_sql_mode; set foreign_key_checks=@old_foreign_key_checks; set unique_checks=@old_unique_checks; -- ----------------------------------------------------- -- data table `hierarchy`.`level` -- ----------------------------------------------------- start transaction; use `hierarchy`; insert `hierarchy`.`level` (`id`, `name`) values (1, 'parent'); insert `hierarchy`.`level` (`id`, `name`) values (2, 'region'); insert `hierarchy`.`level` (`id`, `name`) values (3, 'country'); insert `hierarchy`.`level` (`id`, `name`) values (4, 'district'); commit; insert location (parent,name,level) values (null,'a1',null), (null,'a2',null), (null,'a3',null); insert location (parent,name,level) values (1,'b1',null), (1,'b2',null), (2,'b3',null), (2,'b4',null), (3,'b5',null), (3,'b6',null); insert location (parent,name,level) values (4,'c1',null), (4,'c2',null), (5,'c3',null), (5,'c4',null), (6,'c5',null), (6,'c6',null), (7,'c7',null), (7,'c8',null), (8,'c9',null), (8,'c10',null), (9,'c11',null), (9,'c12',null); select * details_location; [(mostly) auto-generated code mysql workbench 5.2]
note trigger insert_location determines item's level @ registration time, increasing parent's level 1 unit.

Comments
Post a Comment