获取 MySQL 中的底层主键 (InnoDB)

发布于 2024-11-01 10:42:28 字数 320 浏览 1 评论 0原文

据我了解,当我创建一个没有主键的表时,MySQL 会创建一种内部使用的底层主键。

我正在使用一个没有主键的表,但如果我能够以某种方式访问​​该值(假设它确实存在并且可检索),那么它将对我的应用程序非常有用。

所以,我想知道我是否正确地相信这样的值存在于某处,以及是否有可能获得该值。

编辑:为了明确起见,对于我的应用程序来说,该表具有递增的 int 属性将非常有用。不幸的是,它并没有以这种方式实现。所以,我有点想抓住救命稻草来寻找解决方案。我想做的是选择表中的每第 n 行(n 次更改)。所以,正如你所看到的,如果有这个密钥,这将非常简单。

It is my understanding that when I make a table without a primary key that MySQL creates a sort of underlying primary key that it uses internally.

I am working with a table that does not have a primary key, but it would be very useful for my application if I could somehow access this value, assuming it does in fact exist and is retrievable.

So, I am wanting to know if I am correct in believing that such a value exists somewhere and also if it is possible to get that value.

Edit: just to make it clear, it would be very useful for my application for this table to have an incrementing int attribute. Unfortunately, it was not implemented that way. So, I am sort of grasping at straws to find a solution. What I am trying to do is select every nth row in the table (n changes). So, as you can see if there was this key, this would be very simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-11-08 10:42:28

如果表没有主键,则无法指定其中的特定行,因为无法唯一标识项目。即使您使用为每列指定特定值的查询,但仍然不能确定只返回单行,因为没有主键,没有什么可以防止重复行。

然而,主键只是一个唯一索引。如果表的一个或多个列上有唯一索引,并且这些列不接受 NULL,那么这就是该表除名称之外的所有列的主键。

如果您的表没有唯一的列,那么您将无事可做。您必须使一列或列组合(对于复合键)唯一,或者添加一列作为表的主键。幸运的是,向 MySQL 表添加列相对容易,只需向现有表添加主键自动增量列即可。

If a table has no primary key then there's no way of specifying a specific row within it because there is no way to uniquely identify an item. Even if you use a query that specifies a specific value for every column that still wouldn't be certain to only return a single row as without a primary key there's nothing to prevent duplicate rows.

However, a primary key is simply a unique index. If a table has a unique index on one or more of its columns and those columns don't accept NULLs then this is the primary key for the table in all but name.

If you table has no unique columns then you've got nothing to go on. You'll have to either make one column or combination of columns (for a composite key) unique, or add a column that serves as the primary key for the table. Fortunately it's relatively easy to add columns to a MySQL table, just add a primary key autoincrement column to the existing table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文