+ Reply to Thread
Results 1 to 6 of 6

Thread: string or int as primary key?

  1. #1
    ganjasensation0098's Avatar
    ganjasensation0098 is offline x10Hosting Member ganjasensation0098 is an unknown quantity at this point
    Join Date
    Sep 2010
    Posts
    36

    string or int as primary key?

    Which is the better data type in creating primary key of a table?
    Last edited by ganjasensation0098; 11-26-2010 at 01:29 AM.
    HERB is the healing of a nation. ALCOHOL is the destruction.

  2. #2
    mandy0 is offline x10Hosting Member mandy0 is an unknown quantity at this point
    Join Date
    Feb 2009
    Location
    Mumbai->India
    Posts
    32

    Re: string or int as primary key?

    It depends on usage. But most of the times INT key is preferred. usually you will find something like ID to show the primary key.
    Well it depends on the usage .. what kind of requirements you are going to have.. accordingly decide. But INT is most preferred.

  3. #3
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: string or int as primary key?

    I can't think of any situation in which a string primary key would be necessary, as the aim of the key is to provide a unique way of finding and linking rows across tables; whilst a string primary key would work, an auto-imcrement int key will not only be more efficient, but you'll also find that it is faster to search. Having a string primary key will mean all searches have to go through as a string, whilst with a integer primary key, the ones that don't need the string will benefit from not having to use it.
    Of course, if you need to have a key on a string, have it as a unique index alongside your primary key rather than using it as your actual primary key.

  4. #4
    mandy0 is offline x10Hosting Member mandy0 is an unknown quantity at this point
    Join Date
    Feb 2009
    Location
    Mumbai->India
    Posts
    32

    Re: string or int as primary key?

    @lemon-tree
    you can have EMAIL as Primary key also ;) As well as it's possible you might go for Unique key ;) :D nvm ) but very very rearly string would be used ^_^

  5. #5
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: string or int as primary key?

    you can have EMAIL as Primary key also
    I certainly wouldn't, I would create a table like this instead:
    Code:
    USER_ID INT() - PRIMARY KEY
    EMAIL VARCHAR() - UNIQUE KEY
    etc for other user info
    This way, you can create more simple foreign keys between tables and cross-reference tables.
    Of course, it's always down to the programmer's discretion as to which method they chose. All methods are correct, but some methods are more correct than others.
    Last edited by lemon-tree; 11-26-2010 at 11:20 AM.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: string or int as primary key?

    Expanding on lemon-tree's answers: using a string for a primary key may impact both space and time performance. Numeric types are smaller, so indexes and foreign keys on numbers are smaller than those on string types. In particular, indexes and foreign keys will duplicate values; better to duplicate small values rather than larger. Smaller sizes reduces page-outs. Numeric comparison is faster than string comparison.

    See also:
    Last edited by misson; 11-26-2010 at 08:36 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. T string
    By fileupload in forum Programming Help
    Replies: 4
    Last Post: 05-08-2010, 04:20 PM
  2. extracting from a string
    By garrensilverwing in forum Programming Help
    Replies: 28
    Last Post: 06-09-2009, 07:00 PM
  3. PHP found in string help
    By Chris S in forum Programming Help
    Replies: 9
    Last Post: 04-06-2009, 12:08 PM
  4. Split string
    By radofeya in forum Programming Help
    Replies: 3
    Last Post: 10-02-2008, 02:35 AM
  5. MB String Support?
    By civilr in forum Free Hosting
    Replies: 5
    Last Post: 06-19-2007, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers