一对多表设置

发布于 2024-09-19 03:10:06 字数 404 浏览 3 评论 0原文

我有一张表,我必须在其中将主键组相互关联。除了这些团体的存在之外,没有任何关于这些团体的信息。具体来说,我将多组书籍的版本存储在一起,这样我就可以知道一组特定的书籍是彼此的版本。

我目前有一个设置,其中有一个 Edition_Group_ISBN 列,其中任意选择一个 ISBN 将一组版本分组在一起。

解决此问题的典型方法是使用一个单独的表,例如 Book_Editions,其中我将有一个自动递增整数主键,例如将 ISBN 连接在一起的“Edition_Group_ID”。有人告诉我这种方法更好。

然而,实现该系统的问题涉及数据的加载。如何动态加载版本组?一种解决方案可能是锁定表并在自动增量中的下一个 ID 上执行事务。不过,我想这会比我目前的方法更慢、更麻烦。

鉴于在该系统下插入数据的困难,解决该问题的最佳系统是什么?

I have a table where I have to relate groups of primary keys to one another. There is no information about these groups other than that they exist. Specifically, I am storing groups of Editions of Books together, so I can know that a certain set of books are editions of each other.

I currently have a setup where I have an Edition_Group_ISBN column, where one of the ISBN's is arbitrarily chosen to group a set of Editions together.

The typical approach for this problem is to have a separate table like Book_Editions, where I would have an autoincrementing integer primary key like "Edition_Group_ID" linking ISBNs together. I have been told this method is preferable.

However, the problem with implementing that system relates to the loading in of data. How am I to dynamically load in Edition Groups? One solution might be to lock the table and do a transaction on the next ID in the autoincrement. I imagine this would be slower and more cumbersome than my current method, though.

Given the difficulty of inserting data under that system, what is the optimal system to address this problem?

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

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

发布评论

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

评论(1

时光病人 2024-09-26 03:10:06

您可以通过在版本组表中使用 ISBN 表外键来加载版本组,然后使用 ISBN 图书表的主键和版本组表的外键在查询中将两个表内连接在一起在加入中。

ISBN Table
    ISBN_ID                                    // Auto-incrementing Primary Key
    ISBN
    Book_Title
    .etc

EDITIONS Table
    Edition_ID
    FIRST_EDITION_ISBN_ID
    ASSOCIATED_ISBN_ID                         // Foreign Key to ISBN table

大多数数据库系统都有一种方法返回新插入记录的主键 ID,因此:

NEW_ID = INSERT INTO ISBN (ISBN, BOOK_TITLE) 
         VALUES (12345678, "The Frog Prince"); 
         SELECT SCOPE_IDENTITY();              // Returns new ID from ISBN table.

INSERT INTO EDITIONS (FIRST_EDITION_ISBN_ID, ASSOCIATED_ISBN_ID)
    VALUES (12345678, NEW_ID);

You load in Edition Groups by having an ISBN table foreign key in your Edition Groups table, and then inner-joining your two tables together in your query, using the Primary Key of your ISBN Books table, and the Foreign Key of your Edition Groups table in the join.

ISBN Table
    ISBN_ID                                    // Auto-incrementing Primary Key
    ISBN
    Book_Title
    .etc

EDITIONS Table
    Edition_ID
    FIRST_EDITION_ISBN_ID
    ASSOCIATED_ISBN_ID                         // Foreign Key to ISBN table

Most database systems have a way to return the Primary Key ID of a newly inserted record, so:

NEW_ID = INSERT INTO ISBN (ISBN, BOOK_TITLE) 
         VALUES (12345678, "The Frog Prince"); 
         SELECT SCOPE_IDENTITY();              // Returns new ID from ISBN table.

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