« first day (1670 days earlier)      last day (988 days later) » 

2:50 AM
@TehShrike, is it okay that 1 table there are 1-2 tables referencing ?, example I have bill_header with id(pk) then bill_lines bill_id(fk) reference bill_header id,then I have payment bill_id (fk) reference bill_header ?
 
It's pretty common to have multiple tables referencing rows in a single other table.
That schema sounds pretty reasonable to me.
But what if people send you a payment that is meant to pay off multiple bills at once?
 
I am loading here....:) I can't imagine the scenario...you mean if customer will pay all his previous bill and also his current at once ?
 
@jemz yup! Sometimes people take a long time to pay
You bill them for January, then you bill them for February, then they send you a check for everything
 
3:06 AM
I see, what would I do ? what would be my changes of payment table remove the references ?
 
well it depends on what you need
but you may need a "payment receiving" step where you apply a payment to various bills
maybe a "payment application" table that links a single payment to any number of bills, specifying the amount that is applied to each bill
with columns like payment_id, bill_id, amount
and a unique key on payment_id, bill_id
or, if that seems like too much effort for a prototype, you could just force your users to enter in the single check as two different payments against the two different bills
and go for the more robust system later when you really need to
 
@TehShrike,ok, I will change my payment_table to payment_receivng then with columns payment_id,bill_id,amount ?
 
@jemz well you'll need two tables
one to represent the entire payment
"the customer gave me a check for $1000, and it was check number 1061"
and one to assign that payment to bills
"300$ to the February bill, 600$ to the January bill, and 100$ unassigned"
 
3:21 AM
@TehShrike, so my payment_receiving table is the table that represents the entire payment with column, payment_id,bill_id,amount?
 
@jemz no bill_id
because the payment could be assigned to multiple bill_ids
but you do have a payment id, an amount, probably a payment type (check/cash/card/etc)
and maybe a check/transaction number
 
@TehShrike, and customer account_number ?
 
yes.
 
@TehShrike, so the insertion would be like this base in your given example
```
payment_id account_number amount
1 ABC-123 300

2 ABC-123 600

```
excuse me how do I format here
 
I guess you have to hit the
"fixed font" button
that appears after you type in more than one line
 
3:29 AM
payment_id account_number  amount
1             ABC-123       300

2             ABC-123        600
@TehShrike, is that the insertion would be ?
what about the 100 un assigned ?
or you mean this
payment_id account_number  amount
1             ABC-123       1000
@TehShrike please help me I am confuse
@TehShrike so in my other table what probably the columns ?
 
payment: id 1, account number 12345, amount 1000, check/transaction number 9999
payment_allocation: payment_id 1, bill 50, amount 300
payment_allocation: payment_id 1, bill 51, amount 600
payment_allocation: payment_id 1, bill null, amount 100
 
@TehShrike,payment_allocation looks like the line of my payment_receiver ?
@TehShrike, what is bill 50? stands for is it bill_id ? the id in my bill_header ?
 
@jemz right.
@jemz I am unfamiliar with your payment_receiver table
 
3:44 AM
@TehShrike, payment_receiver i just rename my payment_table to payment_receiver
 
well you'll need two tables
you can't have multiple rows for a single payment without having a single payment record somewhere
 
@TehShrike, yes I understand now, I will create payment_table and payment_allocation ,I will delete the payment_receiver . am I right ?
 
@jemz Those sound like good names to me (except your table name shouldn't have _table in it)
 
@TehShrike, ok i will remove the _table, by the way is it
@TehShrike, what would I do for the unsigned 100 ?
in the payment_allocation
 
@jemz that's a bit of a matter of taste. You could add an "unallocated payment amount" column to your payment table, and deal with the difficulty of guaranteeing that it is always up to date.
You could add up all the rows in your payment allocation table and compare them to the amount in the payment table, and note the difference when it is non-zero. Or you could have a row in your payment allocation table with a NULL bill id, and then have the difficulty of guaranteeing that all of the payment allocation rows add up to the same number as what you put for the amount in the payment table.
 
3:56 AM
@TehShrike, ok thank you I will create first this two tables.. I will write back to you if I have some problem. thank you again
 
@jemz No problem, good luck! :-)
 
 
1 hour later…
5:11 AM
@TehShrike, is this okay amount dataType decimal,10,2?
 
@jemz For payment amounts, measured in US currency? Yes, that seems good to me
 
@TehShrike, in payment_allocation I will add foreign_keys for the bill_id and pament_id reference to there tables ?
 
@jemz right.
You can allocate the same payment to multiple bills, and multiple bills can have multiple payments allocated to them.
 
ok thank you I will add foreign keys
 
You should have a unique key on payment_id, bill_id though - you want to make sure the same payment isn't allocated to the same bill multiple times, that would make things confusing
 
5:20 AM
CREATE TABLE `payment_allocation` (
	`payment_allocation_id` INT(11) NOT NULL AUTO_INCREMENT,
	`payment_id` INT(11) NOT NULL DEFAULT '0',
	`bill_header_id` INT(11) NOT NULL DEFAULT '0',
	PRIMARY KEY (`payment_allocation_id`)
)

ENGINE=InnoDB
@TehShrike, I don't know how to add unique key on payment_id and bill_header_id ?
 
@jemz how do you add any key/index to a table?
 
@TehShrike like this
CREATE TABLE `payment_allocation` (
	`payment_allocation_id` INT(11) NOT NULL AUTO_INCREMENT,
	`payment_id` INT(11) NOT NULL DEFAULT '0',
	`bill_header_id` INT(11) NOT NULL DEFAULT '0',
	PRIMARY KEY (`payment_allocation_id`)
	UNIQUE INDEX `payment_id` (`payment_id`),
	UNIQUE INDEX `bill_header_id` (`bill_header_id`)
)

ENGINE=InnoDB
 
yup, pretty much
 
ok thank you
 
except you don't want to limit your allocations so that you can only have one row for a given payment
you want to be unique on payment/billing
you want UNIQUE INDEX payment_bill (payment_id, bill_header_id)
 
5:24 AM
ah ok is this what you mean UNIQUE INDEX payment_bill (payment_id, bill_header_id)
@TehShrike, I change it now as you suggested. thank you again
 
Any index can involve multiple columns. This is important because generally MySQL will only use one index per table.
 
Thank you
 

« first day (1670 days earlier)      last day (988 days later) »