Postgresql学习笔记之——数据类型之布尔类型

Postgresql数据库中布尔数据类型。
一、布尔类型
在Postgresql数据库中Boolean的值除了“true”(真)、“false”(假),还有一个“unknown”(未知)状态。
如果是unknown时用NULL表示。布尔类型在Postgresql中可以用不带引号的TRUE或FALSE表示,也可以用更多表示真和假的带引号的字符表示:

备注
‘TRUE’‘FALSE’
tRuefAlse
‘tRue’‘fAlse’不分大小写
‘t’‘f’单一字符表示
‘yes’‘no’英文的是和否表示
‘y’‘n’yes和no的简写表示
‘1’‘0’1表示true,0表示false

以上表格中值加引号都可以表示真和假,但是在Postgresql中Boolean类型字段里存储的值还是 t (true)和 f(false)。
测试:

postgres=# create table tb_boolean(id int,col1 boolean, col2 text);
CREATE TABLE
postgres=# insert into tb_boolean VALUES(1,TRUE,'TRUE');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(1,FALSE,'FALSE');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(1,tRue,'tRue');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(2,fAlse,'fAlse');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(3,'tRue','''tRue''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(4,'fAlse','''fAlse''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(7,'t','''t''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(8,'f','''f''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(9,'no','''no''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(10,'yes','''yes''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(11,'y','''y''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(12,'n','''n''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(13,'1','''1''');
INSERT 0 1
postgres=# insert into tb_boolean VALUES(14,'0','''0''');
INSERT 0 1
postgres=# select * from tb_boolean ;
 id | col1 |  col2   
----+------+---------
  1 | t    | TRUE
  1 | f    | FALSE
  1 | t    | tRue
  2 | f    | fAlse
  3 | t    | 'tRue'
  4 | f    | 'fAlse'
  7 | t    | 't'
  8 | f    | 'f'
  9 | f    | 'no'
 10 | t    | 'yes'
 11 | t    | 'y'
 12 | f    | 'n'
 13 | t    | '1'
 14 | f    | '0'
(14 rows)

二、布尔类型的操作符
操作符分为逻辑操作符和比较操作符
1.逻辑操作符有:AND、OR、NOT。
SQL标准使用三种值的布尔逻辑:TRUE、FALSE和NULL,NULL代表unknown。

布尔的and、or逻辑运算:

aba AND ba OR b
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
TRUENULLNULLTRUE
FALSEFALSEFALSEFALSE
FALSENULLFALSENULL
NULLNULLNULLNULL

布尔的not运算:

aNOT a
TRUEFALSE
FALSETRUE
NULLNULL

逻辑操作符的两边值可以互换,结果是相同的。

布尔类型使用 IS 作为比较运算符,结果很直观,不多做介绍了。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以和你分享一些关于 PostgreSQL学习笔记。 1. PostgreSQL 是什么? PostgreSQL 是一种开源的关系型数据库管理系统,它支持 SQL 语言和 ACID 事务处理,并且具有丰富的特性和扩展性,可用于大型企业应用、Web 应用和嵌入式应用。 2. PostgreSQL 的特点 PostgreSQL 具有以下特点: - 开源免费 - 支持 ACID 事务处理 - 支持 SQL 语言 - 支持复杂查询和数据类型 - 支持存储过程和触发器 - 可以扩展和定制 - 可以跨平台运行 3. PostgreSQL 的安装 在 Windows 平台下,可以从官网下载 Windows 安装程序进行安装。在 Linux 平台下,可以使用包管理器安装,例如 Ubuntu 可以使用以下命令进行安装: ``` sudo apt-get install postgresql ``` 4. PostgreSQL 的基本操作 创建数据库: ``` CREATE DATABASE databasename; ``` 创建表: ``` CREATE TABLE tablename ( column1 datatype, column2 datatype, column3 datatype, .... ); ``` 插入数据: ``` INSERT INTO tablename (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` 查询数据: ``` SELECT column1, column2, ... FROM tablename WHERE condition; ``` 更新数据: ``` UPDATE tablename SET column1 = value1, column2 = value2, ... WHERE condition; ``` 删除数据: ``` DELETE FROM tablename WHERE condition; ``` 5. PostgreSQL 的扩展 PostgreSQL 可以通过扩展来增强其功能,例如: - PostGIS:空间数据处理扩展 - PL/Python:Python 语言扩展 - pgAdmin:可视化管理工具 - pgpool-II:连接池和负载均衡工具 以上就是一些关于 PostgreSQL 的基本学习笔记,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Major_ZYH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值