What is primary key in MYSQL database for?

Discussion in 'PHP' started by NewTier, Jul 30, 2009.

  1. #1
    I was wondering, what is the primary key in MYSQL database for?
     
    NewTier, Jul 30, 2009 IP
  2. ptalent

    ptalent Peon

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It uniquely identifies each row and cannot be repeated.

    It is usually a column called "id" or "[table]id" or can be a multiple columns combined like make, model, year can be combined to make a unique entity.

    Having a table in mysql without a primary key makes it a nightmare to sort out as you have to pass so many variables to the select (usually via a post or get) and then you have to check them all to make sure theyre safe to use.

    If you have just one primary key column then it shortens the whole process and can turn a select query from this: "select * from table where aaa = 'zzz' and bbb = 'xxx' and ccc = 'vvv'" to "select * from table where id = 1"
     
    ptalent, Jul 30, 2009 IP
    NewTier likes this.