mysql db记录可见性(设计和选择帮助)

时间:2012-05-20 12:33:42

标签: mysql database database-design

我需要设计一个包含用户和项目的mysql数据库。除了其他信息之外,项目表还包含项目信息

  • 对所有用户可见
  • 对位于一个或多个地理区域的用户可见
  • 仅对所选用户可见

例如,项目可以对位于区域1,5和8中的所有用户可见,但也可以对不在位于区域1,5和8中的n个选定用户可见。

用户的地理区域存储在用户表中(例如region:4)

我的问题是存储项目可见性,项目区域和选定用户以及如何向用户显示(选择)所有相关项目的位置。

我需要向用户显示所有项目的列表

  • 对所有用户可见
  • 由于他所在的区域而对用户可见
  • 对用户可见,因为他被选中以查看项目

任何帮助都会非常感谢,谢谢!

2 个答案:

答案 0 :(得分:0)

这是表格结构。请注意,根据您的MySQL版本,您可能需要将type = 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 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个UNIONS并提出全局(1)的项目ID和项目代码,它们属于登录用户的区域,其ID保存在会话变量(2)和所有项目中明确分配给该用户的。

答案 1 :(得分:0)

USER -------- * USER_PROJECT * ----- PROJECT ------- * REGION_PROJECT * ----- REGION --- * USER

USER和PROJECT是多对多的映射表USER_PROJECT

USER和REGION是多对一相关的

PROJECT和REGION是多对多关系的映射表REGION_PROJECT

此评论旨在补充@ somnath的答案。我无法在评论中添加此关系。 @somnath,我希望我的答案能够解释你正确设计的表格之间的关系。