2012-05-20 16 views
2

我需要爲用戶和項目設計一個mysql數據庫。除此之外其它的相關信息,項目表保存在羯羊該項目是mysql db記錄可見性(設計和選擇幫助)

  • 對所有用戶可見
  • 可見到位於一個或多個地理區域的用戶
  • 可見只有選定的用戶信息

例如,項目對位於區域1,5和8中的所有用戶可見,但也對n個不在區域1,5和8中的選定用戶可見。

地理位置用戶區域存儲在用戶表中(例如region:4)

我的問題是在哪裏存儲項目可見性,項目區域和選定用戶以及如何向用戶顯示(選擇)所有相關項目。

我需要顯示的用戶是可見的,因爲他的區域是對用戶可見的所有用戶

  • 所有項目

    • 列表
    • 是給用戶,因爲可見他被選中看到該項目

    任何幫助,他會高度讚賞,謝謝!

  • 回答

    0

    這是表結構。請注意,根據您的MySQL版本,你可能需要的類型= INNODB更改爲ENGINE = INNODB:

    drop table if exists prjusers; 
    
    drop table if exists proj_users; 
    
    drop table if exists project_regions; 
    
    drop table if exists projects; 
    
    drop table if exists regions; 
    
    /*==============================================================*/ 
    /* Table: prjusers            */ 
    /*==============================================================*/ 
    create table prjusers 
    (
        id_prjuser   int not null auto_increment, 
        id_region   int not null, 
        user_name   varchar(100) not null, 
        primary key (id_prjuser) 
    ) 
    type = InnoDB; 
    
    /*==============================================================*/ 
    /* Table: proj_users           */ 
    /*==============================================================*/ 
    create table proj_users 
    (
        id_proj_user   int not null auto_increment, 
        id_prjuser   int not null, 
        id_project   int not null, 
        primary key (id_proj_user) 
    ) 
    type = InnoDB; 
    
    /*==============================================================*/ 
    /* Table: project_regions          */ 
    /*==============================================================*/ 
    create table project_regions 
    (
        id_project_region int not null auto_increment, 
        id_project   int not null, 
        id_region   int not null, 
        primary key (id_project_region) 
    ) 
    type = InnoDB; 
    
    /*==============================================================*/ 
    /* Table: projects            */ 
    /*==============================================================*/ 
    create table projects 
    (
        id_project   int not null auto_increment, 
        prj_code    varchar(100) not null, 
        global_project  char(1) not null, 
        primary key (id_project) 
    ) 
    type = InnoDB; 
    
    /*==============================================================*/ 
    /* Table: regions            */ 
    /*==============================================================*/ 
    create table regions 
    (
        id_region   int not null auto_increment, 
        region_name   varchar(100) not null, 
        primary key (id_region) 
    ) 
    type = InnoDB; 
    
    alter table prjusers add constraint FK_relationship_12 foreign key (id_region) 
         references regions (id_region) on delete restrict on update restrict; 
    
    alter table proj_users add constraint FK_relationship_10 foreign key (id_prjuser) 
         references prjusers (id_prjuser) on delete restrict on update restrict; 
    
    alter table proj_users add constraint FK_relationship_11 foreign key (id_project) 
         references projects (id_project) on delete restrict on update restrict; 
    
    alter table project_regions add constraint FK_relationship_8 foreign key (id_project) 
         references projects (id_project) on delete restrict on update restrict; 
    
    alter table project_regions add constraint FK_relationship_9 foreign key (id_region) 
         references regions (id_region) on delete restrict on update restrict; 
    

    select語句:

    select id_project, prj_code from projects where global_project='Y' 
    UNION 
    select projects.id_project, projects.prj_code 
    from projects join project_regions on projects.id_project = project_regions.id_project 
    join prjusers on prjusers.id_region = project_regions.id_region 
    where prjusers.id_prjuser = {$_SESSION['id_prjuser']} 
    UNION 
    select projects.id_project, projects.prj_code 
    from projects join proj_users on projects.id_project = proj_users.id_project 
    where proj_users.id_prjuser = {$_SESSION['id_prjuser']} 
    

    上面會做3個工會和拿出(1)的項目ID和項目代碼落在登錄用戶的區域中,該用戶的ID保存在會話變量(2)中,並且所有項目都明確分配給該用戶。

    0

    用戶-------- * USER_PROJECT * -----項目------- * REGION_PROJECT * -----地區--- * USER

    用戶和項目與映射表USER_PROJECT多到很多相關的

    USER和地區是多到一個相關

    項目和地區都多到很多相關的一個映射表REGION_PROJECT

    這評論意在補充@ somnath的答案。我無法在評論中添加這種關係。 @somnath,我希望我的答案解釋了你設計正確的表格之間的關係。