PostgreSQL数据类型-数据类型简介和布尔类型

PostgreSQL相对于其他数据库,支持数据类型很多。

PostgreSQL数据类型有布尔类型、整数类型、字符串类型、二进制字符串类型、位串类型、时间与日期类型、枚举类型、几何类型、网络地址类型、数组类型、复合类型、XML类型、json类型、range类型、对象标识符类型、伪类型和其他类型。

为提高SQL语句兼容性,部分数据类型还有别名,例如integer类型,可以用int、int4表示,smallint类型可以用int2表示等。具体情况见下图

postgresql数据类型

数据输入和转换

简单数据可以直接查询输出。复杂类型可以用类型名加单引号进行输入。

postgres=# select 1 AS "1",1.414 AS "Sqrt(2)!=!",'Hello,Tom';
 1 | Sqrt(2)!=! | ?column?
---+------------+-----------
 1 |      1.414 | Hello,Tom
(1 行记录)

#查询复杂类型
postgres=#

postgres=# select smallint '777';
 int2
------
  777
(1 行记录)

postgres=#
postgres=#
postgres=# select int '777';
 int4
------
  777
(1 行记录)

postgres=#

postgres=# select int '777' + smallint '666';
 ?column?
----------
     1443
(1 行记录)

postgres=#

PostgreSQL支持标准SQL类型转换函数CAST进行数据类型转换。也可以使用双冒号进行转换。

postgres=# select cast('2017' as int ),cast('2015-1-1' as date);
 int4 |    date
------+------------
 2017 | 2015-01-01
(1 行记录)

#双冒号转换
postgres=# select '777'::int,'2015-1-1'::date;
 int4 |    date
------+------------
  777 | 2015-01-01
(1 行记录)

postgres=#
#单引号可以省略
postgres=# select '0'::boolean,'0'::int;
 bool | int4
------+------
 f    |    0
(1 行记录)

postgres=#

Boolean类型(布尔类型)

布尔类型只有两种状态,分别是true(真)和false(假)。

创建布尔类型测试表并插入数据。

postgres=# create table testbool(id int,status boolean);
CREATE TABLE
postgres=#
postgres=# insert into testbool values(1,True),(1,True),(2,'True'),(3,'true'),(4,'0'),(5,'1'),(6,'t'),(7,'f');
INSERT 0 5
postgres=#
#查询结果为t或f。
postgres=# select * from testbool;
 id | status
----+--------
  1 | t
  1 | t
  2 | t
  3 | t
  4 | f
  5 | t
  6 | t
  7 | f
(8 行记录)

postgres=#

根据查询结果可知,布尔类型可以用带或者不带单引号不区分大小写true或false表示;0或1,t或f需要加单引号,0表示false,1表示true。

布尔类型可用操作符有逻辑运算符和比较运算符。

常用逻辑操作符有and、or和not。

SQL使用三个值,分别是true,false和null。null代表空值(什么都没有,不是0,也不是空格)。逻辑与和逻辑或运算前后两个操作对象位置可以互换,不影响运算结果。例如true and false 和false and true 查询结果相同。

不含null,2个值全部为真逻辑与运算结果为真,其他为假。

不含null,2个值全部为假逻辑或运算结果为假,其他为真。

true和null逻辑与运算结果为空,逻辑或运算结果为true。 false和null逻辑或运算结果为false,逻辑或运算结果为null。

两个空值逻辑或和逻辑与运算结果为null。

逻辑非运算也就是取反,非真为假,非假为真。下面看演示查询代码。

#bool数据类型练习
postgres=# select true and true,true and false,false and false;
 ?column? | ?column? | ?column?
----------+----------+----------
 t        | f        | f
(1 行记录)

postgres=# select true or true,true or false,false or false;
 ?column? | ?column? | ?column?
----------+----------+----------
 t        | t        | f
(1 行记录)

postgres=# select false and true,false or true;
 ?column? | ?column?
----------+----------
 f        | t
(1 行记录)

postgres=#

postgres=# select true and true,true and false,false and false;
 ?column? | ?column? | ?column?
----------+----------+----------
 t        | f        | f
(1 行记录)

postgres=# select true or true,true or false,false or false;
 ?column? | ?column? | ?column?
----------+----------+----------
 t        | t        | f
(1 行记录)

postgres=# select false and true,false or true;
 ?column? | ?column?
----------+----------
 f        | t
(1 行记录)

postgres=# select true and null,true or null;
 ?column? | ?column?
----------+----------
          | t
(1 行记录)

postgres=# select false and null,false or null;
 ?column? | ?column?
----------+----------
 f        |
(1 行记录)

postgres=# select true and null,false and null;
 ?column? | ?column?
----------+----------
          | f
(1 行记录)

postgres=# select not null;
 ?column?
----------

(1 行记录)

postgres=#

转载于:https://my.oschina.net/u/1011130/blog/1563271

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值