This database needs a Primary Key...

Discussion in 'MySQL' started by sawz, Mar 22, 2007.

  1. #1
    but there are nothing unique in each row.
    is there a way to add a filed to a database with incremental numbers to be able to create a Primary Key?

    i have a zip code database with over 60,000 entries with no Primary Key.
    i need to add a unique field to each row. is there anything out there that will do this for me?
     
    sawz, Mar 22, 2007 IP
  2. Lavee

    Lavee Well-Known Member

    Messages:
    234
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    If it is a zip code database , then you can make zipcode as yr primary key, since that should be unique

    Thanks

    Lavee
     
    Lavee, Mar 22, 2007 IP
    sawz likes this.
  3. sawz

    sawz Prominent Member

    Messages:
    8,225
    Likes Received:
    808
    Best Answers:
    0
    Trophy Points:
    360
    #3
    obvious answer, i already thought of that.
     
    sawz, Mar 22, 2007 IP
  4. sawz

    sawz Prominent Member

    Messages:
    8,225
    Likes Received:
    808
    Best Answers:
    0
    Trophy Points:
    360
    #4
    this database has over 70,000 rows and i need to add an ID field to it.
    is there a way to do this without spending hours manually adding to it?
     
    sawz, Mar 23, 2007 IP
  5. bochgoch

    bochgoch Peon

    Messages:
    1,918
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Exact method depends on the database that you're using ... MYSQL / SQL Server etc. which is it?

    Also, why do you need a primary key, what will be its purpose? For a look-up table such as this, a primary key is not essential.
     
    bochgoch, Mar 23, 2007 IP
    sawz likes this.
  6. sawz

    sawz Prominent Member

    Messages:
    8,225
    Likes Received:
    808
    Best Answers:
    0
    Trophy Points:
    360
    #6
    i have a program that generates php scripts to display the database and it will not do what it does without the db having a Primary Key. thats the ony reason. mysql database.
     
    sawz, Mar 23, 2007 IP
  7. Alam

    Alam Active Member

    Messages:
    316
    Likes Received:
    91
    Best Answers:
    0
    Trophy Points:
    68
    #7
    plz add a ID column with auto increament and assign it as primary key
     
    Alam, Mar 25, 2007 IP
    sawz likes this.
  8. sawz

    sawz Prominent Member

    Messages:
    8,225
    Likes Received:
    808
    Best Answers:
    0
    Trophy Points:
    360
    #8
    will that automatically assign a unique numder, ie 1, 2, 3, etc to each row?
     
    sawz, Mar 25, 2007 IP
  9. ruby

    ruby Well-Known Member

    Messages:
    1,854
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    125
    #9
    You can add a new column as an auto increment and when you do it will automatically assign all the values of the existing rows, of course.
     
    ruby, Mar 25, 2007 IP
    sawz likes this.
  10. sawz

    sawz Prominent Member

    Messages:
    8,225
    Likes Received:
    808
    Best Answers:
    0
    Trophy Points:
    360
    #10
    thanks, i like learning new stuff
     
    sawz, Mar 25, 2007 IP
  11. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #11
    Just create a table right next to it with exact same structure as the Zip Code table except it has an AutoIncrement datatype field. Then just select all from izp table, and insert to new table, lettting the ID be created by itself :)
     
    ccoonen, Mar 25, 2007 IP
    sawz likes this.
  12. sawz

    sawz Prominent Member

    Messages:
    8,225
    Likes Received:
    808
    Best Answers:
    0
    Trophy Points:
    360
    #12
    what i ended up doing is i imported the database, then i created an ID column with the proper variables and it was successful. each entry now has a unique ID number.

    thanks for everyones reply here, rep added to you.:D
     
    sawz, Mar 26, 2007 IP