Re: Data type to use for primary key

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Alexandre,

> What is the common approach? Should I use directly the product_code as
> my ID, or use a sequantial number for speed? (I did the same for the
> company_id, this is a 'serial' and not the shor name of the customer.
> I just don't know what is usually done.

Don't use SERIAL just because it's there.    Ideally, you *want* to use the 
product_code if you can.   It's your natural key and a natural key is always 
superior to a surrogate key all other things being equal.   

Unfortunately, all other things are NOT equal.    Here's the reasons why you'd 
use a surrogate key (i.e. SERIAL):

1) because the product code is a large text string  (i.e. > 10bytes) and you 
will have many millions of records, so having it as an FK in other tables 
will add significantly to the footprint of the database;

2) because product codes get blanket changes frequently, where thousands of 
them pet re-mapped to new codes, and the ON CASCADE UPDATE slow performance 
will kill your database;

3) Because every other table in the database has a SERIAL key and consistency 
reduces errors;

4) or because your interface programmers get annoyed with using different 
types of keys for different tables and multicolumn keys.

If none of the above is true (and I've had it not be, in some tables and some 
databases) then you want to stick with your "natural key", the product_code.


-- 
--Josh

Josh Berkus
Aglio Database Solutions
San Francisco


[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux