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
Is it possible to store '' (empty string) as a non NULL value in the database?
提问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 NULL
on INSERT and UPDATE statements.
Oracle 有一个众所周知的行为,它""
会NULL
在 INSERT 和 UPDATE 语句上默默地转换为。
You have to deal with this in your code to prevent this behavior by converting NULL
to ""
when you read the columns back in and just do not use null
in your program to begin with.
你必须在你的代码中处理这个问题,以通过转换NULL
为""
当你读回列时防止这种行为,只是不要null
在你的程序中使用。
回答by wallenborn
回答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了,不过我相信我们以前都是用一个空格''来代表一个空字符串,看完之后再修整。