如何更新 Oracle 表中的布尔字段

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

How to update a boolean field in Oracle table

sqloracleoracle11goracle10gsql-update

提问by HereToLearn_

I am a novice Oracle user. I wanted to update a boolean field in my table for one of the record. Which one of these statements is correct ?

我是一个新手 Oracle 用户。我想为其中一条记录更新表中的布尔字段。以下哪一项是正确的?

update MyTable set myBooleanColumn = 1 where UserId= 'xx12345';

or 


update MyTable set myBooleanColumn = '1' where UserId= 'xx12345';

any help is greatly appreciated!! thanks !

任何帮助是极大的赞赏!!谢谢 !

回答by DHW

It depends on how the field is defined.

这取决于如何定义字段。

If its defined as a CHAR(1) field, then you can store 'Y'/'N' or 'T'/'F' in it. To update the field, you'd use the quotes as it would be a string literal.

如果将其定义为 CHAR(1) 字段,则您可以在其中存储 'Y'/'N' 或 'T'/'F'。要更新该字段,您将使用引号,因为它是字符串文字。

UPDATE TestTable set myCharBooleanColumn = 'Y';

If the field is defined as NUMERIC, then the convention is 0=false and 1 or -1 is true (I've seen both).

如果该字段定义为 NUMERIC,则约定为 0=false,1 或 -1 为 true(我都见过)。

UPDATE TestTable set myNumericBooleanColumn = 1;

Many people will advocate the CHAR(1) approach, but in the real world - you see both. It depends on how the boolean is implemented.

许多人会提倡 CHAR(1) 方法,但在现实世界中 - 您会看到两者。这取决于布尔值的实现方式。

You can read more in Oracle's docs on Datatypes http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm

您可以在 Oracle 的数据类型文档http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm 中阅读更多内容

回答by Ben

There is no such thing as a Boolean field in Oracle so your field is either a numeric field or a character field. If it's a numeric field you don't need to quote the number; if it's a character field you should quote the string.

Oracle 中没有布尔字段这样的东西,因此您的字段要么是数字字段,要么是字符字段。如果是数字字段,则不需要引用该数字;如果它是一个字符字段,您应该引用该字符串。

You can find out the type of the column by querying USER_TAB_COLUMNS:

您可以通过查询USER_TAB_COLUMNS找出列的类型:

select *
  from user_tab_columns
 where table_name = 'MYTABLE'
   and column_name = 'MYBOOLEANCOLUMN'

or by describingthe table.

或通过描述表格。

回答by Rahul Tripathi

There is nothing as Boolean field in Oracle.

Oracle 中没有布尔字段。

The best what you can do is to create the table like this:-

您能做的最好的事情就是像这样创建表格:-

create table ABC(bool char(1) check (bool in ('N','Y'));

Then simple update like

然后简单的更新就像

UPDATE ABC set bool = 'Y';

WHY TAKING CHAR?

为什么要使用字符?

There is no support for BOOLEAN, BIT, or TINYINTdata types so charwould be the best as it takes 1 byte

不支持BOOLEAN、BIT 或 TINYINT数据类型,因此char将是最好的,因为它需要 1 个字节