PostgreSQL 9.3.2 Json類型使用

來源:互聯網
上載者:User

一、建立測試表


CREATE TABLE job
(
 jobid SERIAL primary key ,
 jobdesc json
)


二、選擇符


->(返回物件類型)
->>(返回字元型)
#>>(選擇數群組類型)


三、插入選擇更新刪除操作記錄


1.插入記錄


insert into job(jobdesc) values('{ 
  "jobname":"linux_os_vmstat",
  "schedule":{
      "type":{"interval":
          "5m"
      },
      "start":"now",
      "end":"None"
  },
  "values":{
      "event":["cpu_r","cpu_w"],
      "data":["cpu_r"],
      "threshold":[1,1]
  },
  "objects":{
      "wintest1":"cpu"
  }
}');


insert into job(jobdesc) values('{ 
  "jobname":"Oracle_tbs_space",
  "schedule":{
      "type":{"interval":
          "1d"
      },
      "start":"now",
      "end":"None"
  },
  "values":{
      "event":["used"],
      "data":["used"],
      "threshold":["90%"]
  },
  "objects":{
      "wintest1":"oradb1"
  }
}');


2.選擇記錄


# select jobdesc->>'jobname' as jobname from job where jobdesc->'objects'->>'wintest1' like 'oradb1';
    jobname
------------------
oracle_tbs_space
(1 行記錄)


# select jobdesc->'objects' as objects from job where jobdesc->>'jobname' = 'linux_os_vmstat';
        objects
--------------------------
{                      +
        "wintest1":"cpu"+
    }
(1 行記錄)


#select jobdesc->'values'#>>'{threshold,0}' from job where jobdesc->>'jobname' = 'oracle_tbs_space';


數組元素選擇


# select jobdesc->'values'#>>'{event,0}' as value1 from job where jobdesc->>'jobname' = 'linux_os_vmstat';
value1
--------
cpu_r
(1 行記錄)


# select jobdesc->'values'#>>'{event,1}' as value2 from job where jobdesc->>'jobname' = 'linux_os_vmstat';
value2
--------
cpu_w
(1 行記錄)

 


2.更新記錄


#update job  set jobdesc = '{ 
  "jobname":"linux_os_vmstat",
  "schedule":{
      "type":{"interval":
          "5m"
      },
      "start":"now",
      "end":"None"
  },
  "values":{
      "event":["cpu_r","cpu_w"],
      "data":["cpu_r"],
      "threshold":[1,2]
  },
  "objects":{
      "wintest1":"cpu"
  }
}' where jobdesc->>'jobname' = 'linux_os_vmstat';
UPDATE 1
# select jobdesc->'values'#>>'{threshold,1}' as threshold2 from job
where jobdesc->>'jobname' = 'linux_os_vmstat';
threshold2
------------
2
(1 行記錄)


更新json類型欄位時必須整個欄位都更新,無法採用指定內部特定值方法更新。


3.刪除記錄


# select * from job;
jobid |              jobdesc
-------+------------------------------------
    3 | {                                +
      |    "jobname":"oracle_tbs_space", +
      |    "schedule":{                  +
      |        "type":{"interval":      +
      |            "1d"                  +
      |        },                        +
      |        "start":"now",            +
      |        "end":"None"              +
      |    },                            +
      |    "values":{                    +
      |        "event":["used"],        +
      |        "data":["used"],          +
      |        "threshold":["90%"]      +
      |    },                            +
      |    "objects":{                  +
      |        "wintest1":"oradb1"      +
      |    }                            +
      | }
    4 | {                                +
      |    "jobname":"linux_os_vmstat",  +
      |    "schedule":{                  +
      |        "type":{"interval":      +
      |            "5m"                  +
      |        },                        +
      |        "start":"now",            +
      |        "end":"None"              +
      |    },                            +
      |    "values":{                    +
      |        "event":["cpu_r","cpu_w"],+
      |        "data":["cpu_r"],        +
      |        "threshold":[1,1]        +
      |    },                            +
      |    "objects":{                  +
      |        "wintest1":"cpu"          +
      |    }                            +
      | }
(2 行記錄)
#
# delete from job where jobdesc->>'jobname' = 'linux_os_vmstat';
DELETE 1
# select * from job;
jobid |              jobdesc
-------+-----------------------------------
    3 | {                                +
      |    "jobname":"oracle_tbs_space",+
      |    "schedule":{                +
      |        "type":{"interval":      +
      |            "1d"                  +
      |        },                      +
      |        "start":"now",          +
      |        "end":"None"            +
      |    },                          +
      |    "values":{                  +
      |        "event":["used"],        +
      |        "data":["used"],        +
      |        "threshold":["90%"]      +
      |    },                          +
      |    "objects":{                  +
      |        "wintest1":"oradb1"      +
      |    }                            +
      | }
(1 行記錄)
#

PostgreSQL 的詳細介紹:請點這裡
PostgreSQL 的:請點這裡

PostgreSQL緩衝詳述

Windows平台編譯 PostgreSQL

Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)環境的配置與安裝

Ubuntu上的phppgAdmin安裝及配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.