当前位置: 首页 > news >正文

03-PGDataType

DataType

数据类型 表示
布尔类型 boolean
数值类型 smallint(2),integer(4),bigint(8)
浮点型 decimal,numeric,real(float),double precision(double),money
字符串 varchar(n),char(n),text
时间 date(年月时),time(时分秒),timestamp(年月时时分秒)
二进制类型 bytea
位图 bit(n)(定长位图),bit-varying(n)(变成位图)
枚举类型 enum
几何类型 点,直线,圆...
数组类型 在类型后添加[]
JSON类型 json(存储json文本),jsonb(二进制json-可加索引)
IP类型 cidr(存储ip地址)

注意

pgsql中, 单引号标注具体的数值,比如'北京',双引号类似mysql的``标识一个关键字。

select 1.414,'北京',"墨西哥";-- 墨西哥这里就会报错

类型转换

select 类型 值 ;

select bit '010101';

select 值::类型;

select '010101'::bit(6);
select '2011-10-11'::date;

select cast(类型 值 类型);

select cast(text '010101' as bit(6)); 

布尔类型

可以存储三个值,true,false,NULL.

但是pgsql的boolean并不严格匹配,允许我们做一些‘出格’的匹配。

比如

-- OK
select true,false;

那如果是True,False,甚至y,n,1,0等都能转换成true,false.

select true,True,TRUE,'y'::boolean,'yes'::boolean,'yeal'::boolean,'YES'::boolean,'1'::boolean,NULL::boolean;
字段A 字段B and or
true true true false
true false false true
true NULL NULL true
false false false false
false NULL false NULL
NULL NULL NULL NULL

数值类型

整形

smallint,int2,integer,int4,bigint,int8

操作符 描述 实例 结果
^ 2 ^ 3 8
|/ 平方根 |/36 6
@ 绝对值 @ -24 24
& 31 & 16 16
| 31 | 32 63
>> 右移 2 >> 1 0
<< 左移 2 << 2 8

函数

-- 3.1415....
pi()
-- 小数点位数
round(n,m)
-- 向上取整
ceil()
-- 向下取整
floor()

浮点类型

decimal(n,m),numeric(n,m)

序列

序列是什么?

简单理解:

  • 就像一个数字生成器,每次调用返回下一个数字
  • 保证数字唯一且递增
  • 独立于表存在,可以被多个表共用

创建序列

create sequence public.table_id_seq;-- 查询和更新
select currval('public.table_id_seq');
select nextval('public.table_id_seq');

使用序列作为表的主键

-- 比较麻烦,每个表创建一个序列
create table public.xxx(id int8 default nextval('public.table_id_seq'));-- 直接使用序列类型,有smallserial,serial,bigserial
create table public.xxx(id serial);

字符串

  • char ,定长字符串,最大1个G
  • varchar(n),最大1个G
  • text,很长的字符串.

常用函数

||
concat
char_length
octet_length
position
substring
trim
upper
lower

image-20260101165559373

日期类型

select '2022-11-20 15:22:57'::timestamp;select time '15:22:57';select date '2045-11-20';select now()::timestamp;select date '2011-10-10' + 10; # 默认单位加天select date '2011-10-10' + time '10:11:22';select timestamp '2011-10-10 10:10:10' + interval '1month';

枚举类型

-- 声明一个星期的枚举
create type week as enum ('Mon','Tus','Wed','Thu','Fri','Sat','Sun');-- 使用枚举
create table public.test(id bigserial,weekday week
);-- 使用week
insert into public.test (weekday) values('Sun');

IP 类型

select '127.0.0.1'::cidr;--  报错,可以检验
select '192.168.2.256'::cidr;-- 范围查询
select ip from table between '192.168.1.1' and '192.168.1.10'; 

JSON & JSONB类型

