简体   繁体   English

如何规范化表格

[英]How to normalize table

I guess I should start first by explaining what the system actually does to get a better understanding.我想我应该首先解释系统实际做了什么,以便更好地理解。

The system is a payslip viewer website wherein the uploader(hr personnel) uploads a bunch of payslips in excel format and those entries get saved into the database.该系统是一个工资单查看器网站,其中上传者(人力资源人员)以 excel 格式上传一堆工资单,并将这些条目保存到数据库中。 The payslip viewer then fetches the emp_id of the currently logged in user and displays all their earnings, deductions, info AND their total earnings(all earnings are added), total deductions(all deductions are added) and netpay(total earning-total deduction) .工资单查看器然后获取当前登录用户的emp_id并显示他们的所有earnings, deductions, info和他们的总earnings(all earnings are added), total deductions(all deductions are added) and netpay(total earning-total deduction) .

My problem is that my professor said I need to "normalize" the table but I was left wondering how could I possibly normalize this as I think this is as "normalized" as it gets.我的问题是我的教授说我需要“规范化”表格,但我想知道我怎么可能将其规范化,因为我认为这是“规范化”的。 However, I did try to create two erds but I have a couple of questions:但是,我确实尝试创建两个 erd,但我有几个问题:

  1. Why would I split earnings and deductions when the point of the system is just to read what the uploader gives?当系统的目的只是为了阅读上传者提供的内容时,为什么我要分开收入和扣除额?
  2. My professor suggested I get the deductions that are same for all of the employees but there is no such thing, the closest one I could think of is the insurance which is also changing depending on the dependencies and other factors that influence it.我的教授建议我为所有员工获得相同的扣除额,但没有这样的事情,我能想到的最接近的是保险,它也根据依赖关系和其他影响它的因素而变化。
  3. If I did try to create no.3 a problem would be in the event of a change in rates (ie Insurance 1 from February = $100 became $200 in March).如果我确实尝试创建第 3 项,那么在费率发生变化时就会出现问题(即,2 月份的保险 1 = 3 月份的 100 美元变为 200 美元)。 Since the payslip viewer relies on that table, it would mean if a user would look at his payslip for the month of February, his insurance will also be $200.由于工资单查看器依赖于该表格,这意味着如果用户查看他 2 月份的工资单,他的保险也将是 200 美元。
  4. I'm creating this database to comply with the requirement of the system which is for the uploader to upload a spreadsheet of payslips, isn't that enough reason to justify my erd?我正在创建此数据库以符合系统的要求,即上传者上传工资单电子表格,这不是证明我的 erd 合理的充分理由吗? Maybe if this was a payroll system I could set up other tables than can influence the output of the payslip BUT based on the system, the outputs are already done and the hr just needs to upload them to the database.也许如果这是一个工资单系统,我可以设置其他表格,而不是影响工资单的输出,但基于系统,输出已经完成,人力资源只需要将它们上传到数据库。

My first ERD:我的第一个 ERD:在此处输入图片说明

My second ERD:我的第二个 ERD:在此处输入图片说明

(I did this to try and normalize it but for the system's requirement, I don't think this is appropriate) (我这样做是为了尝试对其进行规范化,但对于系统的要求,我认为这不合适)

From what i can tell it looks like you are pretty close, you just need to change your relationships to one to many for the deductions and earnings tables and remove all of the excess "earning" and "deduction" columns, leaving you with the following:据我所知,您似乎非常接近,您只需要将扣除额和收入表的关系更改为一对多,并删除所有多余的“收入”和“扣除额”列,为您留下以下内容:

在此处输入图片说明

This way you can have as many earning records and deduction records associated with a payslip as you need.通过这种方式,您可以根据需要拥有与工资单相关的尽可能多的收入记录和扣除记录。

My guess is that your prof wants you to not have 30 deduction fields.我的猜测是您的教授希望您没有 30 个扣除字段。 Like... when you set up a table and find yourself doing:就像......当你摆好一张桌子并发现自己在做:

object_typa_1 | object_typa_2 | object_typa_3 | object_typa_4 | object_typeb_1 | object_typeb_2 ..

And storing the values in their respective fields.并将值存储在各自的字段中。 Instead, you should normalize that in case some day you have to add deduction31 you don't have to do an ALTER TABLE and schluff around all your SQL to accomodate.相反,您应该规范化,以防有一天您必须添加deduction31您不必执行 ALTER TABLE 并围绕所有 SQL 进行 schluff 以适应。

Instead:反而:

Object Table:
type | number | value

In your case:在你的情况下:

employees:
emp_id | password | name | ...

payslips:
id | emp_id | other payslip attributes | ...


payslip_items:
payslip_id | type  | number | value

In the payslip_items table you stick in multiple records for each payslip.在 payslip_items 表中,您为每个工资单保留多条记录。 You tell it whether that item was an earning or a deduction in the type field.您可以在type字段中告诉它该项目是收入还是扣除。 You say which earning or deduction it was in that number field... probably not the best name, but good for example.你说那个number字段中的收入或扣除额......可能不是最好的名字,但例如很好。 And then the value of that earning/deduction in the value field.然后是值字段中该收入/扣除的value So in reality your payslip_items table will be 34 records (30 deductions and 4 earnings) for each payslip...因此,实际上您的 payslip_items 表将为每个工资单提供 34 条记录(30 项扣除额和 4 项收入)...

The reason you want to do it this way is that in the real world, as soon as you launch this thing for the client/business partner, they will want to add a new deduction or earning to the payslip (GUARANTEED).您想这样做的原因是,在现实世界中,一旦您为客户/业务合作伙伴推出此功能,他们就会希望在工资单中添加新的扣除额或收入(保证)。 This schema works because you don't have to ALTER TABLE and mess with ALL of your sql that deals with payslip_items.此模式有效,因为您不必更改表并弄乱处理 payslip_items 的所有 sql。 It will grow and shrink as you need.它会根据您的需要增长和缩小。

I think the below table structure will solve your problem.我认为下表结构将解决您的问题。

CREATE TABLE `Emp` (
    `id` int NOT NULL,
    `name` varchar(100) NOT NULL,
    `password` varchar(255) NOT NULL,
    `user_type` varchar NOT NULL,
    PRIMARY KEY (`id`)
);

CREATE TABLE `payslip_map` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `emp_id` INT NOT NULL,
    `payslip_id` INT NOT NULL,
    PRIMARY KEY (`id`)
);

CREATE TABLE `payslips` (
    `id` INT NOT NULL,
    `particular_id` INT NOT NULL,
    `amt` FLOAT NOT NULL
);

CREATE TABLE `particulars` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `particulars` varchar(80) NOT NULL UNIQUE,
    `is_earning` BOOLEAN NOT NULL,
    PRIMARY KEY (`id`)
);

ALTER TABLE `payslip_map` ADD CONSTRAINT `payslip_map_fk0` FOREIGN KEY (`emp_id`) REFERENCES `Emp`(`id`);

ALTER TABLE `payslip_map` ADD CONSTRAINT `payslip_map_fk1` FOREIGN KEY (`payslip_id`) REFERENCES `payslips`(`id`);

ALTER TABLE `payslips` ADD CONSTRAINT `payslips_fk0` FOREIGN KEY (`particular_id`) REFERENCES `particulars`(`id`);

Visual Presentation视觉呈现在此处输入图片说明

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

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