PostgreSQL相对于其余数据库,支持数据类型不少。sql
PostgreSQL数据类型有布尔类型、整数类型、字符串类型、二进制字符串类型、位串类型、时间与日期类型、枚举类型、几何类型、网络地址类型、数组类型、复合类型、XML类型、json类型、range类型、对象标识符类型、伪类型和其余类型。数据库
为提升SQL语句兼容性,部分数据类型还有别名,例如integer类型,能够用int、int4表示,smallint类型能够用int2表示等。具体状况见下图json
简单数据能够直接查询输出。复杂类型能够用类型名加单引号进行输入。数组
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=#
布尔类型只有两种状态,分别是true(真)和false(假)。函数
建立布尔类型测试表并插入数据。post
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。测试
布尔类型可用操做符有逻辑运算符和比较运算符。postgresql
经常使用逻辑操做符有and、or和not。code
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=#