这是一个好的数据库设计理念吗?

发布于 2024-07-10 11:16:57 字数 1413 浏览 7 评论 0原文

编辑1:尝试通过重命名表及其关系来澄清问题。 EDIT2:请不要查看我在三个数据库表中保存的数据类型。 他们是即时化妆的。 它们不是我的真实世界场景(不,我不能谈论我的真实世界数据.. 事实上,目前有 1 个父母和 6 个孩子)。 请忽略什么类型的数据,只看需要某些数据的事实。 EDIT3:两个 FK 是 0 或 1 对 1 的关系。 不是0到很多。 不是 1 对 1。我试图避免 0 或 1 对 1 关系与 1 对 1 关系,因此我不需要 OUTER JOINS,而是需要 INNER JOIN。

问题:我需要知道建议的数据库设计是否好/坏/蹩脚/等等。

问题:今天我尝试创建一个索引视图,但失败了,因为我的表有外连接。 叹。 所以我想知道是否可以将其重新修复为如下设计:

  • 三张桌子。
  • table_User 在 table_Address 上有一个 FK
  • 上有一个 FK
  • table_User 在 table_Vehicle等

,表 B 和 C(现在有点像查找表)有..

  • Id INT IDENTITY PK
  • Description NVARCHAR(100) NULLABLE

通知可空的? 这样,table_User 中的某些内容在 table_Address 中不存在...该字段为空(因为内部联接)。

之前,我做了一个 LEFT OUTER JOIN,所以如果 table_b 中没有数据,我会得到每个字段的结果为空。

我将在这里抛出一些数据示例...

Table_User

  • ID:1,姓名:Fred,AddressID:1(NULL)
  • ID:2,姓名:Joe,AddressID:2(1 smith street.....)
  • ID:3 ,名称:Jane,AddressID:2(1 smith street.....)

Table_Address

  • ID:1,Description = NULL
  • ID:2,Description = 1 smith street

等等。

然后我终于可以将这一切放入索引视图中。 (我的现实生活场景大约有 8 张桌子)。

注意:数据库是 Microsoft Sql Server 2008,但这可以适用于任何数据库。

Q1:这个设计看起来还可以吗?

Q2:那么我在这里所做的就是标准化数据,对吗? 通过将内部连接保持在一起。

Q3:最后,如果这是一个好的方法..我还可以通过一些唯一的约束或键或索引或什么(我不确定)来确保表中的数据是唯一的(例如街道地址)正确的术语)。

谢谢各位大师!

EDIT1: Tried to clear the question up by renaming the tables AND their relationships.
EDIT2: Please don't look at the what TYPE of data i'm holding in the three DB tables. They were made up on the fly. They are NOT my real world scenarios (and no, I can't talk about my real world data .. in fact it's 1 parent and 6 children, currently). Please just ignore what type of data and just look at the fact that some data is required.
EDIT3: The two FKs are a 0 or 1 to 1 relationship. NOT 0 to many. Not 1 to 1. I'm trying to avoid the 0 or 1 to 1 relationship to a 1 to 1 relationship so i don't need to have OUTER JOINS but instead have an INNER JOIN.

Question: I need to know if the proposed database design is good/bad/lame/etc..

Problem: today i tried to make an indexed view, but failed 'cause my tables have outer joins. Sigh. So i was wondering if i can refix this up to be like the following design:

  • Three tables.
  • table_User has a FK on table_Address
  • table_User has an FK on table_Vehicle
  • etc..

and table B and C (which sorta act like lookup tables now) have..

  • Id INT IDENTITY PK
  • Description NVARCHAR(100) NULLABLE

notice the nullable? this way, something in table_User doesn't exist in table_Address ... the the field is null (because of the inner join).

Before, i made that an LEFT OUTER JOIN, so if there was no data in table_b, i'll get nulls are the result for each field.

I'll throw some data examples here...

Table_User

  • ID: 1, Name: Fred, AddressID: 1 (NULL)
  • ID: 2, Name: Joe, AddressID: 2 (1 smith street.....)
  • ID: 3, Name: Jane, AddressID: 2 (1 smith street.....)

Table_Address

  • ID: 1, Description = NULL
  • ID: 2, Description = 1 smith street

etc.

So then i can finally put this all into an indexed view. (my real life scenario has around 8 tables).

NOTE: DB is Microsoft Sql Server 2008, but this could be for any DB.

Q1: Does that design seem ok?

Q2: So what i'm doing here is i'm normalising the data, right? by keeping the inner joins together.

