-2

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. 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).

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:

  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 (i.e. Insurance 1 from February = $100 became $200 in March). 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.
  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? 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: enter image description here

My second ERD: enter image description here

(I did this to try and normalize it but for the system's requirement, I don't think this is appropriate)

philipxy
  • 14,867
  • 6
  • 39
  • 83
Carl Eren
  • 31
  • 7
  • 1
    Why don't you have a separate table for deductions? And then a 1-N relation from payslip_deduction to deduction. – Olle Härstedt Mar 20 '15 at 17:48
  • @OlleHärstedt my second erd has it. – Carl Eren Mar 20 '15 at 17:50
  • Wait, what kind of field is `deductionN`? – Olle Härstedt Mar 20 '15 at 17:52
  • If you don't have 30 deductions per row then you'll have redundant fields. You need to normalize this as suggested above – Abu Nooh Mar 20 '15 at 17:52
  • are the earning1-4 and deduction1-30 suppose to be columns in the table or are you trying to show records? – whitwhoa Mar 20 '15 at 17:53
  • Thing is, ALL of those deduction fields holds a value. I can't say them as the product owner only gave the acronyms but ensured me that all of those fields do have values. – Carl Eren Mar 20 '15 at 17:55
  • @nullReference yes they're columns, each deduction and earning holds a value, for example earning 1 is monthly salary, earning 2 is allowance and so on. – Carl Eren Mar 20 '15 at 17:56
  • 2
    *"(I did this to try and normalize it but for the system's requirement, I don't think this is appropriate)"* You are seriously mistaken. – Strawberry Mar 20 '15 at 18:05
  • @Strawberry yes it would seem so. I'm thankful though that I was proven wrong as this helped me see erds in a more appropriate way. – Carl Eren Mar 20 '15 at 18:09
  • "Normalized" has 2 general senses, "... to 1NF" & "... to higher NFs". Collapsing a group of tables or columns that differ by some parameter(s) into one table with a column per parameter is typically good design but [it is not normalization](https://stackoverflow.com/a/40640962/3404097). If that's what your instructor means, they are misusing the term--as are the current answers. PS Please ask 1 question per post. – philipxy Jan 09 '20 at 05:35

3 Answers3

1

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:

enter image description here

This way you can have as many earning records and deduction records associated with a payslip as you need.

whitwhoa
  • 2,389
  • 4
  • 30
  • 61
  • 1
    I would argue to combine earnings and deductions tables, and add an extra field so you know whether it's an earning or a deduction. In the end it's all just itemized payslip stuff and their attributes are the same. – JNevill Mar 20 '15 at 18:06
  • Whoah this is a very good answer, I haven't thought about this but this makes so much sense now than my previous erd. I was more focused on displaying each earnings/deductions through columns when in fact they could be on the rows. I guess my problem was I created the erd with the acronyms intact and now that I've changed them to deduction1, deduction2 it made it more clear that they should be on the rows and I could create another column to store their acronym. – Carl Eren Mar 20 '15 at 18:15
  • arghhh it pains me that I can't up this as I have a low rep but I would like to remind the readers THAT THIS SHOULD BE THE ERD (although it should be combined). – Carl Eren Mar 20 '15 at 18:39
0

My guess is that your prof wants you to not have 30 deduction fields. 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.

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. You tell it whether that item was an earning or a deduction in the type field. You say which earning or deduction it was in that number field... probably not the best name, but good for example. And then the value of that earning/deduction in the value field. So in reality your payslip_items table will be 34 records (30 deductions and 4 earnings) for each payslip...

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. It will grow and shrink as you need.

JNevill
  • 46,980
  • 4
  • 38
  • 63
  • Good point, my query could select all earnings,deductions and add. This seems better although due to the system's limitations specifically the `payslip reader` feature that reads those spreadsheets, it would still be limited since I need to recode the `payslip reader` to read the specific cell that holds the value and just a heads up, the payslip they're uploading isn't very clean, they're formatted to display as a normal payslip should (i.e deduction1 is on c2 deduction2 is on d5). – Carl Eren Mar 20 '15 at 18:06
  • But overall this is a VERY GOOD answer and just might get the erd to get accepted despite the problem this solves is still held down by the limitation of the system(can only read fixed cell destinations). – Carl Eren Mar 20 '15 at 18:07
  • I think the problem you are facing, though, is best solved in the front end and how you connect/translate that to the DB. You would just have to move through each cell that is a deduction/earning and insert a new record for each hit. – JNevill Mar 20 '15 at 18:09
  • Also, @nullReference's ERD shows pretty much what I'm advocating here. I just took it one step further and combined your deductions table and your earnings table from your second ERD and then denormalized. I would have made an ERD too... but that would take me like a day because I'm a spazz with that kind of software. Lines and squares and relationships.. ahhh! – JNevill Mar 20 '15 at 18:10
-1

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 enter image description here

dipenparmar12
  • 3,042
  • 1
  • 29
  • 39
  • Please [use text, not images/links, for text--including tables & ERDs](https://meta.stackoverflow.com/q/285551/3404097). Paraphrase or quote from other text. Give just what you need & relate it to your problem. Use images only for what cannot be expressed as text or to augment text. Images cannot be searched for or cut & pasted. Include a legend/key & explanation with an image. PS Without an explanation this is a poor answer. Also it doesn't add anything to the other answers. [answer] [help] – philipxy Jan 09 '20 at 05:18
  • Thanks for your response. added Mysql Schema as well as a visual presentation. – dipenparmar12 Jan 09 '20 at 06:52