MySQL:MySQL 表中的每条记录是否有类似内部记录标识符的东西?
我正在构建一个使用 MySQL 作为存储的电子表格应用程序,我需要识别正在客户端更新的记录以便保存更改。
有没有一种方法,例如某种“内部记录标识符”(数据库引擎本身使用的内部记录标识符)来唯一标识记录,以便我能够更新正确的记录?
当然,SELECT 查询可用于识别记录,包括表中的所有字段,但显然,在大多数情况下,它的缺点是返回多条记录。
重要提示:电子表格应用程序旨在处理任何表,即使是设计非常糟糕且没有任何键的表,因此诸如“定义具有唯一索引的字段并使用它”之类的解决方案不是一个选项,表结构可能非常可变并且一定无关紧要。
非常感谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK 不存在这样的唯一内部标识符(例如,简单的行 ID)。
您可能也许能够在不进行任何排序的情况下运行
SELECT
,然后使用LIMIT
获取第n行>。 mySQL Guru 需要确认在什么条件下可以可靠且安全地使用。也许永远不会。尝试使用 phpMyAdmin,mySQL 的 Web 前端。它旨在处理设计糟糕的没有键的表。如果我没记错的话,它会使用在这种情况下可以获取的所有列:
等等。
当然,这也不完全是重复安全的。
我想到的唯一想法是在运行时添加一个键列,并在应用程序完成后将其删除。这是一个令人起鸡皮疙瘩的想法,但也许总比没有好。
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 aLIMIT
. 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:
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 有“自动增量”数字列,您可以添加这些列,甚至可以将其定义为主键,这将为您提供由数据库自动生成的唯一记录 ID。
select LAST_INSERT_ID()
示例查询刚刚插入的最后一条记录 id您可以使用mysql 官方文档中的 此处
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
据我所知,MySQL 缺乏 Oracle 中所见的隐式 ROWID 功能(并且存在于具有自己语法的其他引擎中)。您必须创建自己的
AUTO_INCRMENT
字段。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.