# json和jsonb的使用几乎没区别,
# 存储上,jsonb是二进制的json类型,但是支持索引,自动去除多余索引,不保证原有顺序
# json可以存储重复的key,但是以最后一个为准,但是jsonb不会保存多余的重复keyselect '9'::json,'null'::json,'"dagouxiong"'::json,'true'::json;select '9'::jsonb,'null'::jsonb,'"dagouxiong"'::jsonb,'true'::jsonb; select '[9,true,null,"asdas"]'::json;select '{"name":"Tom","age":16,"birthday":"2011-10-10"}'::json;select '{"name":"Tom","age":16,"birthday":"2011-10-10"}'::jsonb;
create table json_table(id bigserial,info json,infob jsonb
);create index json_index on json_table(info); -- 报错
create index json_indexb on json_table(infob); -- 正确
JSON PGSQL
string text
number numberic
boolean boolean
null (none)

复合类型

类比一个model对象。

-- 考虑如下结构,只表示结构
User{Id id;Info info;
}
Info{string name;Integer age;
}-- 使用pgsql的复合类型先映射Info,再映射User
create type type_nfo as (name varchar(32),age int);
create table public.user(id serial,info type_info
);-- 添加数据-> ()
insert into public.user (info) values(('Hah',23));

数组类型

create table public.test(id serial,col1 int[],col2 int[2],col3 int[][]
);select '{how,are,you}'::varchar[];insert into public.test(col1,col2,col3) values ('{1,2,3}','{2,3,4}',array[[2,2,4],[1,2,3]]);insert into public.test(col1,col2,col3) values ('{1,2,3}','{2,3,4}','{{2,2,4},{1,2,3}}');-- 当存储的varchar内容等存在单引号,我们需要用两个单引号代替一个单引号
select '{''how'',are}'::varchar[];-- ","使用双引号包裹/使用\转义
select '{"how,are",sad}'::varchar[];
select '{how\,are,sad}'::varchar[];-- 有双引号
select '{\"how\",are}'::varchar[];

数据类型数组的比较

@> 包含

<@ 被包含

&& 比较相同

select array[1,2,3] @> array[1];select array[1,2] <@ [2,3,4];select array[1,2] && array[2,1];
http://www.jsqmd.com/news/177814/

相关文章:

  • 全网最全9个AI论文写作软件,专科生毕业论文必备!
  • 04-PGConstraint
  • 大数据时代 RabbitMQ 助力数据高效分发
  • 学霸同款2025 AI论文平台TOP9:专科生毕业论文全攻略
  • 基于流体输配管网教材的液冷服务器管道设计方案建议
  • 在 Web 前端实现流式 TTS 播放
  • 在 Web 前端实现流式 TTS 播放
  • Dreams in Pursuit 2025
  • 全网最全8个AI论文网站,专科生轻松搞定毕业论文!
  • 2025再见,码农当自强,47岁尚能饭否
  • 2025再见,码农当自强,47岁尚能饭否
  • springboot大学社团管理系统
  • python餐厅点餐及餐桌推荐系统vue
  • [免费]基于Python的Django+Vue3在线商城系统(简易版)【论文+源码+SQL脚本】
  • python高校班主任辅导员管理系统vue
  • Zookeeper集群数据是如何同步的?
  • tsgqec.dll文件损坏丢失找不到 打不开程序 下载方法
  • springboot的企业it资产管理系统--论文
  • 跨年夜武侠风拉满!周小飞林子祥共创《咏春》舞台名场面
  • 机器人工程毕设 基于单片机的太阳追光系统(源码+硬件+论文)
  • 安全验证技术与反爬虫防护机制解析
  • springboot大学生心理健康管理系统
  • a and b are not such bad
  • the way of Koreans to speak English
  • 打工人上班摸魚小說 -第四章 误会升级、深夜办公室与神秘U盘
  • 还在用AI写论文被查AIGC?8款神器维普查重一把过!
  • a and b are not such bad, historically
  • 全网最全MBA必备!10个一键生成论文工具深度测评
  • YOLOFuse镜像亮点解析:三种融合策略对比,适配不同显存与精度需求