布尔类型(boolean)的值有三种:TRUE、FALSE和NULL。其中NULL表示未知状态(unknown)。
boolean在SQL中,可以用不带引号的TRUE、FALSE表示,也可以用表示真假的带引号的字符表示,如:'yes'、'no'、'1'、'0'。
1.建表
CREATE TABLE test (
id int,
coll boolean,
collName varchar(20)
);
2.插入数据
insert into test values(1,TRUE,'TRUE');
insert into test values(2,FALSE,'FALSE');
insert into test values(3,true,'true');
insert into test values(4,false,'false');
insert into test values(5,'yes','''yes''');
insert into test values(6,'no','''no''');
insert into test values(7,'1','''1''');
insert into test values(8,'0','''0''');
3.查询数据
select * from test;
4.操作符
布尔类型可以使用逻辑操作符和比较操作符。
常用的逻辑操作符有:AND、OR、NOT。
TRUE和FALSE的计算规则比较熟悉,不再重复。现在总结一个NULL的计算规则。
a | b | a AND b | a OR b |
---|---|---|---|
TRUE | NULL | NULL | TRUE |
FALSE | NULL | FALSE | NULL |
NULL | NULL | NULL | NULL |
常用的比较操作符有IS
expression IS TRUE;
expression IS FALSE;
expression IS UNKNOWN;
select (1>2) IS UNKNOWN;