<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                {% raw %} # F.16\. hstore 這個模塊實現了`hstore`數據類型,在單個PostgreSQL值中存儲一組鍵/值對。 這在不同的場景中是有用的,如很少檢查帶有許多屬性的行,或半結構化的數據。 鍵和值是簡單的文本字符串。 ## F.16.1\. `hstore` 外部表示 `hstore`的文本表示用于輸入和輸出,包括零個或更多由逗號分開的 `_key_` `=&gt;` `_value_`對。一些例子: ``` k => v foo => bar, baz => whatever "1-a" => "anything at all" ``` 鍵/值對的順序不重要(可能不在輸出中復制)。忽略對和`=&gt;`符號周圍的空格。 包含空格、逗號、`=`或`&gt;`的鍵和值要加雙引號。 要在鍵或值中包含一個雙引號或反斜杠,要用反斜杠逃逸。 `hstore`中的每個鍵都是唯一的。如果你用重復鍵聲明一個`hstore`, 將只有一個存儲在`hstore`中,并且不保證會保存哪一個: ``` SELECT 'a=>1,a=>2'::hstore; hstore ---------- "a"=>"1" ``` 一個值(不是一個鍵)可以是SQL `NULL`。如: ``` key => NULL ``` `NULL`關鍵字是大小寫敏感的。要將`NULL` 當做普通的字符串來對待就要給`NULL`加雙引號。 > **Note:** 記住`hstore`文本格式,當用于輸入時,在任何請求的引用或逃逸_之前_應用。 如果通過一個參數傳遞一個`hstore`文本,那么不需要額外的處理。 但是如果作為引用的文本常量傳遞,那么任何單引號字符和(依賴于 `standard_conforming_strings`配置參數的設置)反斜杠字符需要正確的逃逸。 參閱[Section 4.1.2.1](#calibre_link-969)獲取更多處理字符串常量的信息。 在輸出時,鍵和值總是包含在雙引號中,即使并不嚴格需要也是這樣。 ## F.16.2\. `hstore` 操作符和函數 `hstore`模塊提供的操作符顯示在[Table F-6](#calibre_link-1550)中, 函數在[Table F-7](#calibre_link-1551)中。 **Table F-6\. `hstore` 操作符** | 操作符 | 描述 | 示例 | 結果 | | --- | --- | --- | --- | | `hstore` `-&gt;` `text` | 獲得鍵的值(如果不存在為`NULL`) | `'a=&gt;x, b=&gt;y'::hstore -&gt; 'a'` | `x` | | `hstore` `-&gt;` `text[]` | 獲得多個鍵的值(如果不存在為`NULL`) | `'a=&gt;x, b=&gt;y, c=&gt;z'::hstore -&gt; ARRAY['c','a']` | `{"z","x"}` | | `hstore` `&#124;&#124;` `hstore` | 連接 `hstore` | `'a=&gt;b, c=&gt;d'::hstore &#124;&#124; 'c=&gt;x, d=&gt;q'::hstore` | `"a"=&gt;"b", "c"=&gt;"x", "d"=&gt;"q"` | | `hstore` `?` `text` | `hstore` 包含鍵嗎? | `'a=&gt;1'::hstore ? 'a'` | `t` | | `hstore` `?&` `text[]` | `hstore` 包含所有指定的鍵? | `'a=&gt;1,b=&gt;2'::hstore ?& ARRAY['a','b']` | `t` | | `hstore` `?&#124;` `text[]` | `hstore` 包含任何指定的鍵? | `'a=&gt;1,b=&gt;2'::hstore ?&#124; ARRAY['b','c']` | `t` | | `hstore` `@&gt;` `hstore` | 左操作符包含右操作符? | `'a=&gt;b, b=&gt;1, c=&gt;NULL'::hstore @&gt; 'b=&gt;1'` | `t` | | `hstore` `&lt;@` `hstore` | 左操作符包含于右操作符? | `'a=&gt;c'::hstore &lt;@ 'a=&gt;b, b=&gt;1, c=&gt;NULL'` | `f` | | `hstore` `-` `text` | 從左操作符中刪除鍵 | `'a=&gt;1, b=&gt;2, c=&gt;3'::hstore - 'b'::text` | `"a"=&gt;"1", "c"=&gt;"3"` | | `hstore` `-` `text[]` | 從左操作符中刪除鍵 | `'a=&gt;1, b=&gt;2, c=&gt;3'::hstore - ARRAY['a','b']` | `"c"=&gt;"3"` | | `hstore` `-` `hstore` | 從左操作符中刪除匹配對 | `'a=&gt;1, b=&gt;2, c=&gt;3'::hstore - 'a=&gt;4, b=&gt;2'::hstore` | `"a"=&gt;"1", "c"=&gt;"3"` | | `record` `#=` `hstore` | 用`hstore`里匹配的值替換`record`里的字段 | 查看示例章節 | | `%%` `hstore` | 轉換`hstore`為替換鍵和值的數組 | `%% 'a=&gt;foo, b=&gt;bar'::hstore` | `{a,foo,b,bar}` | | `%#` `hstore` | 轉換`hstore`為兩維鍵/值數組 | `%# 'a=&gt;foo, b=&gt;bar'::hstore` | `{{a,foo},{b,bar}}` | > **Note:** PostgreSQL 8.2之前,包含操作符`@&gt;`和`&lt;@`分別被稱為`@` 和`~`。這些名字現在仍然可用,但是已經廢棄了并且最終將會被移除。 請注意,舊的名字從大會移除,之前跟隨著核心幾何數據類型! **Table F-7\. `hstore` 函數** | 函數 | 返回類型 | 描述 | 示例 | 結果 | | --- | --- | --- | --- | --- | | `hstore(record)` | `hstore` | 從一個記錄或行構造一個 `hstore` | `hstore(ROW(1,2))` | `f1=&gt;1,f2=&gt;2` | | `hstore(text[])` | `hstore` | 從一個數組構造一個`hstore`,可能是一個鍵/值數組,也可能是一個兩維數組 | `hstore(ARRAY['a','1','b','2']) &#124;&#124; hstore(ARRAY[['c','3'],['d','4']])` | `a=&gt;1, b=&gt;2, c=&gt;3, d=&gt;4` | | `hstore(text[], text[])` | `hstore` | 從一個單獨的鍵和值數組構造一個`hstore` | `hstore(ARRAY['a','b'], ARRAY['1','2'])` | `"a"=&gt;"1","b"=&gt;"2"` | | `hstore(text, text)` | `hstore` | 制作單一項`hstore` | `hstore('a', 'b')` | `"a"=&gt;"b"` | | `akeys(hstore)` | `text[]` | 獲取`hstore`的鍵作為一個數組 | `akeys('a=&gt;1,b=&gt;2')` | `{a,b}` | | `skeys(hstore)` | `setof text` | 獲取`hstore`的鍵作為一個集合 | `skeys('a=&gt;1,b=&gt;2')` | `a b` | | `avals(hstore)` | `text[]` | 獲取`hstore`的值作為一個數組 | `avals('a=&gt;1,b=&gt;2')` | `{1,2}` | | `svals(hstore)` | `setof text` | 獲取`hstore`的值作為一個集合 | `svals('a=&gt;1,b=&gt;2')` | `1 2` | | `hstore_to_array(hstore)` | `text[]` | 獲取`hstore`的鍵和值作為一個鍵值交替的數組 | `hstore_to_array('a=&gt;1,b=&gt;2')` | `{a,1,b,2}` | | `hstore_to_matrix(hstore)` | `text[]` | 獲取`hstore`的鍵和值作為一個兩維數組 | `hstore_to_matrix('a=&gt;1,b=&gt;2')` | `{{a,1},{b,2}}` | | `hstore_to_json(hstore)` | `json` | 獲取`hstore`作為一個`json`值 | `hstore_to_json('"a key"=&gt;1, b=&gt;t, c=&gt;null, d=&gt;12345, e=&gt;012345, f=&gt;1.234, g=&gt;2.345e+4')` | `{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}` | | `hstore_to_json_loose(hstore)` | `json` | 獲取`hstore`作為一個`json`值,但是試圖區分數值和布爾值,所以它們在JSON中沒有引號 | `hstore_to_json_loose('"a key"=&gt;1, b=&gt;t, c=&gt;null, d=&gt;12345, e=&gt;012345, f=&gt;1.234, g=&gt;2.345e+4')` | `{"a key": 1, "b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4}` | | `slice(hstore, text[])` | `hstore` | 提取`hstore`的一個子集 | `slice('a=&gt;1,b=&gt;2,c=&gt;3'::hstore, ARRAY['b','c','x'])` | `"b"=&gt;"2", "c"=&gt;"3"` | | `each(hstore)` | `setof(key text, value text)` | 獲取 `hstore`的鍵和值作為一個集合 | `select * from each('a=&gt;1,b=&gt;2')` | `key => value: a => 1, b => 2` | | `exist(hstore,text)` | `boolean` | `hstore` 包含鍵嗎? | `exist('a=&gt;1','a')` | `t` | | `defined(hstore,text)` | `boolean` | `hstore` 包含非`NULL`值的鍵嗎? | `defined('a=&gt;NULL','a')` | `f` | | `delete(hstore,text)` | `hstore` | 刪除匹配鍵的對 | `delete('a=&gt;1,b=&gt;2','b')` | `"a"=&gt;"1"` | | `delete(hstore,text[])` | `hstore` | 刪除匹配鍵的多個對 | `delete('a=&gt;1,b=&gt;2,c=&gt;3',ARRAY['a','b'])` | `"c"=&gt;"3"` | | `delete(hstore,hstore)` | `hstore` | 刪除匹配第二個參數中元素的對 | `delete('a=&gt;1,b=&gt;2','a=&gt;4,b=&gt;2'::hstore)` | `"a"=&gt;"1"` | | `populate_record(record,hstore)` | `record` | 替換`record`中匹配`hstore`中的值的字段 | 參閱示例章節 | > **Note:** 當`hstore`值轉換為`json`時使用`hstore_to_json`函數。 > **Note:** 函數`populate_record`實際上是用`anyelement`, 而不是`record`,聲明為它的第一個參數,但是它將用運行時錯誤拒絕非記錄類型。 ## F.16.3\. 索引 `hstore`有GiST 和 GIN索引支持`@&gt;`, `?`, `?&` 和 `?|`操作符。例如: ``` CREATE INDEX hidx ON testhstore USING GIST (h); CREATE INDEX hidx ON testhstore USING GIN (h); ``` `hstore`也為`=`操作符支持`btree` 或 `hash`索引。 這允許`hstore`字段聲明為`UNIQUE`,或在`GROUP BY`, `ORDER BY` 或 `DISTINCT`表達式中使用。為`hstore` 值的排序順序不是很有用,但是這些索引可能對于等價查找有用處。為`=` 比較創建索引如下: ``` CREATE INDEX hidx ON testhstore USING BTREE (h); CREATE INDEX hidx ON testhstore USING HASH (h); ``` ## F.16.4\. 例子 添加一個鍵,或用新值更新一個現有的鍵: ``` UPDATE tab SET h = h || hstore('c', '3'); ``` 刪除一個鍵: ``` UPDATE tab SET h = delete(h, 'k1'); ``` 轉換`record`為`hstore`: ``` CREATE TABLE test (col1 integer, col2 text, col3 text); INSERT INTO test VALUES (123, 'foo', 'bar'); SELECT hstore(t) FROM test AS t; hstore --------------------------------------------- "col1"=>"123", "col2"=>"foo", "col3"=>"bar" (1 row) ``` 轉換`hstore`為一個預先定義的`record`類型: ``` CREATE TABLE test (col1 integer, col2 text, col3 text); SELECT * FROM populate_record(null::test, '"col1"=>"456", "col2"=>"zzz"'); col1 | col2 | col3 ------+------+------ 456 | zzz | (1 row) ``` 使用`hstore`里的值修改現有的記錄: ``` CREATE TABLE test (col1 integer, col2 text, col3 text); INSERT INTO test VALUES (123, 'foo', 'bar'); SELECT (r).* FROM (SELECT t #= '"col3"=>"baz"' AS r FROM test t) s; col1 | col2 | col3 ------+------+------ 123 | foo | baz (1 row) ``` ## F.16.5\. 統計 `hstore`類型,由于其內在的慷慨,可以包含大量不同的鍵。檢查有效的鍵是應用的任務。 下列的例子演示幾個檢查鍵和獲取統計的技術。 簡單例子: ``` SELECT * FROM each('aaa=>bq, b=>NULL, ""=>1'); ``` 使用一個表: ``` SELECT (each(h)).key, (each(h)).value INTO stat FROM testhstore; ``` 在線統計: ``` SELECT key, count(*) FROM (SELECT (each(h)).key FROM testhstore) AS stat GROUP BY key ORDER BY count DESC, key; key | count -----------+------- line | 883 query | 207 pos | 203 node | 202 space | 197 status | 195 public | 194 title | 190 org | 189 ................... ``` ## F.16.6\. 兼容性 自PostgreSQL 9.0起,`hstore`使用一個不同于以前版本的內部表示。 這樣做對于轉儲/恢復升級沒有什么障礙,因為文本表示(在轉儲中使用的)沒有改變。 在一個二進制升級中,向上兼容是通過使新代碼認識老格式的數據來維護的。 這在處理還未被新代碼修改的數據時會有一點性能代償。通過像下面這樣的`UPDATE` 語句強制升級一個表字段中的所有值是可能的: ``` UPDATE tablename SET hstorecol = hstorecol || ''; ``` 另一個方法是: ``` ALTER TABLE tablename ALTER hstorecol TYPE hstore USING hstorecol || ''; ``` `ALTER TABLE`方法要求在表上的一個排他鎖,但是不會導致有舊行版本的表膨脹。 ## F.16.7\. 作者 Oleg Bartunov `&lt;[oleg@sai.msu.su](mailto:oleg@sai.msu.su)&gt;`, Moscow, Moscow University, Russia Teodor Sigaev `&lt;[teodor@sigaev.ru](mailto:teodor@sigaev.ru)&gt;`, Moscow, Delta-Soft Ltd., Russia Andrew Gierth `&lt;[andrew@tao11.riddles.org.uk](mailto:andrew@tao11.riddles.org.uk)&gt;`附加的增強, United Kingdom {% endraw %}
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看