//flex table opened by JP

Click to See Complete Forum and Search --> : Help designing a database for my C program


nothing
08-14-2005, 10:21 AM
Hello,
I wrote a very simple C program for managing clients and their buying and paying activities for an imaginary clothing store. For this version, I used simple text files but now I'd like to use MySQL. Let me explain how my program works first...

You enter a phone number and if the text file for that phone number is found, you have the following options:
Client is buying
Client is paying

Check total debit of the client
Show all transactions made by the client
Go back

If the text file for the phone number is not found, you have to register the new client and then you'll have the same options. Here's how the text file for one of the clients looks like after a couple of transactions:

Max Payne
123 Main Street
Springfield
NC
12345 (this is the zip code)
123456789 (this is the phone number)
#
bought sunday, 8/7/2005 534.00
534.00
paid monday, 8/8/2005 100.00
434.00
paid tuesday, 8/9/2005 100.00
334.00

The lines consist of the kind of the transaction, the date, and the total spent. The number on the line by itself is the total current debit of the client.

I already have an idea for the database design so I'll post it here and I hope you guys can give me some suggestions. Thanks a lot!

ScaryBinary
08-14-2005, 10:19 PM
You should not use the client's phone number as your primary key....if a client ever changes his phone number, you'll have to change it in the client table, plus once for every transaction the client has ever made in the other table. That would be a nightmare.

Instead, use an "AUTO INCREMENT" number which MySQL will automatically create each time you add a new client. Use this number in the other table as well. That way if a client's phone number changes, you'll only have to change it once.

In your client table, some example data might be:
ID Phone Name
-- -------- ----
1 555-1234 Hank
2 555-5678 Frank
3 555-9101 Tank

Then in the table storing the transactions...
ID Transaction Type
-- ----------------
1 bought
1 paid
1 paid
2 bought
2 paid

That data indicates that Hank bought something and paid it off in two payments, and Frank bought something and paid it off in full later. Now if Hank changed his phone number, you only have to change it in the first table. If you used the phone number as the primary key, you'd have to update it in two different tables, in a total of four different spots. Of course, this really isn't too much trouble with an UPDATE query in MySQL, but it's still a chance to forget to update one table.