PDA

View Full Version : A SQL composite key with 3 members?



batman
21st January 2015, 16:19
Hello everyone, I got stuck trying to normalize my database. I'm putting my client requirements and, in bold, the tables involved so you can see the whole picture:
Requirements:
• One Payment Application are linked to only one Contract.
• One Payment Application has one or more Bills.
• One Bill’s Payment can be associated to one or more Beneficiaries. This implies a many to many relationship between Bills and Beneficiaries.
• One Contract has one or more Beneficiaries. A Beneficiary is mostly a bank account.
• One Contract has one or more Currencies. Since in Cuba we have two currencies, a Contract may have from 1 to 2 Currencies.
• One Beneficiary has only one Currency.
• One Paycheck must be made for each Beneficiary used in a Payment Application. This implies a many to many relationship between Beneficiaries and Payment Application. So the Paycheck table should have Beneficiaries.Id and ‘Payment Application’.Id as multiple key and Paycheck.Id as a unique field.
• Paychecks can be cancelled though they remains persistent as the can be reviewed in a report. Paychecks has 3 status: Delivered, In Transition or Cancelled. This implies that the key from Paychecks table must be changed since there can be several cancelled Paychecks and only one Paycheck either In Transition or Delivered, other possibilities are considered as fraud.
To solve this it would be necessary to add a third member to the key in the Paycheck table. But I haven’t see this and I think this violates a Codd norm. Does somebody know how to solve this or has encountered with a situation near this one?

Added after 47 minutes:

I found a solution, I'm posting it so you can criticize it:
I will create a table named Cancelled Paychecks so I can move cancelled checks there. That way I can eliminate any conflicts while I assure that the user won't commit any fraud. This table will be like Paycheck table but it won't have the status field since in here I will only store cancelled checks.

batman
21st January 2015, 17:39
Come to think this solution has an issue, if I'm not careful I might end it having the same paycheck in both tables. Moreover, someone may use this to corrupt the logical structure making a check to appear either as cancelled and delivered. I could use a row-level trigger on each INSERT to solve this but I rather find out a more suitable solution.

ChrisW67
21st January 2015, 20:39
This is what I think all that boils down to...

You have paychecks associated with two foreign keys. Each paycheck has one of three states, two of the states can only only exist once for a given key pair. The third paycheck state (Cancelled) can appear multiple times for a given key pair. You want a database constraint of some sort to enforce this.

Two thoughts:

Use a paycheck state value of null to mean "cancelled" and put a unique constraint on the two key and one state column. Two nulls are never equal so this should work.
Use before insert and before update triggers to stop attempts to create a second entry of a paycheck with the unique states.


Ok, I have a third thought... are you never going to pay the same beneficiary twice? In that case you would expect to see another cheque in transit, the earlier cheques in delivered.


Not much of a Qt question though.