简体   繁体   English

数据库设计-哪个最适合?

[英]Database Design - Which would be best for this?

I'm a newbie messing around at designing a database. 我是一个设计数据库的新手。 I want to create a database(using mysql) that contains employee information. 我想创建一个包含员工信息的数据库(使用mysql)。 Then I will write Web Client to display each employee profile. 然后,我将编写Web客户端以显示每个员工资料。 I so far the columns would be: 到目前为止,我的专栏文章是:

1) user id
2) first name
3) last name
4) email address
5) phone number
6) fax number
7) department(which will be like a category)

Would the best design be make items 1-6 columns in one table and then have the department column in it's own table(with a id column)? 最好的设计是将一个表中的1-6列放在一个表中,然后在自己的表中包含一个Department列(带有一个id列)? Or should I make all the items their own table, giving each table an extra id column....and would this be #1 normalization form? 还是我应该将所有项目都设置为自己的表,为每个表分配一个额外的id列...。这将是#1规范化表格吗?

只要您的所有列都不是多值的(例如,一个员工可以有多个部门),那么您的设计就是最佳的。

Your first solution is better, Items 1 to 6 should all be in a same table users . 您的第一个解决方案更好,第1到第6项应全部位于同一表users If you have more info on the department to store then you need to have a specific table departments which will have at least two columns : id and name and then in the users table you will have a department _id column that will store an id corresponding to a department in the departments table. 如果您要存储有关部门的更多信息,则需要有一个特定的表departments ,该departments将至少有两列: idname ,然后在users表中, department _id列将存储与departments表中的departments

If you don't store any other info about the department, it might be better to store directly the department name in your users table to avoid having to JOIN tables everytime you retrieve or update information about a user. 如果您没有存储有关部门的任何其他信息,最好将部门名称直接存储在users表中,以避免每次检索或更新有关用户的信息时都必须联接表。

User_Master
------------------

UserID - Primary Key
DeptID  - Foreign Key
FirstName
LastName
EmailID
PhoneNumber
FaxNumber


Dept_Master
----------------

DeptID - PrimaryKey
DepartmentName

Some further thoughts... 一些进一步的想法...

Don't forget to think about time. 不要忘记考虑时间。 Can employees change departments, and are you interested in knowing this history? 员工可以更换部门吗?您有兴趣了解这一历史吗? If that's the case you'll have to have a separate Departments table, and probably a Service (or something) table with EmployeeID, DepartmentID, StartDate and EndDate. 如果是这种情况,则必须有一个单独的Departments表,并且可能要有一个带有EmployeeID,DepartmentID,StartDate和EndDate的Service(或其他)表。

While thinking about changes that happen over time, do phone & fax numbers and email addresses fit with the employee or the position? 在考虑随时间而发生的变化时,电话和传真号码以及电子邮件地址是否适合员工或职位? If Janet in accounts lands the supervisor's job, does she get a different phone number in her new office, or is it one of those places where the telephone numbers move with the people. 如果珍妮特(Janet)担任主管的工作,是在新办公室里获得了另一个电话号码,还是电话号码随人移动的地方之一。 Ditto for email. 同上电子邮件。 Are addresses hr.officer@example.com or joe.smith@example.com? 地址是hr.nzl@example.com还是joe.smith@example.com? If the former in both cases you probably want to think about a Position table that keeps track of phone/fax/email (as well as paygrade, FT or PT etc) which has a DepartmentID foreign key. 如果在这两种情况下都是前者,则您可能想考虑一个位置表,该位置表跟踪具有DepartmentID外键的电话/传真/电子邮件(以及薪水,FT或PT等)。

If you are thinking in SQL, you definitely do well to think in (at least) first normal form. 如果您正在考虑使用SQL,那么绝对可以(至少)以第一范式进行思考。 Multivalued columns are not only slow, they are also the first step towards chaos. 多值列不仅速度慢,而且是走向混乱的第一步。 This rule doesn't apply if you move towards a Nosql based solution. 如果您转向基于Nosql的解决方案,则此规则不适用。

Normal forms 2 through 5 help with populating the tables, but won't help you with your web client. 普通表格2到5有助于填充表格,但不会帮助您使用Web客户端。 The biggest thing you'll gain from full normalization is the guarantee that the database doesn't contradict itself. 通过完全规范化,您将获得的最大好处就是保证了数据库不会与自身矛盾。 Things like two different names for the same department, in two different employee records (rows). 在两个不同的员工记录(行)中,同一部门的两个不同名称之类的东西。

The big thing you need to add to your vision is views. 您需要添加到视野中的最大事情是视图。 You can use views to very good effect to make the data lok more the way you want a web page to look. 您可以使用视图来达到很好的效果,使数据看起来更像您希望网页显示的样子。 This will make building the web client lots easier. 这将使构建Web客户端更加容易。 There are areas where views aren't going to help either. 在某些领域,视图也无济于事。

Take drop down lists. 采取下拉列表。 If as has been suggested, people can have more than one phone number, you might want to have a drop down list for phone numbers on the employee's page. 如果按照建议,人们可以拥有多个电话号码,那么您可能希望在员工页面上有一个电话号码的下拉列表。 This is basically a multivalued field, and views aren't especially helpful in that regard. 这基本上是一个多值字段,并且视图在这方面并不是特别有用。

I think you can have department columns like department1, department2 etc. which will consist ids of each department. 我认为您可以具有部门列,例如department1,department2等,其中将包含每个部门的ID。 You can create another table called department which will have ids and department name. 您可以创建另一个名为Department的表,该表将具有ID和部门名称。 Now you can link the department ids in the first table to the second one. 现在,您可以将第一个表中的部门ID链接到第二个表中。 This will not mess if an employee is belonging to multiple departments. 如果员工属于多个部门,这不会混乱。

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

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