Re: Surrogate primary key plus unique constraint vs. natural primary key: data integrity?

From: Jan Hidders <hidders_at_gmail.com>
Date: Sat, 16 Mar 2013 12:27:46 +0100
Message-ID: <51445730$0$26880$e4fe514c_at_dreader37.news.xs4all.nl>


On 2013-03-15 12:01:45 +0000, robur.6_at_gmail.com said:

> On Friday, March 15, 2013 2:03:43 AM UTC+2, Jan Hidders wrote:

>>
>>> - Performance (?)

>>
>>
>> Doubtful in my experience. Programmers tend to think that since the>
>> join condition becomes simpeler the joins become faster, but that is>
>> usually not the bottle neck. There can be a observable gain if the
>> size> > of many records decreases, which might typically happen for>
>> cross-reference tables representing relationships. But significant>
>> gains are rare in my experience.
>>
> 
> That’s why I put a "?".
> 
>>> - You need more joins (performance?)

>>
>> Really? Why you think that? One typically would have the same number
>> of> tables in the schema, so why would we need more joins?
>>
> 
> Let’s consider the relation reviews{reviewer, review-language, book, 
> rating} and tuple {34, 14, 66734, 4}. Can you tell anything about it? 
> Now replace with {34, 'EN', 'ISBN-XXXXX’, 'verry good'}. Do we need any 
> join if we want to search for a specific book, review language or 
> rating?

Ok. Fair enough. If the query only needs the parts of the entity that is in the foreign key that was replaced with the surrogate key, then, yes, you need an extra join.

> Also if you have something like D -> C -> B -> A, each relation having 
> it’s own surrogate, then you cannot join A and D directly (pretty 
> stupid design, but found in practice).

Hmm, yeah, but that's just stupid design and not really inherent in the use of surrogate keys. You could argue that the mistake cannot even be made if you wouldn't have had surrogate keys.

>>> - You lose the simplicity and power of overlapping composite foreign> > 
>>> keys constraints (discussed in this thread).

>>
>> Using surrogate keys does not imply that you cannot use composite foreign keys.
>>
> 
> The problem may appear only if you replace a natural composite key by a 
> surrogate.

As in "it's because you are using them you might be misled into thinking you don't need composite keys"? Maybe, but what I would like to know is if they cause some inherent problems that cannot be avoided, even when modelling carefully.

  • Jan Hidders
Received on Sat Mar 16 2013 - 12:27:46 CET

Original text of this message