Q3:Lastly, if this is an ok way about it .. can i also make sure the data in the tables are unique (eg. the street addresses) by having some unique constraints or keys or indexes or what (i'm not sure of the proper terminology).

thanks gurus!

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

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

发布评论

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

评论(4

陌路终见情 2024-07-17 11:16:57

我发现你的问题令人困惑,但也许我可以提供一点帮助。

首先,表没有联接,而查询有。 您不会创建一个与另一个表连接的表。 只有 2 个可能相关的表,您可以使用联接查询这些表。

我建议您阅读有关数据库规范化的内容。 百科有一篇很棒的文章:http://en.wikipedia.org/wiki/Database_normalization

维基 你目前的情况,我不确定你想做什么。 如果该地址在不同的行中重复,则拥有地址的 ID 似乎没问题。 然而,需要几个“地址表”似乎很奇怪。 设计时要记住的最重要的事情是:
- 每个表都有正确的主键,这样您就可以正确连接表。
- 除非有非常充分的理由,否则不要重复数据。
但我再次推荐之前的文章。

希望有帮助! :)

I find your question confusing, but maybe I can help a little.

First of all, tables don't have joins, queries have. You don't make a table with a join to another table. There are just 2 tables that may be related, and you can query those tables using joins.

I recommend you to read about db normalization. Wikipedia has a great article: http://en.wikipedia.org/wiki/Database_normalization

About your current case, I'm not sure of what are you trying to do. Having an ID for an address seems ok if that address is repeated in different rows. However, needing several "address tables" seems weird. The most important things to remember when designing are:
- Have a correct primary key in every table, so you can correctly join tables.
- Do not repeat data unless you have a very very good reason.
But I again recommend the previous article.

hope that helps! :)

云淡月浅 2024-07-17 11:16:57

这是一个非常令人困惑的问题,所以请查找数据库的规范化。 第三范式(希望它的英文名称是这样的)应该可以解决大多数问题。

快速提示:如果您有重复的数据,那么您需要一个单独的表,您可以通过外键在第一个表中引用该表。 其他一切都只是查询。

A very confusing question, so please look up normalization of databases. The 3rd normal form (hopefully it is called like that in english) should solve most problems.

Quick tip: if you have data that is repeated then you need a separate table which you reference in the first one via a foreign key. Everything else is just queries.

夏有森光若流苏 2024-07-17 11:16:57

因此,您基本上是在表 B 和 C 中添加虚假记录,以便具有与 A 中相同的行数? 如果我是你,我不会这样做,因为如果你的数据集很大,那么你会在没有任何真正需要的情况下增加行数,而且你还面临着不一致的风险(你的布局在很大程度上取决于你的能力)这些插入的虚假记录)。 除此之外,您希望通过索引视图实现什么目的? 性能增益? 您没有编写您正在使用的 DBMS,但根据我在 MSSQL 中的经验,这不会给您带来很多收获,因为只要您在表 A、B 和 C 中有适当的索引,服务器就能够使用即使没有索引视图,它们也能构建良好的查询计划。

So you are basically adding bogus records in tables B and C in order to have the same number of rows as in A? I wouldn't do it if I were you, because if your dataset is large, then you are increasing the number of rows without any real need and also you are running a risk of an inconsistency (your layout heavily depends on your ability to have these bogus records inserted). Apart from that, what do you want to achieve with an indexed view? Performance gain? You are not writing what DBMS you are using, but from my experience in MSSQL this ain't going to give you a lot of gain, because provided that you have proper indexes in tables A, B and C the server will be able to use them to build a good query plan even without an indexed view.

旧城空念 2024-07-17 11:16:57

我个人对这种设计说“不”。 原因:(

  • 可能不适用)尝试保持地址字段标准化意味着您需要处理此字段以确保避免意外的重复。 即,您必须确保用户以正确的格式输入地址(否则“1 mystreet”也可以输入为“1,mystreet”或其他任何内容 - 需要检查这一点以避免重复,否则规范化毫无用处)
  • 即使你找到了一个标准化的理由(即为地址保留单独的表),“虚拟”地址的概念对我来说很奇怪。 为什么不使用可为空的 FK 关系,即在父用户表中存储 NULL 地址 ID,而不是仅仅在其中放置一个虚拟 ID。

I'd personally say 'no' to this design. Reasons:

  • (might not apply) trying to keep the address field normalized implies you need to process this field to make sure you avoid unintended duplicates. I.e. you must make sure the user enters the address in the correct format (otherwise '1 mystreet' could also be entered as '1, mystreet' or whatever - need to check this to avoid duplicates, otherwise normalization is good for nothing)
  • even if you find a reason to normalize (i.e. keep separate table for address), the concept of "dummy" address is strange to me. Why not use a nullable FK relationship, i.e. store a NULL address ID in the parent user table, instead of just putting a dummy ID in there.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文