字符串类型用的是最多的一种,在PGSQL里,主要支持三种:
操作没什么说的,但是字符串常见的函数特别多。
字符串的拼接一要要使用||来拼接。
完整的函数介绍可查看下面链接:
http://www.postgres.cn/docs/12/functions-string.html
在PGSQL中,核心的时间类型,就三个。
在PGSQL中,声明时间的方式。
只需要使用字符串正常的编写 yyyy-MM-dd HH:mm:ss 就可以转换为时间类型。
直接在字符串位置使用之前讲到的数据类型转换就可以了。
当前系统时间 :
select timestamp 'now';
-- 直接查询now,没有时区的概念
select time with time zone 'now' at time zone '08:00:00'
日期类型的运算
select date '2011-11-11' + time '12:12:12' ;
select timestamp '2011-11-11 12:12:12' + interval '1day' + interval '1minute' + interval '1month';
枚举类型MySQL也支持,只是没怎么用,PGSQL同样支持这种数据类型
可以声明枚举类型作为表中的字段类型,这样可以无形的给表字段追加诡异的规范。
-- 声明一个星期的枚举,值自然只有周一~周日。
create type week as enum ('Mon','Tues','Sun');
-- 声明一张表,表中的某个字段的类型是上面声明的枚举。
drop table test;
create table test(
id bigserial ,
weekday week
);
insert into test (weekday) values ('Mon');
insert into test (weekday) values ('Fri');
PGSQL支持IP类型的存储,支持IPv4,IPv6这种,甚至Mac内种诡异类型也支持
这种IP类型,可以在存储IP时,帮助做校验,其次也可以针对IP做范围查找。
IP校验的效果
IP也支持范围查找。
JSON在MySQL8.x中也做了支持,但是MySQL支持的不好,因为JSON类型做查询时,基本无法给JSON字段做索引。
PGSQL支持JSON类型以及JSONB类型。
JSON和JSONB的使用基本没区别。
撇去JSON类型,本质上JSON格式就是一个字符串,比如MySQL5.7不支持JSON的情况的下,使用text也可以,但是字符串类型无法校验JSON的格式,其次单独的字符串没有办法只获取JSON中某个key对应的value。
JSON和JSONB的区别:
JSON中key对应的value的数据类型
JSON | PGSQL |
---|---|
String | text |
number | numeric |
boolean | boolean |
null | (none) |
[
{"name": "张三"},
{"name": {
"info": "xxx"
}}
]
操作JSON:
select '9'::JSON,'null'::JSON,'"laozheng"'::JSON,'true'::json;
select '9'::JSONB,'null'::JSONB,'"laozheng"'::JSONB,'true'::JSONB;
select '[9,true,null,"我是字符串"]'::JSON;
select '{"name": "张三","age": 23,"birthday": "2011-11-11","gender": null}'::json;
select '{"name": "张三","age": 23,"birthday": "2011-11-11","gender": null}'::jsonb;
create table test(
id bigserial,
info json,
infob jsonb
);
insert into
test
(info,infob)
values
('{"name": "张三" ,"age": 23,"birthday": "2011-11-11","gender": null}',
'{"name": "张三" ,"age": 23,"birthday": "2011-11-11","gender": null}')
select * from test;
create index json_index on test(info);
create index jsonb_index on test(infob);
JSON还支持很多函数,可以直接查看以下文档地址:
http://www.postgres.cn/docs/12/functions-json.html
复合类型就好像Java中的一个对象,Java中有一个User,User和表做了一个映射,User中有个人信息对象。可以基于符合类型对映射上个人信息。
public class User{
private Integer id;
private Info info;
}
class Info{
private String name;
private Integer age;
}
按照上面的情况,将Info构建成一个复合类型
-- 构建复合类型,映射上Info
create type info_type as (name varchar(32),age int);
-- 构建表,映射User
create table tb_user(
id serial,
info info_type
);
-- 添加数据
insert into tb_user (info) values (('张三',23));
insert into tb_user (info) values (('露丝',233));
insert into tb_user (info) values (('jack',33));
insert into tb_user (info) values (('李四',24));
select * from tb_user;
数组还是要依赖其他类型,比如在设置住址,住址可能有多个住址,可以采用数组类型去修饰字符串。
PGSQL中,指定数组的方式就是[],可以指定一维数组,也支持二维甚至更多维数组。
构建数组的方式:
drop table test;
create table test(
id serial,
col1 int[],
col2 int[2],
col3 int[][]
);
-- 构建表指定数组长度后,并不是说数组内容只有2的长度,可以插入更多数据
-- 甚至在你插入数据,如果将二维数组结构的数组扔到一维数组上,也可以存储。
-- 数组编写方式
select '{{how,are},{are,you}}'::varchar[];
select array[[1,2],[3,4]];
insert into test (col1,col2,col3) values ('{1,2,3}','{4,5,6}','{7,8,9}');
insert into test (col1,col2,col3) values ('{1,2,3}','{4,5,6}',array[[1,2],[3,4]]);
insert into test (col1,col2,col3) values ('{1,2,3}','{4,5,6}','{{1,2},{3,4}}');
select * from test;
如果现在要存储字符串数组,如果存储的数组中有双引号怎么办,有大括号怎么办。
-- 如果存储的数组中的值,有单引号怎么办?
-- 使用两个单引号,作为一个单引号使用
select '{''how''}'::varchar[];
-- 如果存储的数组中的值,有逗号怎么办?(PGSQL中的数组索引从1开始算,写0也是从1开始算。)
-- 用双引号将数组的数据包起来~
select ('{"how,are"}'::varchar[])[2];
-- 如果存储的数组中的值,有双引号怎么办?
-- 如果要添加双引号,记得转义。
select ('{"\"how\",are"}'::varchar[])[1];
数组的比较方式
-- 包含
select array[1,2] @> array[1];
-- 被包含
select array[1,2] <@ array[1,2,4];
-- 是否有相同元素
select array[2,4,4,45,1] && array[1];
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。