gpt4 book ai didi

Most efficient way to store historical records of two one-to-one tables?(存储两个一对一表的历史记录的最有效方式?)

转载 作者:bug小助手 更新时间:2023-10-25 23:26:25 24 4
gpt4 key购买 nike



I have two tables, member and subscription. Say the schemas are as following

我有两张桌子,会员和订阅。假设架构如下所示


TABLE member {
member_id integer [not null, pk]
phone_number varchar(15) [not null, UNIQUE]
membership enum(['STUDENT', 'STAFF', 'STAFF_SON']) [not null]
name varchar(100) [not null]
faculty varchar(64)
email varchar(100)
}

and


TABLE subscription {
member_id integer [not null, pk, ref: - member.member_id]
active bool [not null]
invoice varchar(256)
subscription_date datetime [not null]
expiration_date datetime [not null]
}

now, how to store historical records of the subscription table to keep track of a particular member's subscriptions changes over the course period? i.e., when a member renew their subscription.

现在,如何存储订阅表的历史记录,以跟踪特定成员在课程期间的订阅更改?即当成员续订他们的订阅时。


I've tried many things but I doubt their efficiency.
Let's call it subscriptions_record and it should have a one-to-many relationship with subscriptions. Here's my first work around

我试过很多方法,但我怀疑它们的有效性。让我们将其命名为SUBSCRIPTIONS_RECORD,它应该与订阅具有一对多的关系。这是我的第一个作品


TABLE subscription_records {
id integer [pk]
member_id integer [fk, ref: > subscription.member_id]
invoice varchar(256) [fk, ref: > subscription.member_id]
subscription_date datetime [fk, ref: > subscription.member_id]
expiration_date datetime [fk, ref: > subscription.member_id]
}

Is this a good solution? I would appreciate any feedback or suggestions on how to improve my approach.

这是一个好的解决方案吗?如果有任何关于如何改进我的方法的反馈或建议,我将不胜感激。


P.S. the database will contain 20k members at max.

另外,该数据库最多将包含20000名成员。


更多回答
优秀答案推荐

I see the subscription_records as an audit history of the subscription. If so, then I'd suggest to also add a last_modified_datetime and last_modified_user columns to it to record at what time and by who the subscription was changed last.

我将SUBSCRIPTION_RECORDS视为订阅的审核历史记录。如果是这样的话,我建议还向其中添加LAST_MODIFIED_DATETIME和LAST_MODIFIED_USER列,以记录上次更改订阅的时间和人员。


I'd put active column back in too; in case the user marked the subscription as inactive and then back to active ?

我也会放回活动列;以防用户将订阅标记为非活动,然后再标记为活动?


Could you delete a record from subscription ? if yes, then you'll have to think again if you want to have a foreign key on the subscription_records table.

您可以从订阅中删除一条记录吗?如果是,那么如果您希望在SUBSCRIPTION_RECOVERS表上有一个外键,那么您必须三思。


Make sure that your insert/update/delete on the subscription and subsequently on subscription_records are done in one transaction

确保在一个事务中完成对订阅的INSERT/UPDATE/DELETE以及随后的SUBSCRIPTION_RECORDS


You might also want to throw in an index or two on the table depending on how to intend to query it.

您可能还想在表上添加一个或两个索引,具体取决于查询它的方式。


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com