views:

437

answers:

10

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?

+2  A: 

Normalization would say "yes".

How many applications are affected by this change? That would affect the decision.

duffymo
Just one application
Joe
excellent. how extension would the change to a single table be? is it written in a language that you know well? can you change the app? how hard to regression test?
duffymo
+2  A: 

Definitely one table. And I wouldn't call it supplier since this does not reflect the meaning of the table. Something like busibess_partner or something better than that might be more appropriate. Instead of purchase_id and seller_id, then be more generic like business_partner_id, and yes, add a field to distinguish.

Khnle
+7  A: 

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.

J.J.
+4  A: 

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.

BradC
A: 

You must take into consideration the number of records on both tables. if they are to big it could have a big inpact on queries that have multiple joins to customers and suppliers.

Example: Who sold computers to us and to whom did we sell them to.

Sergio
how big is "too big" in your opinion?
duffymo
I dont have a number for it, it depends on how fast your server can be and how many joins you can have to that table on a single query. Get the most complex query you can find and benchmark it. If it runs under 1sec on the developer machine it should be fine.
Sergio
Just be carefull to add at least 10 times the data you have now, because that table will grow.
Sergio
+2  A: 

Not one table. They are different entities that have a similar structure. There's nothing to be gained by consolidating them. (Nothing lost, either, except lucidity; but that's critical IMHO).

"Normalization" doesn't include looking for tables with similar schemas, and merging them.

le dorfier
+1  A: 

A database is always a limited model of your business objective. If it doesn't make sense for you business, ignore those who say you should add complexity to your data model by creating a new companies table (though you probably already have something similar). If you really want to get into the "perfect model" game, just start abstracting everything away into an "entities" table and pretty soon you will have a completely unmanageable database.

Normalization would dictate that you NOT combine the two fields, unless the foreign keys actually point to the same table. A key rule to keep in mind is that each column in a table should only mean one thing. Adding a second field that explains what the first field means breaks this rule.

If your queries are getting to be a mess because you are always joining the two tables, you could create a view.

Also, the number of records in the table is almost completely irrelevant. Always optimize for performance after you have the system in place. If it killing your application to have all the records in one table, set a clustered index on a column that partitions your table in a meaningful way.

Abtin Forouzandeh
A: 

From a completely different point of view. I tend to consider logic over technology. To me the decision is not whether the data is similar in shape or fields, but whether it makes sense mixing them. That is as much to say that whether the technical answer might be normalize, my answer would be: does it make sense to you (business logic) to have both together?

Another answer talks about merging both and changing naming conventions. To me that is a logic decision: you are saying that you don't work with buyers and sellers, but with business partners. If that is your case, then do it.

You might also consider what your use of the tables would be. If they are of one unique logic type (business partner) you will surely have queries that need to access both buyers and sellers. Else, if all your queries are separate, that might be an indication that they are not the same, and should not be held together. Pushing them together will imply a lot of extra checks and cpu time spent differing from what were separate entities.

There is a long used metaphor about interfaces that might apply here. Just because a fire gun and a camera both shoot, that does not mean they share an interface, unless you like playing Russian roulette.

David Rodríguez - dribeas
A: 

From a logical view, there seems to be no difference between the reported transactions, it is just a difference in who reports it to you. It should be a single table with SellerID, BuyerID, and (if you need it) ReporterID(s) (and perhaps additional transaction information).

This is how it should be. Now, how to make the transition? Making a script that uses the two old tables to fill a new table should be an easy exercise, but then you also need to change all the queries that use the information. This is likely a lot of work, and might not be worth the effort.

Svante
A: 

Since none of the experts reporting in are willing to answer your question, the simple answer is: query1 UNION query2

EX. SELECT * FROM table1 UNION SELECT * FROM table2 assuming table1 and table2 have the same structure/heading titles

should have read the question better...