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

梦想与她 提交于 2019-11-28 12:34:09

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!