需要建议来获取 mysql 上两个或多个表的唯一索引键

发布于 2024-10-10 06:47:15 字数 180 浏览 0 评论 0原文

通常,我使用 auto_increment 属性来获取单个表上每一行的唯一键/id,以用作主索引。现在我有两个表,不幸的是 auto_increment 无法使用另一个表上的最后一个 id/key 创建新键以保持两个表中所有键/id 的唯一性。如果我在两个索引列上使用 auto_increment,则可以保证有两个相同的 ID!有简单的方法吗?

Usually, I use auto_increment attribute to get an unique key/id for each row on a single table to use as a primary index. Now I have two tables and unfortunately auto_increment can't create a new key using the last id/key on another table to keep all the keys/id unique in both. If I'll use auto_increment on both index columns, the possibility to have two identical ID is assured! There is a easy way to do it?

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

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

发布评论

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

评论(2

筑梦 2024-10-17 06:47:15

至少从关系数据库设计的角度来看,您的要求似乎不合理。您应该创建一个具有公共 auto_increment 字段的单独表,并使用外键从其他两个表中指向该字段。

At least from a perspective of relational database design, your requirement seems unsound. You ought to create a separate table with the common auto_increment field and point to that from the other two tables with foreign keys.

自此以后,行同陌路 2024-10-17 06:47:15

如果您有:

  • 表 A 的 ID 为 1,2,3。
  • 表 B 的 ID 为 1、2、3。

那么您已经拥有以下(虚拟)唯一标识符:

  • A1、A2、A3、B1、B2、B3

您无需对数据库进行任何更改。这纯粹是一个演示问题。

If you have:

  • table A with ids 1,2,3.
  • table B with ids 1,2,3.

Then you already have the following (virtual) unique identifiers:

  • A1, A2, A3, B1, B2, B3

You don't need to make any changes to your database. This is purely a presentation issue.

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