Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I combine two similar tables into one?

In my database I currently have two tables that are almost identical except for one field.

For a quick explanation, with my project, each year businesses submit to me a list of suppliers that they sale to, and also purchase things from. Since this is done on an annual basis, I have a table called sales and one called purchases.

So in the sales table, I would have the fields like: BusinessID, year, PurchaserID, etc. And the complete opposite would be in the purchases table, except that there would be a SellerID.

So basically both tables are exactly the same field wise except for the PurchaserID/SellerID. I inherited this system, so I did not design the DB this way. I'm debating combing the two tables into one table called suppliers and just adding a type field to distinguish between whether they are selling to, or purchasing from.

Does this sound like a good idea? Is there something I'm missing in regards to why this wouldn't be a good idea?

like image 498
Joe Avatar asked Dec 03 '22 16:12

Joe


2 Answers

Do what works for you.

The textbook answer is normalize. If you normalized you would probably have 2 tables, one with both your buyers and sellers as companies. And a transactions table telling who bought what from who.

like image 147
J.J. Avatar answered Dec 11 '22 16:12

J.J.


If it ain't broke, don't fix it. Leave them separate.

Since the system is already built, I would only consider this if you find yourself doing a lot of queries across the two tables, like big nasty UNION queries. Joining the two tables in one makes queries like "show me all sellers or purchasers who sold/bought between these dates..." much easier.

But it sounds like these two groups are treated very differently from the business rule perspective, so its probably not worth the trouble to make application changes at this point. (Every query would have to have a "WHERE Type = 1" or something like that).

If you'd have asked this during the db design phase, my answer might be different.

like image 45
BradC Avatar answered Dec 11 '22 16:12

BradC