Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider a table in which a column represents a monetary value in some cases and a link to the value of another record in other cases so that we have the following:

Id Product Composite
1  Apple   False
2  Orange  False
3  Pear    True

Product  Date        Price 
1        2010-01-01  0.45
2        2010-01-01  0.50
3        2010-01-01  1.00
1        2010-01-02  0.46
2        2010-01-02  0.49
3        2010-01-02  2.00

The tables above represent the fact that the price for an Apple and Orange are given on each date by the corresponding Price column in Table 2 so that the price of an Apple on 2010-01-01 is 0.45 and the price of an Orange is 0.50 on that same date, however, the price for a Pear on 2010-01-01 is 0.45 and the price for a pear is 0.49 the following day by allowing the Pear's price to be determined by the price of an Apple on the first day and the price of an Orange on the second day.

Please indicate the pros and cons of this approach?

share|improve this question
Would you care to write the queries for each of those... tables? It might help you (and others) understand the idea you are asking about. – user40980 Jul 30 '13 at 3:17
The column you're trying to use for two purposes is not being used for two purposes at all... it's being used for a single purpose, namely holding prices. – Robert Harvey Jul 30 '13 at 3:52
Taking this approach you have just broken all the aggregate functions (AVG,SUM,MIN,MAX) in SQL. You have also made it absurdly complex to obtain a price list. – James Anderson Jul 30 '13 at 8:58
I had to read this several times to figure out that 1.00 was a record ID for Pear on 2010-01-01. That's horrible. – ThinkingMedia Aug 2 '13 at 3:41
up vote 16 down vote accepted

The approach is, frankly, horrible. As I understand it, you want the column 'Price' to be interpreted as a foreign key to Product.Id, and then "redirect" the query for Pear prices to a query for Apple prices or for Orange prices. This requires complicated logic in your SQL code for what should be a straightforward table look-up, and since data base schema don't have comment fields, it is very easy for someone else to overlook this and simply assume that the price is $1.00.

But quite apart from that: you don't even save anything - neither time nor storage. Instead of abusing the Price column as a foreign key, you could just save the actual price in that column at no extra cost! You would end up with a table that has duplicated values, but that is normal and expected in a large data set.

So overall, I can see only cons and no pros here.

share|improve this answer
Thanks Kilian. I was hoping somebody would say this! Although the problem is that the price of a pear really is a composite. If the price of an Apple changes for the first day then the price of the Pear needs to reflect this without a second update query, hence the requirement for a relationship #thingsclientswant – ofraski Jul 30 '13 at 7:08
I wouldn't frame the price of a pear as a composite. The price is what the price is. The determination of that price is based off of the apple, but the database really doesn't need to know about that. – Tombatron Jul 30 '13 at 12:37

I think the word "purpose" phrases the question on a level that's unnecessarily abstract.

On a simpler level, we can say that all the values in a single column should be drawn from the same domain, and have the same semantics. So the same value in the column in two different rows ought to mean the same thing.

In particular, if the values in a column are foreign keys, then they should all be foreign keys to the same table. I've seen the opposite done, where references to multiple tables are stored in a single column. The result is invariably a mess. A companion column is always needed to disambiguate, and this leads to one of two problems.

Whenever the disambiguation is done correctly, the code is more complicated than it needs to be, and also runs more slowly than it should. And of course, if the programmer forgets to disambiguate correctly on a join, the wrong stuff gets joined together. I've seen both cases.

Foreign keys is the most obvious case, but even in other cases the need to disambiguate is just an awkward consequence of introducing ambiguity in the first place. If you keep different data in different columns, you don't have to disambiguate.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.