摘要:的實(shí)踐一數(shù)據(jù)類(lèi)型一數(shù)據(jù)類(lèi)型復(fù)合類(lèi)型創(chuàng)建一個(gè)復(fù)合類(lèi)型命令格式合成類(lèi)型欄位類(lèi)型沒(méi)有找到任何名稱(chēng)為的關(guān)聯(lián)。數(shù)據(jù)庫(kù)會(huì)對(duì)輸人的數(shù)據(jù)進(jìn)行檢查,讓一些不符合標(biāo)準(zhǔn)的數(shù)據(jù)不能存放到數(shù)據(jù)庫(kù)中,同時(shí)還提供了函數(shù)對(duì)其類(lèi)型進(jìn)行安全性檢査。
PostgreSQL的實(shí)踐一:數(shù)據(jù)類(lèi)型(一)
數(shù)據(jù)類(lèi)型 復(fù)合類(lèi)型# 創(chuàng)建一個(gè)復(fù)合類(lèi)型person # 命令格式:create type xxx AS (); testdb=# create type person AS ( testdb(# name text, testdb(# age int4, testdb(# sex boolean testdb(# ); CREATE TYPE testdb=# d person; 合成類(lèi)型 "public.person" 欄位 | 類(lèi)型 | Collation | Nullable | Default ------+---------+-----------+----------+--------- name | text | | | age | integer | | | sex | boolean | | | testdb=# dt person; 沒(méi)有找到任何名稱(chēng)為 "person" 的關(guān)聯(lián)。 testdb=# d person; 合成類(lèi)型 "public.person" 欄位 | 類(lèi)型 | Collation | Nullable | Default ------+---------+-----------+----------+--------- name | text | | | age | integer | | | sex | boolean | | | # 創(chuàng)建一個(gè)帶復(fù)合類(lèi)型的表author testdb=# create table author ( testdb(# id int4, testdb(# people person, testdb(# book text testdb(# ); CREATE TABLE testdb=# d author; 數(shù)據(jù)表 "public.author" 欄位 | 類(lèi)型 | Collation | Nullable | Default --------+---------+-----------+----------+--------- id | integer | | | people | person | | | book | text | | | # 插入數(shù)據(jù) testdb=# insert into author values(1, " ("qkl", 29, TRUE)", "張三的自傳"); INSERT 0 1 # 缺省不填寫(xiě)就是NULL,逗號(hào)間不要有空格 testdb=# insert into author values(2, "("zgq", , TRUE)", "張三的自傳2"); ERROR: invalid input syntax for integer: " " 第1行insert into author values(2, "("zgq", , TRUE)", "張三的自傳2... ^ testdb=# insert into author values(2, "("zgq",,TRUE)", "張三的自傳2"); INSERT 0 1 testdb=# insert into author values(2, "("zgq",, TRUE)", "張三的自傳2"); INSERT 0 1 testdb=# insert into author values(3, "("",, TRUE)", "張三的自傳3"); INSERT 0 1 # ROW行插入,注意ROW后面的字符請(qǐng)用單引號(hào),不能帶雙引號(hào) testdb=# insert into author values(4, ROW("",, TRUE), "張三的自傳5"); ERROR: zero-length delimited identifier at or near """" 第1行insert into author values(4, ROW("",, TRUE), "張三的自傳5"); ^ testdb=# insert into author values(4, ROW("PCB",, TRUE), "張三的自傳5"); ERROR: syntax error at or near "," 第1行insert into author values(4, ROW("PCB",, TRUE), "張三的自傳5... ^ # 省略ROW testdb=# insert into author values(4, ("hgr",30, TRUE), "張三的自傳6"); INSERT 0 1 testdb=# select (people).name from author; name ------ qkl zgq zgq hgr (5 行記錄) # 更新復(fù)合類(lèi)型數(shù)據(jù),復(fù)合類(lèi)型名不帶括號(hào) testdb=# update author set people.name="qkl2" where id=1; UPDATE 1 testdb=# select people from author; people ------------- (zgq,,t) (zgq,,t) ("",,t) (hgr,30,t) (qkl2,29,t) (5 行記錄) testdb=# update author set people.age = people.age +1 where id=1; ERROR: missing FROM-clause entry for table "people" 第1行update author set people.age = people.age +1 where id=1; ^ # 如果更新的字段右邊也是復(fù)合類(lèi)型必須攜帶括號(hào) testdb=# update author set people.age = (people).age +1 where id=1; UPDATE 1 testdb=# select people from author; people ------------- (zgq,,t) (zgq,,t) ("",,t) (hgr,30,t) (qkl2,30,t) (5 行記錄) # 多帶帶插入復(fù)合類(lèi)型的數(shù)據(jù) testdb=# insert into author (id, people.name, people.age) values(7, "ldh", 33); INSERT 0 1 testdb=# select * from author; id | people | book ----+-------------+------------- 2 | (zgq,,t) | 張三的自傳2 2 | (zgq,,t) | 張三的自傳2 3 | ("",,t) | 張三的自傳3 4 | (hgr,30,t) | 張三的自傳6 1 | (qkl2,30,t) | 張三的自傳 7 | (ldh,33,) | (6 行記錄)XML類(lèi)型
xm丨類(lèi)型可以用于存儲(chǔ)XML數(shù)據(jù)。使用字符串類(lèi)型(如text)也可以存儲(chǔ)XML數(shù)據(jù),但text類(lèi)型不能保證其中存儲(chǔ)的是合法XML數(shù)據(jù),通常需要由應(yīng)用程序來(lái)負(fù)責(zé)保證輸人數(shù)據(jù)的正確性,這將增加應(yīng)用程序開(kāi)發(fā)的難度。而使用xml類(lèi)型就不存在此問(wèn)題。數(shù)據(jù)庫(kù)會(huì)對(duì)輸人的數(shù)據(jù)進(jìn)行檢查,讓一些不符合XML標(biāo)準(zhǔn)的數(shù)據(jù)不能存放到數(shù)據(jù)庫(kù)中,同時(shí)還提供了函數(shù)對(duì)其類(lèi)型進(jìn)行安全性檢査。
注意,要使用xml數(shù)據(jù)類(lèi)型,在編譯PostgreSQL源碼時(shí)必須使用以下參數(shù):
configure --with-libxml
xml存儲(chǔ)的XML數(shù)據(jù)杳兩種:
由 XML 標(biāo)準(zhǔn)走義的documents。
由XML標(biāo)準(zhǔn)定義的content片段。
content片段可以有多個(gè)級(jí)元素或character節(jié)點(diǎn).但documents只能有一個(gè)頂級(jí)元素。可以使用xmlvalue is DOCUMENT來(lái)判斷一個(gè)特定的XML值是一個(gè)documents還足content片段。
PostgreSQL的xmlopiions參數(shù)用來(lái)指定輸人的數(shù)據(jù)是documents還是content片段,默認(rèn)情況下此值為content片段。所以輸人的xml可以有多個(gè)頂級(jí)元素,但如果把此參數(shù)改成document,將不能輸人有多個(gè)頂級(jí)元素的內(nèi)容。
PostgreSQL數(shù)據(jù)庫(kù)在客戶(hù)端與服務(wù)器之間傳遞數(shù)據(jù)時(shí),會(huì)自動(dòng)進(jìn)行字符集的轉(zhuǎn)換。如果客戶(hù)端的字符集與服務(wù)端不一樣,PostgreSQL會(huì)自動(dòng)進(jìn)行字符集轉(zhuǎn)換。但也正是因?yàn)橛羞@個(gè)特性,在傳遞XML數(shù)據(jù)時(shí)需要格外注意。我們知道,對(duì)于XML文件來(lái)說(shuō),可以通過(guò)類(lèi)似“encoding="XXX"”的方式指定自己文件的字符集,但當(dāng)這些數(shù)據(jù)在PostgreSQL之間傳遞時(shí), PostgreSQL會(huì)把其原始內(nèi)容的宇符集變成數(shù)據(jù)庫(kù)服務(wù)端的字符集,而這就會(huì)導(dǎo)致問(wèn)題,因?yàn)檫@意味著XML數(shù)據(jù)中的字符集編碼聲明在客戶(hù)端和服務(wù)器之間傳遞時(shí),可能變得無(wú)效。為了應(yīng)對(duì)該問(wèn)題,提交輸人到xml類(lèi)型的字符串中的編碼聲明將會(huì)被忽略掉.同時(shí)內(nèi)容的字符集會(huì)被認(rèn)為是當(dāng)前數(shù)據(jù)庫(kù)服務(wù)器的字符集。
正確處理XML字符集的方式是,先將XML數(shù)據(jù)的字符串在當(dāng)前客戶(hù)端中編碼成當(dāng)前客戶(hù)端的字符集,然后再發(fā)送到服務(wù)端,這樣就會(huì)被轉(zhuǎn)換成服務(wù)端的字符集存儲(chǔ)。當(dāng)査詢(xún)xml 類(lèi)型的值時(shí),此數(shù)據(jù)又會(huì)被轉(zhuǎn)換成客戶(hù)端的字符集,所以客戶(hù)端收到的XML數(shù)據(jù)的字符集就是客戶(hù)端的字符集。
所以通常來(lái)說(shuō),如果XML數(shù)據(jù)的字符集編碼、客戶(hù)端字符集編碼,以及服務(wù)器字符集編碼完全一樣,那么用PostgreSQL處理XML數(shù)據(jù)將會(huì)大大減少字符集問(wèn)題,并且處理的效率也會(huì)很高。通常XML數(shù)據(jù)都是用UTF-8編碼格式處理的,因此把PostgreSQL數(shù)據(jù)庫(kù)服務(wù)器端編碼也設(shè)置成UTF-8將是一種較好的選擇。
postgres=# select xml"json類(lèi)型hello postgres
"; xml -----------------------------hello postgres
(1 行記錄) postgres=# show xmloption; xmloption ----------- content (1 行記錄) postgres=# set xmloption TO document; SET postgres=# show xmloption; xmloption ----------- document (1 行記錄) # document類(lèi)型,插入多頂級(jí)元素,出錯(cuò) postgres=# select xml"hello postgres
hello world "; ERROR: invalid XML document 第1行select xml"hello postgres
hello worldhello postgres hello world ^ postgres=# set xmloption TO content; SET postgres=# select xml"hello postgres
hello world "; xml -------------------------------------------------------hello postgres
hello world (1 行記錄) # xmlparse函數(shù) postgres=# select xmlparse(document""); xmlparse ----------------------------------- qkl (1 行記錄) postgres=# select xmlparse(content" qkl "); xmlparse ----------------------------------- qkl (1 行記錄) qkl
JSON數(shù)據(jù)類(lèi)型是從P〇StgreSQL9.3開(kāi)始提供的一種類(lèi)型,9.3版中只有一種類(lèi)型:JSON
在PosIgreSQL 9.4中乂提供了一種更高效的類(lèi)型JSONB這兩種類(lèi)型在使用上幾乎完全一致,主要的區(qū)別是,JSON類(lèi)型是把輸人的數(shù)據(jù)原封不動(dòng)地存放到數(shù)據(jù)庫(kù)中(當(dāng)然在存放前會(huì)做JSON的語(yǔ)法檢查),使用的時(shí)候需要?:新解析數(shù)據(jù),而JSONB類(lèi)型是在存放時(shí)就把JSON解折成二進(jìn)制格式了,使用的時(shí)候就不需要再次解析,所以JSONB在使用時(shí)性能會(huì)更高。另外,JSONB支持在其上逑索引,iftHSON則不能,這是JSONB類(lèi)型的很大一個(gè)優(yōu)點(diǎn)。
因?yàn)镴SON類(lèi)型是把輸人的整個(gè)字符串原封不改動(dòng)地保存到數(shù)據(jù)庫(kù)中的,因此JSON串中key之間多余的空格也會(huì)保留。而且,如果JSON串中有重復(fù)的key,這些重復(fù)的key也會(huì)保留(默認(rèn)處理時(shí)以最后一個(gè)為準(zhǔn)),同時(shí)也會(huì)保留輸人時(shí)JSON串中各個(gè)key的順序。而JSONB類(lèi)塑則恰恰相反,+會(huì)保留多余的空格,不會(huì)保留key的順序,也不會(huì)保留重復(fù)的key。
在PostgreSQL中只允許毎個(gè)數(shù)據(jù)庫(kù)用一種服務(wù)器編碼,如果數(shù)據(jù)庫(kù)的編碼不是UTF-8,PostgreSQL中的JSON類(lèi)塑是無(wú)法嚴(yán)格符合JSON規(guī)范中對(duì)字符集的要求的。如果輸人中包含不能在服務(wù)器編碼中表示的字符數(shù)據(jù),將無(wú)法導(dǎo)人到數(shù)據(jù)庫(kù)中。但是,能在服務(wù)器編碼中表示的
非UTF-8字符則是被允許的??梢允褂胾XXXX形式的轉(zhuǎn)義,從而忽視數(shù)據(jù)庫(kù)的字符集編碼。
當(dāng)把一個(gè)JSON字符串轉(zhuǎn)換成JSONB類(lèi)塑吋,JSON字符串內(nèi)的數(shù)據(jù)類(lèi)型實(shí)際上被轉(zhuǎn)換成rPostgreSQL數(shù)據(jù)庫(kù)中的類(lèi)塑,兩者的映射關(guān)系見(jiàn)表5-30。耑要注意的是,如果是在JS0NB中,在PostgrcSQL 不能輸人超出numeric數(shù)據(jù)類(lèi)型范鬧的值。
基礎(chǔ)操作
testdb=# select "9"::json, ""osdba""::json, "true"::json, "null"::json; json | json | json | json ------+---------+------+------ 9 | "osdba" | true | null (1 行記錄) testdb=# select "[9, true, "qkl", null]"::json, "[9, true, "qkl", null]"::jsonb; json | jsonb ------------------------+------------------------ [9, true, "qkl", null] | [9, true, "qkl", null] (1 行記錄) testdb=# select json"{"name":"qkl", "age":18, "sex":true, "money":888.88}"; json ------------------------------------------------------ {"name":"qkl", "age":18, "sex":true, "money":888.88} (1 行記錄) # json 存取的是浮點(diǎn)型,存在精度問(wèn)題 testdb=# select json"{"p":1.684544545454e-27}"; json -------------------------- {"p":1.684544545454e-27} (1 行記錄) # jsonb存取的是numeric testdb=# select jsonb"{"p":1.684544545454e-27}"; jsonb -------------------------------------------------- {"p": 0.000000000000000000000000001684544545454} (1 行記錄)
更多和操作符
testdb=# select json"[1,2,3]"->0; ?column? ---------- 1 (1 行記錄) testdb=# select json"[1,2,3]"->3; ?column? ---------- (1 行記錄) testdb=# select json"[1,2,3]"->2; ?column? ---------- 3 (1 行記錄) testdb=# select json"[1,2,3]"->>2; ?column? ---------- 3 (1 行記錄) testdb=# select json"[1,2,3]"->>"2"; ?column? ---------- (1 行記錄) testdb=# select json"{"a":1,"b":22}"->>"a"; ?column? ---------- 1 (1 行記錄) testdb=# select json"{"a":{"a1":{"a11":111}},"b":22}"#>"{a,a1}" testdb-# ; ?column? ------------- {"a11":111} (1 行記錄) testdb=# select json"{"a":{"a1":{"a11":111}},"b":22}"#>"{a,a1,a11}" testdb-# ; ?column? ---------- 111 (1 行記錄) testdb=# select json"{"a":{"a1":{"a11":111}},"b":22}"#>>"{a,a1,a11}"; ?column? ---------- 111 (1 行記錄) testdb=# select json"{"a":{"a1":{"a11":111}},"b":22}"#>>"{a,a1}"; ?column? ------------- {"a11":111} (1 行記錄)Range類(lèi)型
testdb=# select "(0,6)"::int4range; int4range ----------- [1,6) (1 行記錄) testdb=# select "[0,6)"::int4range; int4range ----------- [0,6) (1 行記錄) testdb=# select "[0,6]"::int4range; int4range ----------- [0,7) (1 行記錄) testdb=# select "empty"::int4range; int4range ----------- empty (1 行記錄) # 上面我們看出,int4range總數(shù)會(huì)轉(zhuǎn)換成`[)`格式 testdb=# select "[0,6]"::numrange; numrange ---------- [0,6] (1 行記錄) testdb=# select "[0,6)"::numrange; numrange ---------- [0,6) (1 行記錄) testdb=# select "(0,6)"::numrange; numrange ---------- (0,6) (1 行記錄) testdb=# select "(0,)"::numrange; numrange ---------- (0,) (1 行記錄) testdb=# select "[1,)"::numrange; numrange ---------- [1,) (1 行記錄)數(shù)組類(lèi)型
PostgreSQL支持表的字段使用定長(zhǎng)或可變長(zhǎng)度的一維或多維數(shù)組,數(shù)組的類(lèi)型可以是任何數(shù)據(jù)庫(kù)內(nèi)建的類(lèi)型、用戶(hù)自定義的類(lèi)型、枚舉類(lèi)型,以及組合類(lèi)型。但目前還不支持domain類(lèi)型。
create table test6 (id int, col1 int[], col2 int[10], col3 text[][]); create table test7 (id int, col1 int[10], col2 int[], col3 text[]);
在第一個(gè)語(yǔ)句中第二列的聲明col2 int[10]與第二個(gè)語(yǔ)句中第二列的聲明col2 int[]其意思相同;而在第一個(gè)語(yǔ)句中第三列的聲明col3 text[][]與第二個(gè)語(yǔ)句中第三列的聲明col3 text[]的意思也是相同的
我們來(lái)查看下表結(jié)構(gòu)
testdb=# create table test6 (id int, col1 int[], col2 int[10], col3 text[][]); CREATE TABLE testdb=# create table test7 (id int, col1 int[10], col2 int[], col3 text[]); CREATE TABLE testdb=# d 關(guān)聯(lián)列表 架構(gòu)模式 | 名稱(chēng) | 類(lèi)型 | 擁有者 ----------+--------------+--------+---------- public | author | 數(shù)據(jù)表 | postgres public | test1 | 數(shù)據(jù)表 | postgres public | test2 | 數(shù)據(jù)表 | postgres public | test2_id_seq | 序列數(shù) | postgres public | test3 | 數(shù)據(jù)表 | postgres public | test3_id_seq | 序列數(shù) | postgres public | test6 | 數(shù)據(jù)表 | postgres public | test7 | 數(shù)據(jù)表 | postgres (8 行記錄) testdb=# d test6; 數(shù)據(jù)表 "public.test6" 欄位 | 類(lèi)型 | Collation | Nullable | Default ------+-----------+-----------+----------+--------- id | integer | | | col1 | integer[] | | | col2 | integer[] | | | col3 | text[] | | | testdb=# d test7; 數(shù)據(jù)表 "public.test7" 欄位 | 類(lèi)型 | Collation | Nullable | Default ------+-----------+-----------+----------+--------- id | integer | | | col1 | integer[] | | | col2 | integer[] | | | col3 | text[] | | |
testdb=# create table test66(id int, col1 int[]); CREATE TABLE testdb=# insert into test66 values(1, "{1,2,3}"); INSERT 0 1 testdb=# insert into test66 values(2, "{4,5,6}"); INSERT 0 1 testdb=# select * from test66; id | col1 ----+--------- 1 | {1,2,3} 2 | {4,5,6} (2 行記錄) testdb=# create table test77(id int, col1 text[]); CREATE TABLE testdb=# insert into test77 values(1, "{how, howe, howl}"); INSERT 0 1 testdb=# select * from test77; id | col1 ----+----------------- 1 | {how,howe,howl} (1 行記錄) testdb=# insert into test77 values(1, "{"how", "howe", "howl"}"); INSERT 0 1 testdb=# select * from test77; id | col1 ----+----------------- 1 | {how,howe,howl} 1 | {how,howe,howl} (2 行記錄) # 注意上面不同的類(lèi)型,可能分隔符會(huì)不一樣,下面可查詢(xún)不同類(lèi)型的分隔符 testdb=# select typname,typdelim from pg_type where typname in ("int4", "int8", "bool", "char", "box"); typname | typdelim ---------+---------- bool | , char | , int8 | , int4 | , box | ; (5 行記錄) testdb=# create table test778(id int ,col1 box[]); CREATE TABLE testdb=# insert into test778 values(1, "{((1,1), (2,2)); ((3,3),(4,4))}"); INSERT 0 1 testdb=# select * from test778; id | col1 ----+--------------------------- 1 | {(2,2),(1,1);(4,4),(3,3)} (1 行記錄) # 上面輸人的字符串內(nèi)容是沒(méi)有空格的,在有空格時(shí),乂該如何輸人呢?見(jiàn)下面的例子: testdb=# insert into test77 values(3,"{how many,how mach,how old}"); INSERT 0 1 可以看到有空格,也可以直接輸人。 那么字符串中有逗號(hào)時(shí)怎么辦呢?這時(shí)可以使用雙引號(hào),如下: testdb=# insert into test77 values(4,"{"who, what", "CO.,LTD."}"); INSERT 0 1 如果字符串中有單引號(hào)怎么辦呢?這時(shí)可以使用兩個(gè)連接的單引號(hào)表示一個(gè)單引號(hào): testdb=# insert into test77 values(3,"{"who""s bread", "It""s ok"}"); INSERT 0 1 如果輸人的字符串中有括號(hào)"{"和"}"怎么辦呢?只需把它們放到雙引號(hào)中即可: testdb=# insert into test77 values (5, "{"{os, dba}", "{dba, os}"}"); INSERT 0 1 如果輸人的字符串中有雙引號(hào)怎么辦呢?這時(shí)需要在雙引號(hào)前加反斜扛,如下所示: testdb=# insert into test77 values(6,"{os"dba}"); testdb=# select * from test77; id | col1 ----+----------------------------------- 1 | {how,howe,howl} 1 | {how,howe,howl} 3 | {"how many","how mach","how old"} 4 | {"who, what","CO.,LTD."} 3 | {"who"s bread","It"s ok"} 5 | {"{os, dba}","{dba, os}"} 6 | {"os"dba"} (7 行記錄) # ARRAY關(guān)鍵詞的使用,需帶單引號(hào) testdb=# insert into test77 values(9, array["os", "dba"]); INSERT 0 1 testdb=# insert into test77 values(9, array["os"win", "dba"]); INSERT 0 1 testdb=# insert into test77 values(9, array["os""win", "dba"]); INSERT 0 1 testdb=# select * from test77; id | col1 ----+----------------------------------- 1 | {how,howe,howl} 1 | {how,howe,howl} 3 | {"how many","how mach","how old"} 4 | {"who, what","CO.,LTD."} 3 | {"who"s bread","It"s ok"} 5 | {"{os, dba}","{dba, os}"} 6 | {"os"dba"} 9 | {os,dba} 9 | {"os"win",dba} 9 | {os"win,dba} (10 行記錄)
# 多維數(shù)組 testdb=# create table test779 (id int , col1 test[][]); ERROR: type "test[]" does not exist 第1行create table test779 (id int , col1 test[][]); testdb=# create table test779 (id int , col1 text[][]); CREATE TABLE testdb=# d test779; 數(shù)據(jù)表 "public.test779" 欄位 | 類(lèi)型 | Collation | Nullable | Default ------+---------+-----------+----------+--------- id | integer | | | col1 | text[] | | | testdb=# insert into test779 values(1, array[["aa"], ["cc"]]); INSERT 0 1 testdb=# insert into test779 values(1, array[["aa","bb"], ["cc","dd"]]); INSERT 0 1 testdb=# select * from test779; id | col1 ----+------------------- 1 | {{aa},{cc}} 1 | {{aa,bb},{cc,dd}} (2 行記錄) testdb=# insert into test779 values(1, array[["aa","bb"], ["cc","dd","dd"]]); ERROR: multidimensional arrays must have array expressions with matching dimensions testdb=# insert into test779 values(3, "{{aa, bb}, {cc, dd}}"); INSERT 0 1 testdb=# select * from test779; id | col1 ----+------------------- 1 | {{aa},{cc}} 1 | {{aa,bb},{cc,dd}} 3 | {{aa,bb},{cc,dd}} (3 行記錄) # 默認(rèn)情況下數(shù)組的下標(biāo)是從1開(kāi)始的,postgres的數(shù)組是可以指定開(kāi)始下標(biāo)的 testdb=# create table test7799 (id int[]); CREATE TABLE testdb=# insert into test7799 values("[2:4]={1,2,3}"); INSERT 0 1 testdb=# select id[2], id[3], id[4] from test7799; id | id | id ----+----+---- 1 | 2 | 3 (1 行記錄) testdb=# select id from test7799; id --------------- [2:4]={1,2,3} (1 行記錄) testdb=# insert into test7799 values("{11,22,33}"); INSERT 0 1 testdb=# select id[2], id[3], id[4] from test7799; id | id | id ----+----+---- 1 | 2 | 3 22 | 33 | (2 行記錄) testdb=# select id[1], id[2], id[3] from test7799; id | id | id ----+----+---- | 1 | 2 11 | 22 | 33 (2 行記錄) # 從上面的例子可以看出,指定數(shù)組上下標(biāo)的格式為: `[下標(biāo):上標(biāo)] = [元素值1, 元素值2, 元素值3..]`
testdb=# create table test666 (id int, col1 int[][]); CREATE TABLE testdb=# d test666; 數(shù)據(jù)表 "public.test666" 欄位 | 類(lèi)型 | Collation | Nullable | Default ------+-----------+-----------+----------+--------- id | integer | | | col1 | integer[] | | | testdb=# insert into test666 values(1, "{{1,2,3}, {4,5,6}, {7,8,9}}"); INSERT 0 1 testdb=# select * from test666; id | col1 ----+--------------------------- 1 | {{1,2,3},{4,5,6},{7,8,9}} (1 行記錄) # 多維數(shù)組讀取 testdb=# select col1[1][1], col1[1][2], col1[2][1], col1[2][2] from test666; col1 | col1 | col1 | col1 ------+------+------+------ 1 | 2 | 4 | 5 (1 行記錄) # 無(wú)法直接獲取外層數(shù)組 testdb=# select id, col1[1] from test666; id | col1 ----+------ 1 | (1 行記錄) # 數(shù)組切換可獲取 testdb=# select id, col1[1:1] from test666; id | col1 ----+----------- 1 | {{1,2,3}} (1 行記錄) testdb=# select id, col1[1:2] from test666; id | col1 ----+------------------- 1 | {{1,2,3},{4,5,6}} (1 行記錄) # 注意這里的外層其實(shí)是col1[3][1:2] == col1[1:3][1:2] testdb=# select id, col1[3][1:2] from test666; id | col1 ----+--------------------- 1 | {{1,2},{4,5},{7,8}} (1 行記錄) testdb=# select id, col1[1:3][1:2] from test666; id | col1 ----+--------------------- 1 | {{1,2},{4,5},{7,8}} (1 行記錄) # 注意這里的外層其實(shí)是col1[1:2][2] == col1[1:2][1:2] testdb=# select id, col1[1:2][2] from test666; id | col1 ----+--------------- 1 | {{1,2},{4,5}} (1 行記錄) testdb=# select id, col1[1:2][1:2] from test666; id | col1 ----+--------------- 1 | {{1,2},{4,5}} (1 行記錄) # 修改 # 可以修改整個(gè)字段數(shù)組值,或修改某一維度的單元素的值 # 不過(guò)無(wú)法直接修改某一維數(shù)組的值 testdb=# select * from test666; id | col1 ----+--------------------------- 1 | {{1,2,3},{4,5,6},{7,8,9}} (1 行記錄) testdb=# update test666 set col1[2][1] = 1000 where id=1; UPDATE 1 testdb=# select * from test666; id | col1 ----+------------------------------ 1 | {{1,2,3},{1000,5,6},{7,8,9}} (1 行記錄) testdb=# update test666 set col1[2] = "{1,2,3}" where id=1; ERROR: invalid input syntax for integer: "{1,2,3}" 第1行update test666 set col1[2] = "{1,2,3}" where id=1;偽類(lèi)類(lèi)型
偽類(lèi)型(Pseudo-Types)是PostgreSQL中不能作為字段的數(shù)據(jù)類(lèi)型,但是它可以用于聲明
一個(gè)函數(shù)的參數(shù)或者結(jié)果類(lèi)型。所包含的類(lèi)型有:
□ any:用于指示函數(shù)的輸人參數(shù)可以是任意數(shù)據(jù)類(lèi)型的。
□ anyelement:表示一個(gè)函數(shù)接受任何數(shù)據(jù)類(lèi)型。
□ anyarray:表示一個(gè)函數(shù)接受任何數(shù)組類(lèi)型。
□ anynonarray:表示一個(gè)函數(shù)接受任何非數(shù)組類(lèi)塑。
□ anyenum:表示一個(gè)函數(shù)接受任何枚舉類(lèi)型_。
□ anyrange:表示一個(gè)函數(shù)接受任何范類(lèi)型。
□ cstring:表示一個(gè)函數(shù)接受或返回一個(gè)空字符(0)結(jié)尾的C語(yǔ)言字符串。
□ internal:表示一個(gè)函數(shù)接受或者返冋一種服務(wù)器內(nèi)部的數(shù)據(jù)類(lèi)型。
□ language_handler:聲明一個(gè)函數(shù)返回類(lèi)型是pi語(yǔ)言的一個(gè)handler閑數(shù)。
□ fdw_handler:聲明一個(gè)函數(shù)的返冋類(lèi)型楚foreign-data wrapper的handler函數(shù)。
□ record:標(biāo)識(shí)一個(gè)函數(shù)返回一個(gè)未詳細(xì)定義各列的row類(lèi)型。
□ trigger: 一個(gè)觸發(fā)器函數(shù)要聲明為返回trigger類(lèi)型。
□ void:表示一個(gè)函數(shù)沒(méi)有返問(wèn)值。
□ opaque:已經(jīng)過(guò)時(shí)的類(lèi)型,舊的PostgreSQL版本中用于上面這些用途。
用C編寫(xiě)的函數(shù)(不管是內(nèi)H的還是動(dòng)態(tài)裝載的)都可以盧明為接受或返冋上面任意一種偽數(shù)據(jù)類(lèi)型。在把偽類(lèi)型用作函數(shù)參數(shù)類(lèi)型時(shí),PostgreSQL數(shù)據(jù)庫(kù)本身對(duì)類(lèi)型檢査就少了很多,保證類(lèi)沏正確的任務(wù)就交給了寫(xiě)函數(shù)的幵發(fā)人員。
用過(guò)程語(yǔ)言編寫(xiě)的函數(shù)不一定都能使用上面列出的全部偽類(lèi)型,具體能使用哪些需要喪看相關(guān)的過(guò)程語(yǔ)言文襠,或者查C?過(guò)程語(yǔ)言的實(shí)現(xiàn)。通常,過(guò)程語(yǔ)言都不支持使用“any”類(lèi)型,但基本都能支持使用void和record作為結(jié)果類(lèi)?,能支持多態(tài)閑數(shù)的過(guò)程語(yǔ)言還支持使
用“311)^〇^”、“311>^丨6咖111”、“3!1)^1111171”和‘‘311>010113〇"3>^’類(lèi)型〇偽 類(lèi) 型 “intemar用于聲明那種只能在數(shù)據(jù)庫(kù)系統(tǒng)內(nèi)部調(diào)用的函數(shù),它們不能直接在SQL査詢(xún)里調(diào)用。如果函數(shù)至少有一個(gè)“internal”類(lèi)型的參數(shù),那么就不能從SQL里調(diào)用。
為了保留這個(gè)限制的類(lèi)塑安全,一定要遵循這樣的編碼規(guī)則:對(duì)于沒(méi)有任何一個(gè)"internal”參數(shù)的函數(shù),不要把返回類(lèi)型創(chuàng)建為“internal”。
UUID( Universally Unique Identifiers)用于存儲(chǔ)一個(gè) UUID; UUID 定義在RFC 4122和 ISO/IEC 9834-8:2005中。它是一個(gè)128bit的數(shù)字
PostgreSQL核心源代碼中沒(méi)提供產(chǎn)生UUID的函數(shù),contrib下的uuid-ossp模塊提供UUID的函數(shù)
pg_lsn類(lèi)切是PostgrcSQL9.4版本之后提供的表示LSN ( Log Sequence Number)的一種數(shù)據(jù)類(lèi)擬。LSN表示W(wǎng)ALfi志的位酋。在一些記錄WALti志信息的系統(tǒng)表中某些字段的類(lèi)型就是pg_lsn類(lèi)型
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/39017.html
摘要:數(shù)據(jù)類(lèi)型類(lèi)型轉(zhuǎn)換數(shù)值類(lèi)型數(shù)值類(lèi)型序列類(lèi)型不同于的自增長(zhǎng),和都是序列的方式創(chuàng)建使用創(chuàng)建序列關(guān)聯(lián)列表架構(gòu)模式名稱(chēng)類(lèi)型擁有者數(shù)據(jù)表數(shù)據(jù)表序列數(shù)行記錄數(shù)據(jù) 數(shù)據(jù)類(lèi)型 showImg(https://segmentfault.com/img/bVbi9mw?w=750&h=379);showImg(https://segmentfault.com/img/bVbi9mz?w=729&h=626)...
摘要:作者譚峰張文升出版日期年月頁(yè)數(shù)頁(yè)定價(jià)元本書(shū)特色中國(guó)開(kāi)源軟件推進(jìn)聯(lián)盟分會(huì)特聘專(zhuān)家撰寫(xiě),國(guó)內(nèi)多位開(kāi)源數(shù)據(jù)庫(kù)專(zhuān)家鼎力推薦。張文升中國(guó)開(kāi)源軟件推進(jìn)聯(lián)盟分會(huì)核心成員之一。 很高興《PostgreSQL實(shí)戰(zhàn)》一書(shū)終于出版,本書(shū)大體上系統(tǒng)總結(jié)了筆者 PostgreSQL DBA 職業(yè)生涯的經(jīng)驗(yàn)總結(jié),本書(shū)的另一位作者張文升擁有豐富的PostgreSQL運(yùn)維經(jīng)驗(yàn),目前就職于探探科技任首席PostgreS...
摘要:它在其他開(kāi)放源代碼數(shù)據(jù)庫(kù)系統(tǒng)和專(zhuān)有系統(tǒng)之外,為用戶(hù)又提供了一種選擇。將插入空間以填補(bǔ)任何額外的空間。始終被視為唯一值上述兩個(gè)約束的組合。表范圍的約束可以是,,或。如何在中創(chuàng)建表我們將創(chuàng)建一個(gè)名為的表,它定義了各種游樂(lè)場(chǎng)設(shè)備。 歡迎大家前往騰訊云+社區(qū),獲取更多騰訊海量技術(shù)實(shí)踐干貨哦~ 本文由angel_郁 發(fā)表于云+社區(qū)專(zhuān)欄 什么是PostgreSQL? PostgreSQL是自由...
摘要:每個(gè)服務(wù)由多個(gè)進(jìn)程組成,為首的進(jìn)程名為。服務(wù)使用字節(jié)長(zhǎng)的內(nèi)部事務(wù)標(biāo)識(shí)符,即時(shí)發(fā)生重疊后仍然繼續(xù)使用,這會(huì)導(dǎo)致問(wèn)題,所以需要定期進(jìn)行操作。操作被認(rèn)為是緊跟操作后的操作。在涉及高比例插入刪除的表中,會(huì)造成索引膨脹,這時(shí)候可以重建索引。 簡(jiǎn)介和認(rèn)知 發(fā)音 post-gres-q-l 服務(wù)(server) 一個(gè)操作系統(tǒng)中可以啟動(dòng)多個(gè)postgres服務(wù)。每個(gè)服務(wù)由多個(gè)進(jìn)程組成,為首的進(jìn)程名為p...
閱讀 994·2021-11-15 17:58
閱讀 3846·2021-11-12 10:36
閱讀 3901·2021-09-22 16:06
閱讀 1075·2021-09-10 10:50
閱讀 1395·2019-08-30 11:19
閱讀 3373·2019-08-29 16:26
閱讀 1026·2019-08-29 10:55
閱讀 3429·2019-08-26 13:48