[英]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,但我有几个问题:
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`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.