布尔类型
boolean的值要么是true,要么是false,如果是unknown状态,用NULL表示。
boolean在SQL中可以用不带引号的TRUE或FALSE表示,也可以用其他表示“真”和“假”的带引号字符表示,如true、false、yes、no等等。
create table t(id int,col1 boolean,col2 text);
insert into t values (1,true,'TRUE');
insert into t values (2,false,'FALSE');
insert into t values (3,true,'tRue');
insert into t values (4,false,'fAlse');
insert into t values (5,'tRuE','''tRuE''');
insert into t values (6,'fAlsE','''fAlsE''');
insert into t values (7,'true','''true''');
insert into t values (8,'false','''false''');
insert into t values (9,'t','''t''');
insert into t values (10,'f','''f''');
insert into t values (11,'y','''y''');
insert into t values (12,'n','''n''');
insert into t values (13,'yes','''yes''');
insert into t values (14,'no','''no''');
insert into t values (15,'1','''1''');
insert into t values (16,'0','''0''');
select * from t;
select * from t where col1;
select * from t where not col1;
布尔类型的操作符
布尔类型可以使用的操作符
- 逻辑操作符
- AND、OR、NOT
- 比较操作符(常用IS这个比较运算符)
- expression IS TRUE
- expression IS NOT TRUE
- expression IS FALSE
- expression IS NOT FALSE
- expression IS UNKNOWN
- expression IS NOT UNKNOWN
PG中“a AND b”与“b AND a”的结果是相同的。
布尔AND、OR运算真值表
a | b | a AND b | a OR b |
---|---|---|---|
TRUE | TRUE | TRUE | TRUE |
TRUE | FALSE | FALSE | TRUE |
TRUE | NULL | NULL | TRUE |
FALSE | FALSE | FALSE | FALSE |
FALSE | NULL | FALSE | NULL |
NULL | NULL | NULL | NULL |
NOT运行真值表
a | NOT a |
---|---|
TRUE | FALSE |
FALSE | TRUE |
NULL | NULL |