postgresql数据库对json数据的处理

1. 两种数据形式存储 json,jsonb

(1)json存储快,使用慢; 存的时候不做处理,使用时再解析
(2)jsonb存储稍慢,存储时就做了解析,使用时速度较快
(3)两者的部分函数很相似,稍有区别

2.使用例子

(1)创建学生表
CREATE TABLE if not exists public.student
        (
          name varchar,
          user_card varchar,
          age int,
          info jsonb,
          CONSTRAINT student_pkey PRIMARY KEY (user_card)
        );
        insert into public.student values('tom', '1234567890', 11,'{"key1":[1,2,4,["a","b","c"]],"key2":[2,5,7,["a","d","e"]]}');
        insert into public.student values('katty', '1234509890', 20,'{"key1":[8,10,4,["j","n","c"]],"key2":[9,5,19,["a","m","e"]]}');
(2)根据条件获取json
select info from public.student where name='tom'

+————————————————————————–+
| info |
+————————————————————————–+
| {“key1”: [1, 2, 4, [“a”, “b”, “c”]], “key2”: [2, 5, 7, [“a”, “d”, “e”]]} |
+————————————————————————–+

(3) 根据key获取数据
select info->'key1'  as info_list from public.student;

+—————————–+
| info_list |
+—————————–+
| [1, 2, 4, [“a”, “b”, “c”]] |
| [8, 10, 4, [“j”, “n”, “c”]] |
+—————————–+
2 rows in set

你可能感兴趣的:(PostgreSQL)