使用带有 NOT NULL 的虚拟行来解决 DEFAULT NULL

发布于 2024-10-09 06:43:28 字数 670 浏览 0 评论 0原文

我知道使用 DEFAULT NULLS 不是一个好的做法,但是我有许多可选的查找值,这些值在系统中是 FK,因此为了解决这个问题,我正在做的事情是:我对每个 FK / 查找列使用 NOT NULL。我在每个查找表中都有第一行,它是 PK id = 1 作为虚拟行,所有列中只有“无”。这样,我可以在我的架构中使用 NOT NULL,并且如果需要,对于没有任何查找值的 FK,可以引用无行值 PK =1。

这是一个好的设计还是有其他解决方法?


编辑: 我有:
邻里表
邮政表。

每个街区都有一个城市,因此 FK 不能为 NULL。 但并非每个邮政编码都属于一个社区。有些会,有些不会,具体取决于国家/地区。因此,如果我对邮政和邻居之间的 FK 使用 NOT NULL,那么我就会被搞砸,因为必须输入一些值。所以我本质上所做的是:在每个表中都有一行作为虚拟行,只是为了链接 FK。

这样邻域表中的第一行将是:
n_id = 1
姓名=无
等等...

在邮政表中我可以有:
邮政编码 = 3456A3
FK(城市)= 莫斯科
FK (neighborhood_id)=1 作为 NOT NULL。

如果邻域查找表中没有虚拟行,那么我必须将 FK (neighborhood_id) 声明为默认空列并在表中存储空白。这是一个示例,但有大量值在许多表中都会有空白。

I know having DEFAULT NULLS is not a good practice but I have many optional lookup values which are FK in the system so to solve this issue here is what i am doing: I use NOT NULL for every FK / lookup colunms. I have the first row in every lookup table which is PK id = 1 as a dummy row with just "none" in all the columns. This way I can use NOT NULL in my schema and if needed reference to the none row values PK =1 for FKs which do not have any lookup value.

Is this a good design or any other work arounds?


EDIT:
I have:
Neighborhood table
Postal table.

Every neighborhood has a city, so the FK can be NOT NULL.
But not every postal code belongs to a neighborhood. Some do, some don't depending on the country. So if i use NOT NULL for the FK between postal and neighborhood then I will be screwed as there has to be some value entered. So what i am doing in essence is: have a row in every table to be a dummy row just to link the FKs.

This way row one in neighborhood table will be:
n_id = 1
name =none
etc...

In postal table I can have:
postal_code = 3456A3
FK (city) = Moscow
FK (neighborhood_id)=1 as a NOT NULL.

If I don't have a dummy row in the neighborhood lookup table then I have to declare FK (neighborhood_id) as a Default null column and store blanks in the table. This is an example but there is a huge number of values which will have blanks then in many tables.

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

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

发布评论

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

评论(4

夏末染殇 2024-10-16 06:43:28

这是一个好的设计还是有其他解决方法?

ISNULLCOALESCELEFT JOIN

Is this a good design or any other work arounds?

ISNULL or COALESCE and LEFT JOIN

意中人 2024-10-16 06:43:28

通常,“无”是与选项列表中的任何其他选项一样的选项。为其专门排一排可能是完全合理的;它简化了事情。如果您将其他信息链接到选项(例如人类可读的名称),这可能会特别实用。

Often "None" is an option like any other in a list of options. It may be totally reasonable to have a special row for it; it simplifies things. It may be especially practical if you link other information to options, e.g. a human-readable name.

青衫负雪 2024-10-16 06:43:28

您始终可以使用左连接来连接可能不存在的邮政编码。

select * from from table_a 
  left join table_b    
  on table_a.postalcode_id = table_b.postalcode_id

无论 postalcode_id 是否为空,都会选择行。当您使用幻数来指定空值时,查询的可读性就会降低。

明确:

select count(*) from table_a where postalcode_id is null;

不太清楚:

 select count(*) from table_a where postalcode_id = 1;

使用空值使您的查询显式处理空值情况,但它也会自我记录您正在处理空值的意图。

You can always use left joins to join postal codes that may not exists.

select * from from table_a 
  left join table_b    
  on table_a.postalcode_id = table_b.postalcode_id

will select rows whether or not the postalcode_id is null or not. When you use magic numbers to designate nulls then queries become less readable.

clear:

select count(*) from table_a where postalcode_id is null;

Not so clear:

 select count(*) from table_a where postalcode_id = 1;

Using nulls makes your queries explicitly handle null cases, but it also self-documents your intentions that nulls are being handled.

慕烟庭风 2024-10-16 06:43:28

这似乎是数据库中过早优化的一个简单案例:

alt text

如果您的架构是这样的,那么我不没看出什么问题。有些邮政编码位于附近,有些则不在附近。对于可为空的列来说这是一个很好的例子。

关于避免空值的建议是关于避免不属于表中的信息。例如,如果您还有另外五列仅与邻近地区的邮政编码相关,那么对于不在邻近地区的邮政编码,这些列将为空。这将是为邻近地区的邮政编码创建第二个并行表的一个很好的理由,其中可以包含其他五列。

更重要的是,如果性能是一个问题,那么解决方案是尝试两种方法,测试性能,看看哪种性能最好。这种性能考虑将与设计的简单性和可读性相竞争,并且性能可能会获胜。


举个例子来说明这个问题。我从对象角色建模模型开始,与我用来生成早期 ER 图的模型相同。但是,我创建了 PostalCode 的子类型,并向该子类型添加了两个以上的强制角色:

ORM Model

这可以生成 ER 模型与第一个非常相似:

ER Model Adsorbed

但此模型无法显示每当邮政编码为邻里邮政编码。以下模型确实表明:

ER Model Sepeded

我会说,如果您有一套在某些情况下是强制性的可选列,那么您应该创建一个“子类型”,该子类型始终使这些列不为空。但是,如果您只是有可能随机不为空的随机列,则将它们保留为主表中的 NULL 列。

This seems like a simple case of premature optimization in a database:

alt text

If your schema is something like this, then I don't see a problem. Some postal codes are in a neighborhood, some aren't. That's a good case for a nullable column.

The advice about avoiding nulls is about avoiding information that does not belong in the table. For instance, if you had another five columns which only pertained to postalcodes which were in a neighborhood, then those columns would be null for postal codes which were not in a neighborhood. This would be a good reason to have a second, parallel table for postalcodes which were in a neighborhood, which could contain these other five columns.

More importantly, if performance is a concern, then the solution is to try it both ways, test the performance, and see which performs best. This performance consideration would then compete with the simplicity and readability of the design, and performance might win.


An example to illustrate the issue. I started with an Object-Role Modeling model, the same that I used to produce the earlier ER diagram. However, I created a subtype of PostalCode and added two more mandatory roles to the subtype:

ORM Model

This can produce an ER model very similar to the first:

ER Model Adsorbed

But this model fails to show that there are columns which are mandatory whenever the PostalCode is a NeighborhoodPostalCode. The following model does show that:

ER Model Separated

I would say that if you have a set of optional columns which are mandatory under certain circumstances, then you should create a "subtype" which always has those columns NOT NULL. However, if you simply have random columns which may randomly be not null, then keep them as NULL columns in the main table.

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