简体   繁体   English

数据库设计选择

[英]Database Design Choices

I'm trying to create a work schedule creator with multiple stores and each store with multiple employees. 我正在尝试创建一个具有多个商店且每个商店都有多个雇员的工作计划创建者。 Each store can also access past schedule created, but only the current schedule is modifiable. 每个商店也可以访问过去创建的日程表,但是只能修改当前日程表。 The way I have my SQL database setup is that I have two tables, stores and employees. 我设置SQL数据库的方式是有两个表,即stores和employee。 Each employee has a store they work for, and time for all the days of the week, so I saved employees from all the stores in one table and get them as necessary with queries. 每个员工都有他们工作的商店,并且一周中的所有时间都在工作,因此我将所有商店中的员工保存在一个表中,并在需要时通过查询来获取他们。

My question is, should I add another column (week) to the employee table to designate which week that schedule is for, or create a new html file for each schedule for that store's past schedules? 我的问题是,我应该在雇员表中添加另一列(星期)以指定该日程表是在哪个星期进行的,还是应该为该商店的过去日程表的每个日程表创建一个新的html文件? I like the second choice because it decreases the chances of my SQL database being corrupted. 我喜欢第二个选择,因为它减少了我的SQL数据库损坏的机会。 As past schedules are unmodifiable, I have no issues with it. 由于过去的时间表无法更改,因此我没有任何问题。

PS just to make sure: if i write to a file using php, say $name = "monir"; write("My name is $name") PS只是为了确保:如果我使用php写入文件,说$name = "monir"; write("My name is $name") $name = "monir"; write("My name is $name") . $name = "monir"; write("My name is $name") Will the file say "My name is $name" or "My name is monir" ? 文件将显示"My name is $name"还是"My name is monir"

Following the concept of Database normalization you should add another table called eg schedule which contains a reference to an employee and a store. 遵循数据库规范化的概念,您应该添加另一个表,例如schedule,其中包含对雇员和商店的引用。 In addition to that you should put your week number in this schedule table to uniquely identity your table row. 除此之外,您还应该将周数放在此计划表中,以唯一标识表行。

My questions is should I add another column (week) to the employee table 我的问题是我应该在员工表中添加另一列(星期)吗?

I would suggest creating a schedules table. 我建议创建一个schedules表。 Employees would then belong to the schedule and the schedule would belong to a store. 然后,员工将属于日程表,而日程表将属于商店。 This provides more flexibility and allows you to better store past schedules. 这提供了更大的灵活性,并允许您更好地存储过去的日程表。

I like the second choice because it decreases the chances of my SQL database being corrupted. 我喜欢第二个选择,因为它减少了我的SQL数据库损坏的机会。

You don't want to avoid a dynamic solution (table) in favor of a static one (html page) simply to prevent users from updating information. 您不想仅仅为了防止用户更新信息而避免使用动态解决方案(表)来支持静态解决方案(HTML页)。 You simply need to create access rules in your application. 您只需要在应用程序中创建访问规则。

You should have store, employee and a table that joins the stores and employees as stores and employees do NOT have a one-to_one relationship. 您应该具有商店,员工和连接商店和员工的表,因为商店和员工之间没有一对一的关系。 Everyone I have ever known who worked in retail has had times when they were asked to work at other stores. 我认识的每个从事零售工作的人都曾被要求在其他商店工作。

Schedule should be in a separate table with the employee id, the week and the scheduled hours. 时间表应在单独的表中,其中包含员工编号,星期和计划时间。 A trigger should be put on this table to prevent schedules from past dates from being updated. 应该在此表上放置一个触发器,以防止更新过去日期的日程表。 Alternatively you could have a view that exposes only the current schedule and future schedules and make all updates use the view but selects can use the whole table. 或者,您可以有一个视图,该视图仅公开当前计划和将来的计划,并使所有更新都使用该视图,但选择项可以使用整个表。 This would allow a database admin to change the past schedules if need be but not the application since it only uses the view. 这将允许数据库管理员根据需要而不是应用程序更改过去的时间表,因为它仅使用视图。

As the comments made by DA and the other solutions indicate, having a third table is the way to go. 正如DA和其他解决方案的评论所表明的那样,拥有第三张表是必经之路。 At a minimum, try having the following: 至少尝试以下操作:

store: id | 商店:id | name | 名称| address | 地址| etc. 等等

employee: id | 员工:id | first_name | first_name | last_name | last_name | etc. 等等

schedule: id | 时间表:id | store_id | store_id | employee_id | employee_id | week | 周| date | 日期| start_time | start_time | end_time 时间结束

While week is redundant since date is stored, it allows you to query much faster. 自从存储日期以来,星期是多余的,它使您可以查询得更快。 You don't need the archive/past bit because you can just query whether schedule.week is the current week or not. 您不需要存档/过去位,因为您只需查询schedule.week是否为当前星期。

I've actually just done something quite similar, but our system is a whole hell of a lot more complicated than this. 实际上,我所做的事情非常相似,但是我们的系统比这复杂得多。

Personally, given your present structure, I'd choose to add the extra column. 就个人而言,鉴于您目前的结构,我将选择添加额外的列。 It's not too much more work, and will give you the ability to reproduce the HTML files after the fact. 不需要太多的工作,并且可以让您在事后重现HTML文件。 I know you don't want to be able to modify past schedules, but that's more what I'd call business logic - so just prevent it from being possible somewhere in your code. 我知道您不想修改过去的时间表,但这就是我所说的业务逻辑-因此,请防止在代码中的某些地方使用它。

I'd have otherwise suggested you add another extra table for "weeks". 否则,我建议您为“周”添加另一个表。 Give it an ID, the start date and the end date, then use that "week_id" elsewhere to save duplication. 给它一个ID,开始日期和结束日期,然后在其他地方使用该“ we​​ek_id”来保存重复项。 You could also separate out schedule entries from employee records, so each employee would have a unique ID and your "schedule" table would be: ID, employee_id, week_id, date, start_time, end_time. 您还可以将时间表条目与员工记录分开,因此每个员工都将具有唯一的ID,并且您的“时间表”表将是:ID,employee_id,week_id,日期,start_time,end_time。

It'd give you a lot more control over your queries and reduce the duplication in the database quite considerably for later on. 它可以让您更好地控制查询,并在以后大大减少数据库中的重复项。 Also, unless your database is really really unstable, corruption shouldn't be a problem. 另外,除非您的数据库真的非常不稳定,否则损坏应该不是问题。

For your PS - as long as you surround your output in double-quotes, PHP will interpolate your values. 对于您的PS-只要您将输出用双引号引起来,PHP就会插值。 So you'll get "My name is monir", rather than "My name is $name". 因此,您将获得“我的名字是monir”,而不是“我的名字是$ name”。

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

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