mysql db records visibility (design and select help)

标签 mysql database database-design

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

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

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

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

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

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

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

他将不胜感激任何帮助,谢谢!

最佳答案

这是表结构。请注意,根据您的 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 并得出全局的项目 ID 和项目代码 (1),它们位于已登录用户的区域中,其 ID 保存在 session 变量中 (2) 和所有项目明确分配给该用户。

关于mysql db records visibility (design and select help),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10673428/

相关文章:

mysql - 如何按周对具有 Unix 时间戳的行进行分组?

database - 数据导入/导出过程的 Abinitio 和推荐使用场景

php - 无法使用 Codeigniter 中提供的设置连接到您的数据库服务器

c# - MVC 4 中的 OO 设计/实现

mysql - 动态显示计算结果与将预先计算的结果存储在表格中

database - 在 Qlikview 中处理多个事实表

php - 多个进程更新相同的行会导致mysql死锁?

php - Codeigniter - 从二级表中获取值(value)

Mysql过滤查询where条件

mysql - 错误: No unique index found for the referenced field of the primary table