1

I'm looking for the best practice for designing a database schema where someone can post a template of something (for example how a certain 'schedule' looks like) and then user can 'join' that template but change stuff from it without of course changing the original template.

I'll add an example, as i'm not the best in explaining something in text form. (and english is not my native language)

Say you have 4 tables:

  • Schedule
  • Day
  • Task
  • TaskRule

A schedule can have N days, a day can have N tasks, and a task finally can have N taskRules. A user will 'join' this schedule via a join table (user_schedule that has the user id and the schedule_id).

Now the problem is that when a user changes something from the template (lets say he adds a few taskRules or he removes a task) the schedule will change for everyone that is connected to it.

My question is what is the best practice to handle this? I really can't see how this can be done other then row duplication which is not my favorite thing to do.

Edit: This schema means he will join the template schedule and choose 1 of 2 things (namely to change it or not to change it, if he doesn't change it duplication is clearly redundant)

4

2 回答 2

1

你有不同的选择:

  1. 复制所有数据。这很容易做到,但会重复大量数据,这可能是个问题,尤其是在修改除外的情况下。

  2. 一个变体通常只是链接到模板并仅在实际进行更改时复制它。这增加了代码的复杂性,但可能会节省大量数据。这也可以单独应用于每个表。

  3. 链接数据+更改:您只能参考问题中建议的时间表,然后跟踪特定用户所做的所有更改。基本上正如 Spark 所描述的那样。但并不是说您可能还需要处理删除和更改。这是一种更复杂的处理方式,但甚至可能在“加入”发生后处理对时间表的更改。

于 2012-08-22T13:08:08.443 回答
1

我的建议是:

  • 将任务重命名为 task_template。
  • 使用 template_id、user_id 和 task_template 中的所有其他字段创建一个名为 task 的表。
  • 添加数据时,仅在新表中插入差异。
  • 加载数据时,您会读取 template_id 并用行中非空的内容覆盖。

这里的主要问题是您试图在 sql 数据库中实现继承/扩展/原型设计......

于 2012-08-22T12:39:43.670 回答