Results 1 to 5 of 5

Thread: Should Primary Keys be self-incrementing?

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Should Primary Keys be self-incrementing?

    It seems that most things I have read about mySQL recommend having a PRIMARY KEY that is an autonumber column in most tables (?).

    I have a table in which the invoice number is unique and I want to make it my primary key and not have an autonumber column. Is there any downside to ditching the autonumber column? Thanks.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    AUTOINCREMENT has the advantage of always being a unique value without making you figure out how to generate/assign it. If that's not a problem for you, then don't worry about it. Use your invoice number.

  3. The Following User Says Thank You to traq For This Useful Post:

    kuau (07-04-2011)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Thanks for putting my mind at ease. Is there any difference in terms of speed or priority or anything between a primary key field and a regular field defined as a unique index? These are things I have always wondered about.

  5. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    yes, but I'm not the one to ask about those distinctions

    mysql uses keys (primary, unique, etc.) to create indices, which help with efficiency when searching the table, but I don't know specifically how that works.

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Generally, unless I'm sure I won't need it, I always use a primary key auto-incremented. It guarantees that there will be a unique value for each entry (even if the rest of the information is not unique).

    There's no reason you need to use that as part of your system though. You can use another column (such as your invoice number), and the other additionally.

    If you have another value (invoice number?) that is also guaranteed to be unique, then there's no reason not to use that.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •