views:

68

answers:

3

I'm building a spreadsheet app using MySQL as storage, I need to identify records that are being updated client-side in order to save the changes.

Is there a way, such as some kind of "internal record identifier" (internal as in used by the database engine itself), to uniquely identify records, so that I'll be able to update the correct one?

Certainly, a SELECT query can be used to identify the record, including all the fields in the table, but obviously that has the downside of returning multiple records in most situations.

IMPORTANT: the spreadsheet app aims to work on ANY table, even ones tremendously poorly designed, without any keys, so solutions such as "define a field with an UNIQUE index and work with that" are not an option, table structure may be extremely variable and must not matter.

Many thanks.

A: 

MySQL has "auto-increment" numeric columns that you can add and even define as a primary key, that would give you a unique record id automatically generated by the database. You can query the last record id you just inserted with select LAST_INSERT_ID()

example from mysql's official documentation here

ggiroux
Good idea, had it, does not comply with the specs: no control over table structure, cannot add fields, keys.
Webmaster
A: 

To my knowledge, MySQL lacks the implicit ROWID feature as seen in Oracle (and exists in other engines with their own syntax). You'll have to create your own AUTO_INCREMENT field.

Adam Backstrom
Does not comply with the specs: no control over table structure, cannot add fields, keys.That ROWID feature you've mentioned is probably what I'm trying to talk about. Thanks.
Webmaster
+1  A: 

AFAIK no such unique internal identifier (say, a simple row ID) exists.

You may maybe be able to run a SELECT without any sorting and then get the n-th row using a LIMIT. Under what conditions that is reliable and safe to use, a mySQL Guru would need to confirm. It probably never is.

Try playing around with phpMyAdmin, the web frontend to mySQL. It is designed to deal with badly designed tables without keys. If I remember correctly, it uses all columns it can get hold of in such cases:

UPDATE xyz set a = b WHERE 'fieldname'  = 'value' 
                       AND 'fieldname2' = 'value2' 
                       AND 'fieldname3' = 'value3' 
                       LIMIT 0,1;

and so on.

That isn't entirely duplicate-safe either, of course.

The only idea that comes to my mind is to add a key column at runtime, and to remove it when your app is done. It's a goose-bump-inducing idea, but maybe better than nothing.

Pekka
I believe phpMyAdmin will also add a LIMIT 1 to DELETE and UPDATE clauses where no primary key is available.
Adam Backstrom
@Adam True. I added it to the example. This is probably safe to do as long as phpMyAdmin is the only instance accessing the database.
Pekka
Cheers. This is exactly the solution I employ at the moment and it's definitely far from duplicate-safe.Whoa, I felt the goose-bumps to. The later actually a bril idea, goose-bump inducing, indeed, but even if I were to undertake the mission, I cannot alter table structure. This would have to be played in a whole new temporary table.Thanks.
Webmaster