简体   繁体   English

数据库设计:多个用户实体。 单表或多表

[英]Database Design: Multiple User Entities. Single or Multiple Tables

I am building a PHP app that has 3 basic entity types: Coach, Student, Lesson, where coaches create digital lessons for students. 我正在构建一个具有3种基本实体类型的PHP应用程序:教练,学生,课程,其中教练为学生创建数字课程。 I'm using MySQL and innoDB tables. 我正在使用MySQL和innoDB表。

Requirements 要求

  1. Coach and student login. 教练和学生登录。
  2. Coach can deliver a digital lesson specifically for a single student. 教练可以专门为单个学生提供数字课程。

I'm unsure what is the best DB schema to use given the requirements. 我不确定给定要求要使用的最佳数据库架构是什么。 Here are two options: 这里有两个选择:

Option 1 选项1
User (PK id, user_type (coach or student), firstname, lastname, email, password, etc...) 用户(PK ID,用户名(教练或学生),名字,姓氏,电子邮件,密码等)
Lesson (PK id, FK coach_user_id (ref: User.id), FK student_user_id (ref: User.id), lesson_name, etc…) 课程(PK id,FK coach_user_id(参考:User.id),FK student_user_id(参考:User.id),lease_name等)

Pros: 优点:
- One user table -一个用户表
- Each user has a unique ID and email -每个用户都有唯一的ID和电子邮件
- Makes login auth easy with single table -通过单个表使登录身份验证变得容易

Cons: 缺点:
- No validation of user_type when a coach or student User.id is recorded as a FK in the lesson table. -当教练或学生的User.id在课程表中记录为FK时,不验证user_type。 This problem will reoccur in any new table where a coach or student User.id needs to be recorded as a FK. 此问题将在教练或学生User.id需要记录为FK的任何新表中再次出现。
- Potential polymorphism issues and the need to normalise down the track. -潜在的多态性问题以及规范化的必要性。

Option 2 选项2
Coach (PK id, firstname, lastname, email, password, etc...) 教练(PK ID,名字,姓氏,电子邮件,密码等)
Student (PK id, firstname, lastname, email, password, etc...) 学生(PK ID,名字,姓氏,电子邮件,密码等)
Lesson (PK id, FK coach_id (ref: Coach.id), FK student_id (ref: Student.id), lesson_name, lesson_text, etc…) 课程(PK ID,FK教练ID(参考:Coach.id),FK学生ID(参考:Student.id),课程名称,课程文本等)。

Pros: 优点:
- Normalised DB schema. -标准化的数据库架构。 Independent coach, students entity tables. 独立教练,学生实体表。
- No user type validation issues. -没有用户类型验证问题。 Coach ID and student ID FK's point independently to Coach.id and Student.id respectively. 教练ID和学生ID FK分别指向Coach.id和Student.id。

Cons: 缺点:
- Coach and student can have the same ID. -教练和学生可以具有相同的ID。 (This can be solved though with ID prefixes eg C1001, S1001) (这可以通过ID前缀来解决,例如C1001,S1001)
- Coach and student can have the same email. -教练和学生可以使用相同的电子邮件。
- Login auth involves querying two 2 tables for single login page, or creating 2 different login pages and auth request types. -登录身份验证涉及为单个登录页面查询两个2表,或创建2个不同的登录页面和身份验证请求类型。

I'm really torn which is the best way to go. 我真的很伤心,这是最好的方法。 Is there a better way to do this? 有一个更好的方法吗?

In my opinion, both of your approaches would work. 我认为,两种方法都行得通。 The first one is more universal, and capable of fitting various currently unknown requirements . 第一个是更通用的,并且能够满足各种当前未知的要求。 If you choose it, I'd recommend to add concept of Role to the model - "user_type" is a role, and one user can be associated with different roles [at the same time]. 如果选择它,我建议将角色的概念添加到模型中-“ user_type”是一个角色,并且一个用户可以[同时]与不同的角色相关联。 Also, "The Data Model Resource Book" by Len Silverston is a great resource . 另外,Len Silverston的“数据模型资源书”也是一个很好的资源。

However, you may not always want your schema to be too general. 但是,您可能并不总是希望您的架构过于笼统。 You listed pros and cons for 2 approaches on very low level; 您在非常低的级别列出了两种方法的优缺点; I think that practicability is more important than particular technical issues (which can be overcome ). 我认为实用性比特定的技术问题(可以克服)更为重要。 I'd put it that way : 我这样说:

1) Pros : 1)优点:

  • easy to accommodate new features without major changes to schema 易于容纳新功能,而无需对架构进行重大更改
  • very flexible 非常灵活
  • easy to build cubes on top of the schema 易于在架构之上构建多维数据集
  • fits long term projects 适合长期项目

Cons : 缺点:

  • requires more resources (experienced DBA/Data Model specialist[s], comparatively longer design time ) 需要更多资源(经验丰富的DBA /数据模型专家,设计时间相对较长)
  • way more complex than (2) 比(2)更复杂

2) Pros : 2)优点:

  • fast delivery of first working version 快速交付第一个工作版本
  • quite easy for understanding even by non-technical people (until it grows up) 即使是非技术人员也很容易理解(直到它长大)
  • fits either small projects or projects with well-defined domains which [almost] never change 适合小型项目或具有明确定义的领域的项目,这些领域[几乎]永远不会改变

Cons : 缺点:

  • never ending refactoring of schema as new requirements come 随着新需求的到来,架构的重构永无止境
  • if project lives long enough, database becomes full of "not used anymore" columns(or other db objects) nobody wants to touch 如果项目寿命足够长,则数据库将充满“没人要使用”的列(或其他数据库对象)
  • harder to enforce integrity 难以执行诚信

I hope it makes sense and helps you to make the right decision which fits your needs. 我希望这是有道理的,并可以帮助您做出适合您需求的正确决定。

Option 1 looks better to me. 选项1在我看来更好。

It will simplify your code when you don't care to distinguish students from coaches, and will be pretty much the same as option 2 if you want to distinguish them. 当您不希望将学生与教练区分开时,它将简化您的代码,并且如果您要区分他们,则与选项2几乎相同。

If you really need to validate the foreign keys you can use triggers to check if its a coach or not. 如果您确实需要验证外键,则可以使用触发器来检查其是否为教练。

I'm not sure what you mean by "Potential polymorphism issues and the need to normalise down the track." 我不确定“潜在的多态性问题和规范化的必要性”是什么意思 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM