SQL Oracle 数据库中是否有任何布尔类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3726758/
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 there any boolean type in Oracle databases?
提问by Peder
Is there any Boolean type in Oracle databases, similar to the BIT
datatype in Ms SQL Server?
Oracle 数据库中是否有类似于BIT
Ms SQL Server 中的数据类型的布尔类型?
回答by Erich Kitzmueller
Not only is the boolean datatype missing in Oracle's SQL (not PL/SQL), but they also have no clear recommendation about what to use instead. See this threadon asktom. From recommending CHAR(1) 'Y'/'N'
they switch to NUMBER(1) 0/1
when someone points out that 'Y'/'N'
depends on the English language, while e.g. German programmers might use 'J'/'N'
instead.
不仅 Oracle 的 SQL(不是 PL/SQL)中缺少布尔数据类型,而且他们也没有明确建议使用什么来代替。在 asktom 上看到这个线程。从推荐CHAR(1) 'Y'/'N'
他们切换到NUMBER(1) 0/1
当有人指出这'Y'/'N'
取决于英语,而例如德国程序员可能会使用'J'/'N'
。
The worst thing is that they defend this stupid decision just like they defend the ''=NULL
stupidity.
最糟糕的是,他们为这个愚蠢的决定辩护,就像他们为''=NULL
愚蠢辩护一样。
回答by Bohdan
Nope.
不。
Can use:
可以使用:
IS_COOL NUMBER(1,0)
1 - true
0 - false
--- enjoy Oracle
--- 享受甲骨文
Or use char Y/N as described here
或者使用这里描述的char Y/N
回答by Alex Stephens
As per Ammoq and kupa's answers, We use number(1) with default of 0 and don't allow nulls.
根据 Ammoq 和 kupa 的回答,我们使用默认值为 0 的 number(1) 并且不允许空值。
here's an add columnto demonstrate:
这是一个添加列来演示:
ALTER TABLE YourSchema.YourTable ADD (ColumnName NUMBER(1) DEFAULT 0 NOT NULL);
Hope this helps someone.
希望这可以帮助某人。
回答by vc 74
Not at the SQL level and that's a pity There is one in PLSQL though
不是在 SQL 级别,这很遗憾虽然在 PLSQL 中有一个
回答by Roberto Góes
No, there isn't a boolean type in Oracle Database, but you can do this way:
不,Oracle 数据库中没有布尔类型,但您可以这样做:
You can put a check constraint on a column.
您可以在列上放置检查约束。
If your table hasn't a check column, you can add it:
如果您的表没有检查列,您可以添加它:
ALTER TABLE table_name
ADD column_name_check char(1) DEFAULT '1';
When you add a register, by default this column get 1.
添加寄存器时,默认情况下此列会获得 1。
Here you put a check that limit the column value, just only put 1 or 0
在这里你放了一个限制列值的检查,只放 1 或 0
ALTER TABLE table_name ADD
CONSTRAINT name_constraint
column_name_check (ONOFF in ( '1', '0' ));
回答by kupa
No there doesn't exist type boolean,but instead of this you can you 1/0(type number),or 'Y'/'N'(type char),or 'true'/'false' (type varchar2).
不,不存在类型布尔值,但您可以使用 1/0(类型编号)或“Y”/“N”(字符类型)或“真”/“假”(类型 varchar2)来代替它。
回答by Klaus Byskov Pedersen
There is a boolean type for use in pl/sql, but none that can be used as the data type of a column.
回答by Pranay Rana
A common space-saving trick is storing booleanvalues as an Oracle CHAR, rather than NUMBER:
一个常见的节省空间的技巧是将布尔值存储为 Oracle CHAR而不是 NUMBER:
回答by Filburt
Just because nobody mentioned it yet: using RAW(1) also seems common practice.
仅仅因为还没有人提到它:使用 RAW(1) 似乎也很常见。
回答by zloctb
DECLARE
error_flag BOOLEAN := false;
BEGIN
error_flag := true;
--error_flag := 13;--expression is of wrong type
IF error_flag THEN
UPDATE table_a SET id= 8 WHERE id = 1;
END IF;
END;