oracle 是否可以将 ''(空字符串)作为非 NULL 值存储在数据库中?

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

Is it possible to store '' (empty string) as a non NULL value in the database?

sqloracleoracle11g

提问by RHT

I am using Oracle DB. At the database level, when you set a column value to either NULL or '' (empty string), the fetched value is NULL in both cases. Is it possible to store '' (empty string) as a non NULL value in the database?

我正在使用 Oracle 数据库。在数据库级别,当您将列值设置为 NULL 或 ''(空字符串)时,两种情况下获取的值都是 NULL。是否可以将 ''(空字符串)作为非 NULL 值存储在数据库中?

I execute this

我执行这个

UPDATE contacts SET last_name = '' WHERE id = '1001';

commit;

SELECT last_name, ID FROM contacts WHERE id ='1001';

LAST_NAME                  ID
------------               ------
null                       1001

Is it possible to store the last_name as a non-NULL empty string ('')?

是否可以将 last_name 存储为非 NULL 空字符串 ('')?

采纳答案by recursive

The only way to do this in oracle is with some kind of auxiliary flag field, that when set is supposed to represent the fact that the value should be an empty string.

在 oracle 中做到这一点的唯一方法是使用某种辅助标志字段,当设置时应该表示值应该是空字符串的事实。

回答by recursive

Oracle has a well know behavior that it silently converts ""to NULLon INSERT and UPDATE statements.

Oracle 有一个众所周知的行为,它""NULL在 INSERT 和 UPDATE 语句上默默地转换为。

You have to deal with this in your code to prevent this behavior by converting NULLto ""when you read the columns back in and just do not use nullin your program to begin with.

你必须在你的代码中处理这个问题,以通过转换NULL""当你读回列时防止这种行为,只是不要null在你的程序中使用。

回答by wallenborn

As far as i know Oracle does not distinguish between '' and NULL, see here.

据我所知,Oracle 不区分 '' 和 NULL,请参见此处。

回答by Joe

A long time since I used Oracle, but I believe we used to use a single space ' ' to represent an empty string, then trim it after reading.

好久没用Oracle了,不过我相信我们以前都是用一个空格''来代表一个空字符串,看完之后再修整。