数据库字符串字段类型:是否可以为空?

时间:2022-04-27 22:35:07

I have several tables that contain several strings for fields. Some of these fields may or may not always contain data. If I create a record with a string field that's null at the time of the record's creation, does it become a problem when I do a read query of that record because the field is null or does the query return an empty string? What's considered based? Non-nullabe strings or nullable strings, and when to know the difference?

我有几个表,其中包含字段的几个字符串。其中一些字段可能或不总是包含数据。如果我用一个字符串字段创建一个记录,该字段在创建记录时是空的,那么当我对该记录进行读查询时是否会出现问题,因为该字段是空的,还是查询返回一个空字符串?被认为是基于什么?非空字符串或可空字符串,什么时候知道它们的区别?

Thanks for your suggestions.

谢谢你的建议。

3 个解决方案

#1


1  

The query will return an empty string if that is what is stored in the database and a null value (System.DBNull) if it is a nullable string with a null in it.

如果数据库中存储的是空字符串,如果是空值(System.DBNull),则查询将返回空字符串。

The decision, however, should be based on the data you are modeling, not the functionality of queries.

然而,这个决定应该基于您所建模的数据,而不是查询的功能。

Null in a DB generally means that a value is either unknown or was left blank. This can be distinct from an empty string which could be used to mean there IS no value.

DB中的Null通常意味着一个值是未知的,或者是空白的。这可以与空字符串不同,空字符串可以用来表示没有值。

Take for example a MiddleName field. You might want to distinguish between someone with no middle name (empty string) and someone who didn't enter it yet (Null). In this case you'd want a nullable string field.

以米德尔南场为例。您可能想要区分没有中间名(空字符串)的人和尚未输入中间名(空字符串)的人。在这种情况下,您需要一个空字符串字段。

#2


1  

There is a similar question on the stack exchange site for Database Administrators. They pretty much echo what is said here.

在堆栈交换站点上,数据库管理员也有类似的问题。他们几乎和这里说的一致。

One thing that has not been mentioned is that if you attempt to multiply by a null value by a real value, you get a null result. Same for lots of other operations.

有一件事没有提到如果你试图用一个空值乘以一个实值,你会得到一个空结果。很多其他操作也是如此。

#3


1  

That has been debated over and over again million times. In my opinion, though, NULL values are there for a reason, and you should stay away from "magic" values (even empty string means something, doesn't it).

这已经被争论了无数次了。但在我看来,零值的存在是有原因的,你应该远离“神奇”的值(即使是空字符串也有意义,不是吗?)

NULL values do NOT get converted to anything at read-time. You'll get null. You're supposed to interpret it as "this value has not been set", and treat it that way. If you replace it with any magic value (e.g. empty string), you're just looking for trouble, as sooner or later that magic value will start having a meaning of something else, and you won't know how to distinguish it from those that have never been set.

空值在读取时不会转换为任何内容。你会得到零。你应该把它解释为“这个值还没有设置”,然后这样对待它。如果你用任何神奇的值(例如空字符串)替换它,你只是在寻找麻烦,因为这个神奇的值迟早会开始有其他东西的含义,你不会知道如何区分它和那些从未被设置过的。

In other words, if you consider a property to be optional, then it's definitely a candidate to be nullable. Whenever you start debating that with yourself, remember the word "optional", and I'm sure you'll make a good decision.

换句话说,如果您认为一个属性是可选的,那么它肯定是一个可以为空的候选。无论何时你开始和自己辩论,记住“可选”这个词,我相信你会做出一个好的决定。

#1


1  

The query will return an empty string if that is what is stored in the database and a null value (System.DBNull) if it is a nullable string with a null in it.

如果数据库中存储的是空字符串,如果是空值(System.DBNull),则查询将返回空字符串。

The decision, however, should be based on the data you are modeling, not the functionality of queries.

然而,这个决定应该基于您所建模的数据,而不是查询的功能。

Null in a DB generally means that a value is either unknown or was left blank. This can be distinct from an empty string which could be used to mean there IS no value.

DB中的Null通常意味着一个值是未知的,或者是空白的。这可以与空字符串不同,空字符串可以用来表示没有值。

Take for example a MiddleName field. You might want to distinguish between someone with no middle name (empty string) and someone who didn't enter it yet (Null). In this case you'd want a nullable string field.

以米德尔南场为例。您可能想要区分没有中间名(空字符串)的人和尚未输入中间名(空字符串)的人。在这种情况下,您需要一个空字符串字段。

#2


1  

There is a similar question on the stack exchange site for Database Administrators. They pretty much echo what is said here.

在堆栈交换站点上,数据库管理员也有类似的问题。他们几乎和这里说的一致。

One thing that has not been mentioned is that if you attempt to multiply by a null value by a real value, you get a null result. Same for lots of other operations.

有一件事没有提到如果你试图用一个空值乘以一个实值,你会得到一个空结果。很多其他操作也是如此。

#3


1  

That has been debated over and over again million times. In my opinion, though, NULL values are there for a reason, and you should stay away from "magic" values (even empty string means something, doesn't it).

这已经被争论了无数次了。但在我看来,零值的存在是有原因的,你应该远离“神奇”的值(即使是空字符串也有意义,不是吗?)

NULL values do NOT get converted to anything at read-time. You'll get null. You're supposed to interpret it as "this value has not been set", and treat it that way. If you replace it with any magic value (e.g. empty string), you're just looking for trouble, as sooner or later that magic value will start having a meaning of something else, and you won't know how to distinguish it from those that have never been set.

空值在读取时不会转换为任何内容。你会得到零。你应该把它解释为“这个值还没有设置”,然后这样对待它。如果你用任何神奇的值(例如空字符串)替换它,你只是在寻找麻烦,因为这个神奇的值迟早会开始有其他东西的含义,你不会知道如何区分它和那些从未被设置过的。

In other words, if you consider a property to be optional, then it's definitely a candidate to be nullable. Whenever you start debating that with yourself, remember the word "optional", and I'm sure you'll make a good decision.

换句话说,如果您认为一个属性是可选的,那么它肯定是一个可以为空的候选。无论何时你开始和自己辩论,记住“可选”这个词,我相信你会做出一个好的决定。