1 大多数人的选择
2 使用自定义类型处理
drop table if exists documents;drop type if exists ctdocument;create type ctdocument as (标题 text,作者 text,内容 text);create table documents(objectid bigserial not null, --编号doc ctdocument not null, --文档,注意类型为ctdocumentconstraint pk_documents_objectid primary key(objectid));
insert into documents(doc) values(('标题1','作者1','内容1')::ctdocument);insert into documents(doc) values(('标题2','作者2','内容2')::ctdocument);insert into documents(doc) values(('标题3','作者3','内容3')::ctdocument);insert into documents(doc) values(('标题4','作者4','内容4')::ctdocument);insert into documents(doc) values(('标题5','作者5','内容5')::ctdocument);--读取类型中的字段select objectid,(doc)."标题",doc from documents;--转换为jsonselect objectid,row_to_json(doc),doc from documents;
alter type ctdocument rename attribute "标题" to title;--这时读取类型中的字段要改用新的名字select objectid,(doc).title,doc from documents;--转换为json5.select objectid,row_to_json(doc),doc from documents;
alter type ctdocument add attribute "发布日期" timestamptz;alter type ctdocument add attribute "阅读数" integer;--列出类型定义\dS+ ctdocument;--重新插入一条数据insert into documents(doc) values(('标题6','作者6','内容6',now(),1)::ctdocument);--读取类型中的字段select objectid,(doc).title,doc from documents;--转换为json10.select objectid,row_to_json(doc),doc from documents;
alter type ctdocument drop attribute if exists "发布日期";--列出类型定义\dS+ ctdocument;--读取类型中的字段select objectid,(doc).title,doc from documents;--转换为jsonselect objectid,row_to_json(doc),doc from documents;
alter type ctdocument add attribute "发布日期" timestamptz;--注意,"发布日期"在2.3中已经被删除,然后重新添加的,此时的自定义类型顺序已经和2.2中不一样了,所以建议不要删除自定义类型的字段insert into documents(doc) values(('标题7','作者7','内容7',1,now())::ctdocument);alter type ctdocument alter attribute "发布日期" set data type date;
ERROR: cannot alter type "ctdocument" because column "documents.doc" uses it
3 自定义类型和json相互转换
select * from json_populate_record(null::ctdocument,null);select * from json_populate_record(null::ctdocument,(select row_to_json(doc) from documents where objectid=6));select (json_populate_record(null::ctdocument,row_to_json(doc)))::ctdocument as doc from documents;
--nullselect * from json_populate_recordset(null::ctdocument,null);--将表中的数据转换为json数组select array_to_json(array_agg(row_to_json(doc) order by objectid)) from documents;--将json数组转换为记录集显示select * from json_populate_recordset(null::ctdocument,(select array_to_json(array_agg(row_to_json(doc) order by objectid)) from documents));--转换为json然后再转换回来select (t1) from json_populate_recordset(null::ctdocument,(select array_to_json(array_agg(row_to_json(doc) order by objectid)) from documents)) as t1;
4 结束语
I Love PG
关于我们
中国开源软件推进联盟PostgreSQL分会(简称:中国PG分会)于2017年成立,由国内多家PostgreSQL生态企业所共同发起,业务上接受工信部中国电子信息产业发展研究院指导。中国PG分会是一个非盈利行业协会组织。我们致力于在中国构建PostgreSQL产业生态,推动PostgreSQL产学研用发展。
技术文章精彩回顾 PostgreSQL学习的九层宝塔 PostgreSQL职业发展与学习攻略 搞懂PostgreSQL数据库透明数据加密之加密算法介绍 一文读懂PostgreSQL-12分区表 一文搞懂PostgreSQL物化视图 PostgreSQL源码学习之:RegularLock Postgresql源码学习之词法和语法分析 2019,年度数据库舍 PostgreSQL 其谁? Postgres是最好的开源软件 PostgreSQL是世界上最好的数据库 从Oracle迁移到PostgreSQL的十大理由 从“非主流”到“潮流”,开源早已值得拥有 PostgreSQL国际专家系列直播:请选出你最想听的分享主题 PG活动精彩回顾 创建PG全球生态!PostgresConf.CN2019大会盛大召开 首站起航!2019“让PG‘象’前行”上海站成功举行 走进蓉城丨2019“让PG‘象’前行”成都站成功举行 中国PG象牙塔计划发布,首批合作高校授牌仪式在天津举行 群英论道聚北京,共话PostgreSQL 相聚巴厘岛| PG Conf.Asia 2019 DAY0、DAY1简报 相知巴厘岛| PG Conf.Asia 2019 DAY2简报 独家|硅谷Postgres大会简报 PostgreSQL线上沙龙第一期精彩回顾 PostgreSQL线上沙龙第二期精彩回顾 PostgreSQL线上沙龙第三期精彩回顾 PostgreSQL线上沙龙第四期精彩回顾 PostgreSQL线上沙龙第五期精彩回顾 PostgreSQL线上沙龙第六期精彩回顾 PG培训认证精彩回顾 中国首批PGCA认证考试圆满结束,203位考生成功获得认证! 中国第二批PGCA认证考试圆满结束,115位考生喜获认证! 重要通知:三方共建,中国PostgreSQL认证权威升级! 近500人参与!首次PGCE中级、第三批次PGCA初级认证考试落幕! 通知:PostgreSQL技术能力电子证书上线! 2020年首批 | 中国PostgreSQL初级认证考试圆满结束
最后修改时间:2020-06-04 09:22:16
文章转载自开源软件联盟PostgreSQL分会,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。





