Oracle database 23c 引入了新的 SQL 布尔(Boolean)数据类型,可以像其他内置类型一样使用。实际上,PL/SQL 则在很多年前已经提供了布尔数据类型的支持。
布尔字段
布尔数据类型使用关键字 BOOLEAN 或者 BOOL 指定,例如:
drop table if exists boolean_test purge;
create table boolean_test (
id number generated always as identity,
active boolean,
archived bool
);
其中,drop table if exists 也是这个版本新增的功能。
布尔数据的赋值方式有很多,以下语句都可以将 active 标识设置为 true,同时将 archived 标识设置为 false。
insert into boolean_test (active, archived) values (true, false);
insert into boolean_test (active, archived) values (TRUE, FALSE);
insert into boolean_test (active, archived) values ('true', 'false');
insert into boolean_test (active, archived) values ('TRUE', 'FALSE');
insert into boolean_test (active, archived) values ('yes', 'no');
insert into boolean_test (active, archived) values ('YES', 'NO');
insert into boolean_test (active, archived) values ('on', 'off');
insert into boolean_test (active, archived) values ('ON', 'OFF');
insert into boolean_test (active, archived) values (1, 0);
insert into boolean_test (active, archived) values ('1', '0');
insert into boolean_test (active, archived) values ('t', 'f');
insert into boolean_test (active, archived) values ('T', 'F');
insert into boolean_test (active, archived) values ('y', 'n');
insert into boolean_test (active, archived) values ('Y', 'N');
commit;
查询一下数据:
select * from boolean_test;
ID ACTIVE ARCHIVED
---------- ----------- -----------
1 TRUE FALSE
2 TRUE FALSE
3 TRUE FALSE
4 TRUE FALSE
5 TRUE FALSE
6 TRUE FALSE
7 TRUE FALSE
8 TRUE FALSE
9 TRUE FALSE
10 TRUE FALSE
11 TRUE FALSE
12 TRUE FALSE
13 TRUE FALSE
14 TRUE FALSE
14 rows selected.
旧版本的 SQL*Plus 以及 SQLcl 23.1 之前的版本(包含)都会将 true 显示为 1,false 显示为 0。因为它们使用的驱动不支持 true/false。更高的版本则会正常显示。
布尔类型的字段也支持空值,除非定义了 NOT NULL 约束。
insert into boolean_test (active, archived) values (null, null);
commit;
select *
from boolean_test
where active is null;
ID ACTIVE ARCHIVED
---------- ----------- -----------
15
delete from boolean_test
where active is null;
commit;
布尔字段不仅可以定义 NOT NULL 以及 CHECK 约束,也可以作为 UNIQUE、PRIMARY KEY 或者 FOREIGN KEY 约束的一部分。
SQL 支持
布尔表达式可以像其他表达式一样使用。以下是一些示例:
select count(*)
from boolean_test
where active;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active is true;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active and not archived;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active or archived;
COUNT(*)
----------
14
select count(*)
from boolean_test
where active is null;
COUNT(*)
----------
0
select count(*)
from boolean_test
where active > archived;
COUNT(*)
----------
14
新版本还增加了一个 TO_BOOLEAN 函数,可以将数字或者字符转换为布尔值。同时,重载了支持布尔类型的 TO_NUMBER、TO_CHAR 以及 TO_NCHAR 函数。
select to_boolean('true'),
to_boolean('false'),
to_number(active),
to_number(archived),
to_char(active),
to_char(archived),
to_nchar(active),
to_nchar(archived)
from boolean_test
where id = 1;
TO_BOOLEAN( TO_BOOLEAN( TO_NUMBER(ACTIVE) TO_NUMBER(ARCHIVED) TO_CH TO_CH TO_NC TO_NC
----------- ----------- ----------------- ------------------- ----- ----- ----- -----
TRUE FALSE 1 0 TRUE FALSE TRUE FALSE
PL/SQL 支持
PL/SQL 很早就已经支持了布尔类型,新版本可以支持布尔数据的持久化。
扫描二维码关注公众号,回复:
15460209 查看本文章
declare
l_active boolean := true;
l_archived boolean := false;
begin
insert into boolean_test (active, archived)
values (l_active, l_archived);
commit;
end;
/
PL/SQL procedure successfully completed.
上文提到的 TO_BOOLEAN 函数也可以在 PL/SQL 中使用。
declare
l_true boolean;
l_false boolean;
begin
-- Work
l_true := to_boolean('true');
l_false := to_boolean('false');
end;
/
其他重载的转换函数默认不能在 PL/SQL 中使用,而是需要在系统或者会话级别将初始化参数 PLSQL_IMPLICIT_CONVERSION_BOOL 设置为 true。
alter session set plsql_implicit_conversion_bool=true;
declare
l_true boolean := true;
l_false boolean := false;
l_number number;
begin
l_number := to_number(l_true);
l_number := to_number(l_false);
end;
/
declare
l_true boolean;
l_false boolean;
l_string varchar2(5);
begin
l_string := to_char(l_true);
l_string := to_char(l_false);
end;
/
declare
l_true boolean;
l_false boolean;
l_string nvarchar2(5);
begin
l_string := to_nchar(l_true);
l_string := to_nchar(l_false);
end;
/