java 如何传递NULL数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1048539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:55:17  来源:igfitidea点击:

How to Pass NULL Data Type?

java.netdatabasenull

提问by Hemant

What is the datatype for NULLwhen passing that value for no data into a database?

NULL将没有数据的值传递到数据库时的数据类型是什么?

回答by Hemant

There is NOdata type of NULL. NULL itself means ABSENCEof data. When there is no data, how can it have type?

NONULL的数据类型。NULL本身就意味着没有数据。没有数据,怎么会有类型?

回答by ConcernedOfTunbridgeWells

Null does not have a specific data type in SQL. Any nullable column or variable can contain null. Null is never equal or unequal to anything. You can cast a variable holding null to another variable and get null, for example:

Null 在 SQL 中没有特定的数据类型。任何可为空的列或变量都可以包含空值。Null 永远不等于或不等于任何东西。您可以将一个持有空值的变量转换为另一个变量并获得空值,例如:

declare @a integer
set @a = null
select convert (float, @a)

----------------------
NULL

(1 row(s) affected)

回答by Greg

Usually NULL is its own datatype - the type of 1 is "INTEGER", the type of the type of NULL is "NULL"

通常NULL是它自己的数据类型——1的类型是“INTEGER”,NULL类型的类型是“NULL”

回答by nvogel

In SQL a NULL is a "mark" (something other than a value) that can apply to any SQL type. So it is orthogonal to type.

在 SQL 中,NULL 是可以应用于任何 SQL 类型的“标记”(值以外的东西)。所以它与类型正交。

回答by Quassnoi

Datatype for NULLis as meaningless as datatype for 0: it can be INTEGER, FLOATor a VARCHAR. You cannot tell it just from the value.

数据类型 forNULL与数据类型 for 一样无意义0:它可以是INTEGERFLOAT或 a VARCHAR。你不能仅仅从价值上说出来。

NULLis legitimate value in almost every datatype domain, which means the absence of actual value.

NULL几乎在每个数据类型域中都是合法值,这意味着没有实际值。

It's also meaningless to discuss datatypes out of context of certain RDBMS.

脱离特定上下文讨论数据类型也是没有意义的RDBMS

In SQLite, for instance, datatypes are value-bound, not column-bound, and NULLis a first-class datatype per se.

SQLite例如,在 中,数据类型是值绑定的,而不是列绑定的,并且NULL本身就是一流的数据类型。

In Oracle, the datatypes are more strictly defined. For instance, this query works:

在 中Oracle,数据类型的定义更为严格。例如,此查询有效:

SELECT  COALESCE(dt, i)
FROM    (
        SELECT  CAST(NULL AS DATE) AS dt, CAST(NULL AS DATE) i
        FROM    dual
        ) q

and this does not:

这不会:

SELECT  COALESCE(dt, i)
FROM    (
        SELECT  CAST(NULL AS DATE) AS dt, CAST(NULL AS NUMBER) i
        FROM    dual
        ) q

, because the latter query returns two columns of different datatypes, both of them having values of NULL, and COALESCErequires both arguments to have same datatype.

,因为后一个查询返回两个不同数据类型的列,它们的值都为NULL,并且COALESCE要求两个参数具有相同的数据类型。

It's better to say that a NULLof any datatype can be implicitly converted to a NULLon another datatype.

最好说NULL任何数据类型的 a 都可以隐式转换为NULL另一种数据类型的 a 。

For instance, a VARCHARcan be implicitly converted to a INTEGERif it has value of 0, but cannot if it has value of 'some_string'.

例如,如果VARCHARa 的INTEGER值为 ,则可以隐式转换为 a 0,但如果值为,则不能'some_string'

For NULL's, any datatype can be implicitly converted to any other datatype, if the implicit conversion between them is allowed at all.

对于NULL's,任何数据类型都可以隐式转换为任何其他数据类型,前提是它们之间的隐式转换是完全允许的。

回答by user250343

Usually SQL NULLdoes not have a type associated with it. However there are exceptions. Database engines postgresql and derby (javadb) require that null has a type. In other words they do not support untyped null. So query conditions like NULL IS NULLmay fail. Here, NULLmust be given a type, the expected type of the target that processes the NULLvalue. In this case this appears silly because there is no target and this can be counterproductive.

通常 SQLNULL没有与之关联的类型。不过也有例外。数据库引擎 postgresql 和 derby (javadb) 要求 null 具有类型。换句话说,它们不支持无类型空值。所以查询条件NULL IS NULL可能会失败。这里,NULL必须给定一个类型,即处理该NULL值的目标的预期类型。在这种情况下,这看起来很愚蠢,因为没有目标,这可能会适得其反。

See CAST function: -- you must cast NULL as a data type to use it

请参阅CAST 函数: -- 您必须将 NULL 转换为数据类型才能使用它

See Queries with guarded null Parameter failand Add support for setObject(arg, null)

请参阅带有受保护的 null 参数失败的查询添加对 setObject(arg, null) 的支持

Please vote for these issues so that the odd database engines change their ways.

请为这些问题投票,以便奇怪的数据库引擎改变他们的方式。

回答by MAbraham1

Prabhahar, each type of database driver has its own way of handling NULL. You will have to examine the driver API for the specific database.

Prabhahar,每种类型的数据库驱动程序都有自己的处理NULL. 您必须检查特定数据库的驱动程序 API。

For example if you are using the Java Derby database, simply pass in the Java native type, null as shown in Ian Bjorhovde's answer to "Derby's Handling of NULL Values":

例如,如果您使用的是 Java Derby 数据库,只需传入 Java 本机类型 null,如Ian Bjorhovde 对“Derby 对 NULL 值的处理”的回答所示:

insert into T_AUTHOR (
  ID, FIRST_NAME, LAST_NAME, 
  DATE_OF_BIRTH, YEAR_OF_BIRTH, ADDRESS) 
VALUES ( 
  1000, 'Lukas', 'Eder', 
  '1981-07-10', null, null 
);

Here is another nullexample of JDBC:Inserting null to Integer column:

这是JDBC:Inserting null to Integer column 的另一个null示例:

pst.setNull(4, java.sql.Types.INTEGER);

回答by bobby

I think the question defeats itself. If NULL had a datatype, wouldn't you be forced to change it with every instantiation outside of its default. For example, when you create it as a character, but then force it into an object's value?

我认为这个问题本身就失败了。如果 NULL 有一个数据类型,你会不会被迫在每个实例化之外的默认值时更改它。例如,当您将其创建为角色,然后将其强制转换为对象的值时?

NULL==NULL

NULL==NULL

That is all.

就这些。

回答by antoni.rasul

Actually, in PowerShell comparing $null -eq $null gives False. Also, -not $null will give you True, so here it seems to be reprezented as False. I know, PowerShell might not be a good example, but still :)

实际上,在 PowerShell 中比较 $null -eq $null 会给出 False。此外,-not $null 会给你 True,所以在这里它似乎被表示为 False。我知道,PowerShell 可能不是一个很好的例子,但仍然 :)