需要数据库设计指导
一位奶农兼兼职漫画家,拥有几群奶牛。他将每头牛分配到一个特定的牛群。在每个牛群中,农民都有一头他最喜欢的牛——这头牛通常出现在漫画中。每个牛群中都有一些不满者,主要是那些认为自己应该出现在漫画中的人,不同意农民选择最喜欢的牛,他们轻蔑地将其称为神圣的牛。结果,现在每个牛群都选出了一个牛群领袖。
这就是我认为表格应该看起来的样子,你能告诉我是否可以做得更好吗?到目前为止,我正在使用最喜欢的表作为中间进行多对多操作,这是最好的解决方案,也不需要 SQL 语句,这只是出于设计目的。
预先感谢您
Table Herd Table Favorite Table Cartoon Table Cow
PK herdID Intermediate Table PK cartoonID PK cowID
herdname cartoonTitle cowName
herdleader cartoonType
cartoonDate
编辑的图像 @ 3:01pmEST 这是正确的吗?
cowErd http://img838.imageshack.us/img838/1268/capture3h.png
添加了新图片 @ 8:57am 7/20/2010 请有人批评一下这个 ERD Erd2 http://img37.imageshack.us/img37/5794/capture3fc.png
添加了新图片 @ 12:47pm 7/20/2010 除非有任何反对意见,这是根据 Mark 的解释的最终草案 标记 ERD http://img651.imageshack.us/img651/691/capture4b.png< /a>
A dairy farmer, who is also a part-time cartoonist, has several herds of cows. He has assigned each cow to a particular herd. In each herd, the farmer has one cow that is his favorite - often that cow is featured in a cartoon. A few malcontents in each herd, mainly those who feel they should have appeared in the cartoon, disagree with the farmer's choice of a favorite cow, whom they disparagingly refer to as the sacred cow. As a result, each herd now has elected a herd leader.
This is what I think the tables should look like can you let me know if it can be done better? So far I'm doing a many to many using the favorite table as the intermediate is this the best possible solution also no SQL statements are needed this is just for design purposes.
Thank you in advance
Table Herd Table Favorite Table Cartoon Table Cow
PK herdID Intermediate Table PK cartoonID PK cowID
herdname cartoonTitle cowName
herdleader cartoonType
cartoonDate
edited image @ 3:01pmEST is this correct?
cowErd http://img838.imageshack.us/img838/1268/capture3h.png
added new image @ 8:57am 7/20/2010 can some one critique this ERD please
Erd2 http://img37.imageshack.us/img37/5794/capture3fc.png
added new image @ 12:47pm 7/20/2010 unless there's any objections this is the final draft per Mark's explanation
mark ERD http://img651.imageshack.us/img651/691/capture4b.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
迈克尔:
问题陈述中有哪些名词?有多少个?
由于只有一名农民,因此将他排除在未来的讨论之外。这些是你的基础
实体。
每个实体有什么属性?
因此,其中一些属性引用其他实体,而另一些则不引用。不引用其他基本实体的属性很简单。其他的则需要更多的考虑。
这些问题有助于概述实体之间的关系是强制性的还是可选的,以及关系是一对多还是多对多。
Michael:
What are the nouns in the problem statement, and how many of them are there?
As there is only one farmer, leave him out of future discussions. These are your base
entities.
What attributes does each entity have?
So some of these attributes reference other entites, and some do not. The attributes that do not reference other base entities are simple. The other ones require more consideration.
These questions help outline whether or not the relationships between the entities are mandatory or optional, and whether the relationships are one-to-many or many-to-many.
一个讨论项目,所以我将其设为社区 Wiki。
关系模型做得不好的一件事是强制在 Herd 级别持有的 SacredCow 和 HerdLeader 实际上指向属于该 Herd 成员的奶牛。
假设你的牛群是星形和十字形。 “星”兽群的详细信息可能会将 Rigel 指定为圣牛,将 Castor 指定为兽群领袖,但“牛”表可能将 Castor 显示为“十字”兽群的成员。实际上,当创建一个新的牛群时,当您的牛群没有牛(因此没有 HerdLeader/SacredCow)或牛没有牛群时,您将面临先有鸡还是先有蛋的情况。
另一种模型将具有“Cow”表,指示特定的牛是否是其牛群的 HerdLeader 和/或 SacredCow。 [在物理实现中,可以通过一个独特的约束强制每个 Herd 只有一头牛是 SacredCow 和一头牛是 HerdLeader。]。“Herd”表不会有 SacredCow 或 HerdLeader 。该模型无法强制每个牛群都有一个 HerdLeader 和一个 SacredCow。
两人都是模特。两者都有缺陷。在逻辑层面上,我可能会选择前者,因为它更加规范化。在物理上,我会考虑哪种不一致会更麻烦并且更有可能发生,并且我会选择最能防止这种情况的模型。
A discussion item, so I've made it a Community Wiki.
One thing that the relational model doesn't do well is enforcing that the SacredCow and HerdLeader held at the Herd level actually point to Cows that are members of that Herd.
Say your Herds are Star and Cross. The details for the 'Star' Herd may give Rigel as the SacredCow and Castor as the HerdLeader, but the 'Cow' table may show Castor as a member of the 'Cross' Herd. In practice, when creating a new Herd, you face a chicken and the egg scenario when you either have a Herd with no Cows (and hence no HerdLeader/SacredCow) or a Cow without a Herd.
An alternative model would have the 'Cow' table indicating whether a particular Cow is the HerdLeader and/or SacredCow for their herd. [In a physical implementation, it would be possible for a unique constraint to enforce that every Herd only had one cow that was a SacredCow and one cow that was a HerdLeader.] .The "Herd" table wouldn't have the SacredCow or HerdLeader. This model would fail to enforce that every herd had a HerdLeader and a SacredCow.
Both are models. Both have flaws. At the logical level, I'd probably go with the former as it is more Normalised. At the physical, I'd be be considering which inconsistency would be more troublesome and more likely to occur, and I'd be picking the model that best prevented it.
我喜欢你实际上尝试自己做这件事。
事实上你已经快到了,所以你的工作一点也不差。
让我们从对象/实体的角度来看这个问题。
这些实体包括以下内容:
Cows
这可能是关键,但是
牛可能有相同的名字,对吧?)
Herds
最终这是你的决定)
CowHerds
HerdID 上述字段的组合作为主键
CowCartoonCartoonID
....
正如评论中提到的,您还可以摆脱 CowHerds 表并直接在 Cows 表中引用 HerdID。
I like that you've actually tried to do this on your own.
In fact you are nearly there, so you haven't done a bad job at all.
Let's look at this from an Object / Entity perspective.
The entities include the following:
Cows
this could potentially be the key but
cow's may have the same name right?)
Herds
ultimately this is your decision)
CowHerds
The combination of the above field serves as a primary key
CowCartoon
....
As mentioned in the comments, you can also get rid of the CowHerds table and reference the HerdID directly in the Cows table.
假设一头牛一次只能属于一个牛群,并且假设同一幅漫画中可以出现不止一头牛(显然来自不同的牛群),我的建议是:
Table Herd
Table Cow
桌子卡通
桌子外观
Assuming a Cow can only belong to one herd at a time and assuming that more than one cow (from different herds, obviously) can appear in the same cartoon, my suggestion:
Table Herd
Table Cow
Table Cartoon
Table Appearance
第 1 部分。
如果满足以下条件:
那么您可以使用此部分模型实现这些规则:
请注意 FK 关系如何包含 herdID,而不仅仅是owID。这确保了只有牛群中的那些牛才可以成为该牛群的神圣牛或牛群领导者。
这种设计使得实施起来有点棘手,但并非不可能。 Herd 上的外键在像 Oracle 这样的数据库中必须是可延迟的,因为我们需要能够先为 Herd 插入行,然后才能为奶牛插入行,而一个 Herd 至少需要两头奶牛(圣牛和牧群领袖)。
第 2 部分。
下一个挑战是实现以下约束:
实现此目的的一种方法可能是将 Cows 拆分为两个独立的关系:SacredCows 和 NonSacredCows。
(在此设计中,不再需要约束 (sacredCow != herdLeader),因为根据定义它们现在是不同的奶牛。)
Part 1.
If the following are true:
Then you could implement these rules with this partial model:
Notice how the FK relationships include the herdID, not just the cowID. This ensures that only those Cows in a Herd may be made the sacredCow or herdLeader for that Herd.
This design makes things a little tricky to implement but not impossible. The foreign keys on Herd would have to be made deferrable in a database like Oracle, since we need to be able to insert the rows for a Herd before we can insert the rows for the Cows, and a Herd requires at least two Cows (the sacredCow and the herdLeader).
Part 2.
The next challenge is to implement the following constraint:
One way to do this may be to split Cows into two separate relations: SacredCows and NonSacredCows.
(In this design, the constraint (sacredCow != herdLeader) is no longer required because they are now different cows by definition.)