数据库设计——这里有冗余吗?
我有四个表:公司、产品、客户和销售。它们之间的关系如下:
- 一家公司可以拥有多个产品
- 一家公司可以拥有多个客户
- 销售是产品和客户的子项。每条销售记录都涉及一种产品和一位客户。
我对您可以通过两条不同的路线从销售追溯到公司这一事实感到不安。这可能会出现数据完整性问题 - 如果产品路由说最终母公司是 A 公司,而客户路由说它是 B 公司怎么办?
对我来说,这听起来像是 SQL 反模式,但我可能是错的。我能做什么呢?
谢谢!
I have four tables: Companies, Products, Customers and Sales. They are related as follows:
- One Company can have multiple Products
- One Company can have multiple Customers
- A Sale is a child of a Product and a Customer. Each Sale record refers back to one Product and one Customer.
I am feeling uneasy about the fact that you can trace back from the Sale to the Company via two different routes. This raises the potential for data integrity issues - what if the Products route says the ultimate parent is Company A, and the Customer route says it's Company B?
This sounds like a SQL-antipattern to me, but I could be wrong. What can I do about it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于。你说:
这是否意味着每个
客户
只能在一家公司
购物?如果不是,
客户
和公司
之间的关系不应该由他们购买的产品
来定义吗?这意味着删除
Customer
上的fk_Company
字段(或等效字段)。但是,如果关系是
客户
注册的公司
,则情况有所不同。在这种情况下,如果购买的
产品
与客户
“所属”的公司
之间存在冲突,则不是真正的冲突- 冲突将发生在客户
注册的公司
与客户注册的
刚刚购物at,即不是真正的冲突。公司
之间最终的问题是,
客户
和公司
之间的关系是什么 -客户
It depends. You say:
Does this mean that each
Customer
only shops at oneCompany
?If not, shouldn't the relationship between a
Customer
and aCompany
be defined by theProducts
they have purchased?This would mean removing the
fk_Company
field (or equivalent) on theCustomer
.However, if the relationship is instead the
Company
that theCustomer
registered at, this is different.In that case, if there was a conflict between the
Products
bought and theCompany
theCustomer
"belongs to", it is not a real conflict - the conflict would be between theCompany
theCustomer
registered at, and theCompany
that theCustomer
has just shopped at, i.e. not a real conflict.The ultimate question is, what is the relationship between a
Customer
and aCompany
- is it where theCustomer