Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: is there something like an internal record identifier for every record in a MySQL table?

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.

like image 874
Webmaster Avatar asked Nov 06 '22 16:11

Webmaster


1 Answers

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.

like image 195
Pekka Avatar answered Nov 30 '22 14:11

Pekka