1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

MySQL Unique Row Identifier

Discussion in 'Databases' started by flamy, Oct 22, 2009.

  1. #1
    If I want to UPDATE a record in a MySQL database which doesn't have a unique auto increment key or id in the first column, the only way I can update the row is based on non unique information. So if I do an update on a name i'd run the risk of overwriting all other instances.

    Is there any way of selecting a unique row id that I can update against? Without having to alter the database structure and adding another column for a unique id?
     
    flamy, Oct 22, 2009 IP
  2. flamy

    flamy Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are functions such as rowid, rownum in other SQL databases but not MySQL.

    The above will assign a row id to each row in a select statement, which I suppose could be used as a more reliable identifier but it's still open to problems if the rows were to change or inserted.

    Any suggestions?
     
    flamy, Oct 22, 2009 IP
  3. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    If there's no unique identifier, how do you know its unique? If you can't verbally define how your data is unique you have no hope of having a database doing so.


    'John', 'Smith', '1234 Main Street', 'Plano', 'TX'
    'John', 'Smith', '1234 Main Street', 'Plano', 'TX'
    'John', 'Smith', '1234 Main Street', 'Plano', 'TX'

    Without using their position in relation to each other as I just listed them (i.e. middle one, 1st one), tell me how each one is unique. You can't--the data is not unique and in an update query they will all get updated.
     
    plog, Oct 22, 2009 IP
  4. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #4
    You need to create an auto increment column to work on. If your datra is not unique then every time you do a select, the rows are not guaranteed to be returned in the same sequence.

    You definitely need a unique column to work on.
     
    rayqsl, Oct 22, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Is there an insert datetime or timestamp field? You could use that as a unique key if it's in the database. Otherwise you need to alter the table and add an autoincrement column.
     
    jestep, Oct 22, 2009 IP
  6. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Whenever there is no unique identifier in MySql, combining all fields makes identifier for one row, however, there can be duplicate rows as well, but if you look at it logically, what is the use of having duplicate rows and updating only one of them in such cases? NO Use I would say. SO ultimate way to update a row in such cases is to specify each and every field in table for update query. BUT if you want peace of mind, add an auto incremented column to table.
     
    mastermunj, Oct 23, 2009 IP
  7. flamy

    flamy Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Unfortunatley I don't have the option of inserting an auto increment field.

    What I've done is check using _rowid if a primary key exists in the table i am updating, if so update using this. Otherwise specify every field for update query and LIMIT 1.

    Thanks v much for the replies.
     
    flamy, Oct 24, 2009 IP