postgresql 整数列可以为空吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46477189/
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-21 02:38:39  来源:igfitidea点击:

Can an integer column be null?

postgresqlnullintegerquotespostgresql-9.3

提问by efoc

I made an integercolumn that is nullby default but when I put empty double quotes ""it gives this error:

我创建了一个默认integer列,null但是当我放置空双引号时,""它会出现此错误:

ERROR: invalid input syntax for integer: ""
ERROR: invalid input syntax for integer: ""

Does the integercolumn have to be 0then?

那么integer柱子必须是0吗?

I am using Java to send the query to the database.

我正在使用 Java 将查询发送到数据库。

回答by Sam Hartman

An integer column can be null, but ''is an empty string not null. The right syntax for a null integer (or any other sql type) is null.

整数列可以为空,但''不能为空字符串。空整数(或任何其他 sql 类型)的正确语法是null.

回答by Erwin Brandstetter

Double quotes can be used to delimit identifiers, like "myColumnName".
Single quotes are used to delimit valuesin string literals, like 'my string literal'.
For integervalues you typically use numeric constantsto input values, which are not quoted at all, like 123. (But you cancast a string literal, too.)
Numeric data types (integer, numeric, double precision, ...) cannot store empty strings (''), only string types (text, varchar, ...) can.
To pass a NULLvalue, use the key word NULL. Or omit the column completelyfrom your INSERTstatement. If you did not define a different column default, it defaults to NULLautomatically.

双引号可用于分隔标识符,例如"myColumnName".
单引号用于分隔字符串文字,喜欢'my string literal'
对于integer值,您通常使用数字常量来输入值,这些值根本不带引号,例如123. (但您也可以转换字符串文字。)
数字数据类型 ( integer, numeric, double precision, ...) 不能存储空字符串 ( ''),只有字符串类型 ( text, varchar, ...) 可以。
要传递NULL值,请使用关键字NULL。或者完全从你的INSERT陈述。如果您没有定义不同的列默认值,则默认为NULL自动。

The double quotes you see in the error message are just delimiters added by Postgres for the purpose of the error message, meaning you passed an empty string (''). If you'd actually pass

您在错误消息中看到的双引号只是 Postgres 为错误消息添加的分隔符,这意味着您传递了一个空字符串 ( '')。如果你真的通过

empty double quotes ""

空双引号 ""

(which would require to be single quoted in turn: '""'), you would see this error message:

(这将需要依次单引号:)'""',您将看到此错误消息:

ERROR:  invalid input syntax for integer: """"
ERROR:  invalid input syntax for integer: """"

Not sure how Java plays into this.

不知道 Java 如何发挥作用。

Consider the chapter Lexical Structurein the Postgres manual.

考虑Postgres 手册中的词法结构一章。

Related:

有关的: