SQL 模型/架构设计、检查约束、FK 或表重新设计?

发布于 2025-01-01 16:43:21 字数 605 浏览 1 评论 0原文

我有以下模型(简化后,忽略所有其他字段,重点关注主键 ID):

[invoice]
 invoice_id
 customer_id
 invoice_description

[customer]
  customer_id
  company_id
  customer_name

[company]
 company_id
 company_name

[strategy]
 strategy_id
 strategy_descripton

一家公司包含许多客户,而这些客户又可以拥有许多属于该客户的发票。

虽然上面没有显示,但我也希望有一份属于一家公司(并且只有一家公司)的策略列表。

我希望客户、发票和公司能够定义一个“默认策略”,引用他们所属公司的策略。

我知道我可以在每个表(发票、客户和公司)中添加一个名为strategy_id的额外FK字段,但是我如何确保如果进行更改(有人试图将策略转移到另一家公司),我们最终不会得到发票或客户指向属于另一家公司策略列表的策略?

我将如何处理这个问题 - 我是否需要在业务逻辑中检查所有这些,或者使用检查约束,或者这可以通过表重新设计和外键来实现吗?

预先非常感谢,

克里斯

I have the following models (simplified leaving all other fields out, focusing on primary key ID's):

[invoice]
 invoice_id
 customer_id
 invoice_description

[customer]
  customer_id
  company_id
  customer_name

[company]
 company_id
 company_name

[strategy]
 strategy_id
 strategy_descripton

A company contains many customers, which in turn can have many invoices belonging to that customer.

Although not shown above, I also wish to have a list of strategies, that belong to a company (and only one company).

I would like the customer, invoice, and company, to be able to define a 'default strategy' which references a strategy of the company to which they belong.

I know i could put an extra FK field called strategy_id in each table (invoice, customer and company), but how can i ensure that if changes are made (someone tries to move a strategy to a different company) we do not end up with an invoice, or customer pointing to a strategy that belongs in another company's strategy list?

How would i deal with this - Do i need to check all this in business logic, or use check constraints, or can this be achieved through table redesign and foreign keys?

Many thanks in advance,

Chris

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

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

发布评论

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

评论(2

对岸观火 2025-01-08 16:43:21

如果公司改变策略,您是否介意属于该公司的客户或发票改变其策略。

例如:如果公司 1 有策略 1。它有 customer1、customer2(有发票1)。那么,如果company1 将其发票更改为strategy2,那么customer1、customer2 和invoice1 属于strategy2 或strategy1 是否可以。

如果没关系,请执行以下操作:

  • 向公司表添加约束。要检查客户/发票的策略,请找到母公司并检查其策略。

否则,如果确实重要的话:

  1. 向公司表添加约束,如果该公司有子客户/发票,则不允许更改它(检查它们是否存在)。
  2. 像上面提到的那样为每个表设置约束。

如果您想要更具体的答案,则必须澄清。

Do you mind if customer or invoice that belong to a company change their strategy if the company changes their strategy.

For example: If company1 has strategy1. It has customer1, customer2 (having invoice1). Then if company1 changes it's invoice to strategy2 is it ok if customer1, customer2 and invoice1 belong to strategy2 or strategy1.

If it doesn't matter do:

  • Add constraint to the company table. To check the strategy of the customer/invoice find the parent company and check it's strategy.

Otherwise if it does matter:

  1. Add constraint to the company table and don't allow it to be changed if there belongs child customer/invoice to the company (check if they exist).
  2. Set a constraint to each table like you mentioned above.

If you want a more specific answer you will have to clarify.

时光瘦了 2025-01-08 16:43:21

如果我理解正确的话,你希望每个公司都有很多战略,每个战略都属于一个公司(因此公司和战略之间存在1:n关系)。每家公司都有一个默认策略,这也是所有客户及其发票的默认策略。

您可以通过添加一个与 [strategy] 存在 1:1 关系的表 [company_default_strategy] 将这些约束定义为外键约束> 表(某些策略是默认的):

[invoice]
 invoice_id     PK
 customer_id    FK  to  customer(customer_id)
 invoice_description

[customer]
 customer_id    PK
 company_id     FK  to  company(company_id)
 customer_name

[company]
 company_id     PK
 company_name

[strategy]
 strategy_id    PK
 company_id     FK  to  company(company_id)
 strategy_descripton

[company_default_strategy]
 company_id     PK, FK1 
 strategy_id    FK1
   FK1 (strategy_id, company_id)  to  strategy(strategy_id, company_id)

If I understand correctly, you want every company to have many strategies, every strategy belonging to one company (so a 1:n relationship between company and strategy). And also one default strategy for every company which is is also the default strategy for all its customers and their invoices.

You could define these constraints as foreign keys constraints by adding one more table [company_default_strategy] which is in a 1:1 relationship with the [strategy] table (some strategies are default):

[invoice]
 invoice_id     PK
 customer_id    FK  to  customer(customer_id)
 invoice_description

[customer]
 customer_id    PK
 company_id     FK  to  company(company_id)
 customer_name

[company]
 company_id     PK
 company_name

[strategy]
 strategy_id    PK
 company_id     FK  to  company(company_id)
 strategy_descripton

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