数据库设计:存储完整的交易历史记录并快速查找当前状态?

发布于 2024-10-05 02:16:44 字数 875 浏览 0 评论 0原文

这是一个非常基本的问题,我是初学者:)

我正在使用 Django 和 构建图书馆数据库。 MySQL。该数据库具有书籍用户以及交易(用户取出或归还书籍)的概念。

我需要能够:

  • 快速获取特定书籍的完整交易列表 - 谁签出了该书籍以及何时
  • 获取书籍的当前状态。

我曾想过使用这样的表格,但是这样的设计有问题吗?

class Book(models.Model):
    name = models.CharField()
class User(models.Model):
    name = models.CharField()
TRANSACTION_TYPES = (
    ('I', 'Check in'),
    ('O', 'Check out'),
)
class Transaction(models.Model):
    book = models.ForeignKey(Book)
    user = models.ForeignKey(User)
    type = models.CharField(max_length=1, choices=TRANSACTION_CHOICES)
    date_added = models.DateTimeField(auto_now_add=True)

这对于获取完整的交易列表来说是可行的。但是,如果我想获取一本书的当前状态,我必须搜索该书的所有交易并找到最新的交易。

这明智吗?它是面向未来的吗?

我预计数据库不会变得那么大,但是是否有一种更有效的方法来存储书籍的当前状态,而不会对数据库进行反规范化?

非常感谢所有建议!

This is a pretty basic question, I'm a beginner :)

I'm building a library database using Django & MySQL. The database has the concepts of books and users, and of transactions, where a user takes out or returns a book.

I need to be able to:

  • get the full list of transactions for a particular book - who checked it out and when
  • get the current status of a book, quickly.

I had thought of using tables like this, but are there problems with this design?

class Book(models.Model):
    name = models.CharField()
class User(models.Model):
    name = models.CharField()
TRANSACTION_TYPES = (
    ('I', 'Check in'),
    ('O', 'Check out'),
)
class Transaction(models.Model):
    book = models.ForeignKey(Book)
    user = models.ForeignKey(User)
    type = models.CharField(max_length=1, choices=TRANSACTION_CHOICES)
    date_added = models.DateTimeField(auto_now_add=True)

This would work OK for getting the full list of transactions. But if I want to get the current status of a book, I'd have to search through all the book's transactions and find the most recent.

Is this sensible? Is it future-proof?

I don't foresee the database getting that big, but is there a more efficient way to store the current status of a book, without de-normalising the database?

All suggestions gratefully appreciated!

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

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

发布评论

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

评论(1

你的心境我的脸 2024-10-12 02:16:44

设计良好的数据库不会搜索书籍的所有交易来查找最新的交易。至少,数据库设计者将创建索引,并且 dbms 将(可能)使用它们。

MySQL 可以做到这一点;我对 Django 不太熟悉,不知道它是否足够聪明,可以让 MySQL 做到这一点。但如果不是的话,我会有点惊讶。

至于它是否明智且面向未来——我从当地的图书馆查阅了书籍、CD(音乐和书籍)、盒式磁带、地图、DVD、视频游戏、教育软件、玩具和镶框艺术品。

A well-designed database won't search through all the book's transactions to find the most recent. At the very least, the database designer will create indexes, and the dbms will (probably) use them.

MySQL can do that; I'm not familiar enough with Django to know whether it's smart enough to get MySQL to do that. I'd be a little surprised if it weren't, though.

As to whether it's sensible and future proof--from my local library I've checked out out books, CDs (of both music and books), cassette tapes, maps, DVDs, video games, educational software, toys, and framed artwork.

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