Results 1 to 10 of 10

Thread: Primary Key

  1. #1
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Primary Key

    When I see examples of creating tables in MySQL, i often see Primary Key used. So i had no idea what it was and researced it...but found nothing out.
    For example:
    Code:
    $sql = "create table IF NOT EXISTS Forum
    (
    UserName varchar(15),
    Password varchar(15),
    ID int,
    Primary Key (ID)
    )"
    then that is queried to the MySQL database. My question is, what does that Primary Key (ID) thing do? All i know is that it increases the ID value each time the Insert query is called. I think. Can someone please explain it for me?

    Thanks in advance.

    P.S. - I'm not sure if i wrote that code right either.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    I'm pretty sure the point is that it just gives each a unique value.
    By chance, you might have two entries that have, for example, the same password, or maybe even the same username (if that's allowed), and you need to have a sure way to distinguish between the different entries. If each has a unique ID, then that will do it for you.
    You don't always need one, but they are helpful in lots of cases, and rarely hurt.
    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

  3. #3
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, but my code above will just increase ID with each user entry? so that it will make a table in my MySQL database that looks sort of like the following:

    ID------UserName
    1 Bob
    2 Rich
    3 Sue
    4 Mike

    is that the basic idea of it?

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Sure. I think that will do fine.
    I'm not really familiar with the term "primary key", but I believe that's the point of it... I've just seen it in passing.
    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

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The primary key won't auto-increment by default. You need to specify
    Code:
    ID int auto_increment
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Right, that's the crucial piece... sorry I forgot to mention it
    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

  7. #7
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Wait, so in my code do i need to put this?

    ID int,
    Primary Key (ID) auto_increment

    or do i just need to put

    ID int auto_increment

    ?

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    ID int auto_increment,
    primary key(ID)
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Allright, thanks.

    So then, if the auto_increment increases ID with each Insert query, what does Primary Key do?

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Insofar as I know (not a big database guy) just marks it as being the primary, unique, identifying column. The SQL implementation may perform some optimisations to how it runs based on that -- I'm guessing it would usually assign a primary key column itself if none existed.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •