Numerics Or Varchars

  • Pls can anybody suggest me which data type is best suitable for primary key, joins

    Numeric or Varchar.

    none


    none

  • Obviously dependant upon the situation but it is usual to define the primary keys as integer identity fields where the database automatically assigns an incrementing integer to the column. For performance reasons you should try to keep the size (in bytes) of the field to a minimum, so tinyints, smallints, ints and bigints are defined as 1,2,4 and 8 bytes respectively with a varchar(n) field being a maximum of n bytes.

    Regards,

    Andy Jones

    andyj93@hotmail.com

    .

  • Use to be that numeric was the best way to go by the way it was evaluated by the processor but I remeber someone pointing out in another thread things have changed where this is not the case anymore. The best item for a PK is whatever will make the data unique without nulls even if it is a combination of columns. Identity fields should be saved for data representations in foreing keys (smaller storage to link the two tables) or for tables where all values can repeat but you need to identify what records is what. The only downside is IDENTITY fields are int based and the version of int you use is finite in how large it can be, most don't tend to hit this limit but some do.

    In a nutshell boils down to your table and how to uniquely identify records.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

  • At present I almost use an identity col with an integer OR a guid. Guids are my favorite when I need to handle master/detail relationships on the client. I agree with Antares that you should go with the key that makes sense. As long as its one column, I think its usable.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply