PostgreSQL 數據類型

數值類型

數值類型由兩個字節,4字節和8字節的整數,4字節和8字節的浮點數和可選精度的小數。下表列出了可用的類型。 www.yiibai.comhtml

Name Storage Size Description Range
int2 2 bytes small-range integer -32768 to +32767
int4 4 bytes typical choice for integer -2147483648 to +2147483647
int8 8 bytes large-range integer -9223372036854775808 to 9223372036854775807
decimal variable user-specified precision,exact up to 131072 digits before the decimal point; up to 16383 digits after the decimal point
float4 4 bytes variable-precision,inexact 6 decimal digits precision
float8 8 bytes variable-precision,inexact 15 decimal digits precision
serial2 2 bytes small autoincrementing integer 1 to 32767
serial4 4 bytes autoincrementing integer 1 to 2147483647
serial8 8 bytes large autoincrementing integer 1 to 9223372036854775807

貨幣類型

貨幣類型存儲的貨幣金額與一個固定的分數精度。能夠轉換爲金錢的數字,int和bigint數據類型的值。不推薦使用浮點數來處理金錢的潛力,因爲舍入偏差。git

 

Name Storage Size Description Range
money 8 bytes currency amount -92233720368547758.08 to +92233720368547758.07

字符類型

下表列出了可在PostgreSQL通用字符類型。編程

 

名稱 描述
varchar(n) variable-length with limit
char(n) fixed-length, blank padded
text variable unlimited length

二進制數據類型

bytea數據類型容許存儲二進制字符串,以下面的表格中說明。json

 

Name Storage Size Description
bytea 1 or 4 bytes plus the actual binary string variable-length binary string

日期/時間類型

PostgreSQL支持全套的SQL日期和時間類型,列於下表。根據公曆日期計算。在這裏,全部的類型有日期類型之外,其分辨率爲day1微秒/14位的解析度。數組

 

Name Storage Size Description Low Value High Value
timestamp [(p)] [without time zone ] 8 bytes both date and time (no time zone) 4713 BC 294276 AD
timestamp [(p) ] with time zone 8 bytes both date and time, with time zone 4713 BC 294276 AD
date 4 bytes date (no time of day) 4713 BC 5874897 AD
time [ (p)] [ without time zone ] 8 bytes time of day (no date) 00:00:00 24:00:00
time [ (p)] with time zone 12 bytes times of day only, with time zone 00:00:00+1459 24:00:00-1459
interval [fields ] [(p) ] 12 bytes time interval -178000000 years 178000000 years

布爾類型

PostgreSQL提供了標準的SQL類型布爾值。布爾類型能夠有幾種狀態:truefalse,和第三狀態null,這是SQL空值表示。網絡

 

Name Storage Size Description
boolean 1 byte state of true or false

枚舉類型

枚舉(枚舉)類型的數據類型,包括靜態,有序設置的值。在許多編程語言支持枚舉類型,它們是相等。app

Unlike other types, Enumerated Types need to be created using CREATE TYPE command. This type is used to store a static, ordered set of values, for example compass directions, i.e. NORTH, SOUTH, EAST, and WEST or days of the week as below:yii

 

CREATE TYPE week AS ENUM ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); 

枚舉一旦產生,它們能夠像任何其餘類型。編程語言

幾何類型

幾何數據類型表示二維空間對象。最根本的不一樣點是造成的全部其餘類型的基礎。ide

 

Name Storage Size Representation Description
point 16 bytes Point on a plane (x,y)
line 32 bytes Infinite line (not fully implemented) ((x1,y1),(x2,y2))
lseg 32 bytes Finite line segment ((x1,y1),(x2,y2))
box 32 bytes Rectangular box ((x1,y1),(x2,y2))
path 16+16n bytes Closed path (similar to polygon) ((x1,y1),...)
path 16+16n bytes Open path [(x1,y1),...]
polygon 40+16n Polygon (similar to closed path) ((x1,y1),...)
circle 24 bytes Circle <(x,y),r> (center point and radius)

網絡地址類型

PostgreSQL提供的數據類型來存儲的IPv4IPv6的地址和MAC地址。這是更好地使用這些類型,而不是純文本類型存儲網絡地址,由於這些類型提供輸入錯誤檢查和特殊的操做和函數。

 

Name Storage Size Description
cidr 7 or 19 bytes IPv4 and IPv6 networks
inet 7 or 19 bytes IPv4 and IPv6 hosts and networks
macaddr 6 bytes MAC addresses

位串類型

位串類型用於存儲位掩碼。他們要麼是0或1。 SQL位類型有兩種:(n)的位而變位(n)的,其中n是一個正整數 www.yiibai.com

文本搜索類型

這個類型支持全文檢索,這是經過天然語言文檔的集合的搜索,找到那些最符合查詢活動。這有兩種數據類型:

名稱 描述
tsvector This is a sorted list of distinct words that have been normalized to merge different variants of the same word, called as "lexemes".
tsquery This stores lexemes that are to be searched for, and combines them honoring the Boolean operators & (AND), | (OR), and ! (NOT). Parentheses can be used to enforce grouping of the operators.

UUID類型

一個UUID(通用惟一標識符)寫成小寫的十六進制數字序列,由連字號,特別是一組8位數字,而後由三組4位數字,而後由一組12位數字分開幾組,總32位,128位表明。

 

一個UUID的例子是: 550e8400-e29b-41d4-a716-446655440000

XML Type

xml數據類型能夠用來存儲XML數據。對於存儲XML數據,首先建立XML值函數XMLPARSE以下:

 

XMLPARSE (DOCUMENT '<?xml version="1.0"?> <tutorial> <title>PostgreSQL Tutorial </title> <topics>...</topics> </tutorial>') XMLPARSE (CONTENT 'xyz<foo>bar</foo><bar>foo</bar>') 

JSON類型

JSON數據類型能夠用來存儲JSON(JavaScript對象符號)數據。這樣的數據也能夠被存儲爲文本,但json數據類型具備的優勢是檢查每一個存儲的值是否爲有效的JSON值。也有相關的支持功能能夠直接用來處理JSON數據類型,以下所示:

 

Example Example Result
array_to_json('{{1,5},{99,100}}'::int[]) [[1,5],[99,100]]
row_to_json(row(1,'foo')) {"f1":1,"f2":"foo"}

陣列/數組類型

PostgreSQL的機會定義爲可變長度的多維數組的列一個表。任何內置或用戶定義的基本類型數組,枚舉類型,或者能夠建立複合型。

 

DECLARATION OF ARRAYS

數組類型能夠聲明爲:

 

CREATE TABLE monthly_savings ( name text, saving_per_quarter integer[], scheme text[][] ); 

或經過使用關鍵字「ARRAY」:

 

CREATE TABLE monthly_savings ( name text, saving_per_quarter integer ARRAY[4], scheme text[][] ); www.yiibai.com 

插入值

數組的值能夠插入一個文本常量,內附大括號內的元素值,並用逗號將它們隔開。例子以下:

 

INSERT INTO monthly_savings
VALUES ('Manisha', '{20000, 14600, 23500, 13250}', '{{"FD", "MF"}, {"FD", "Property"}}'); 

訪問數組

用於訪問陣列的一個例子以下所示。下面的命令將選擇人員,他們存儲在第二,第四個。 yiibai.com

SELECT name FROM monhly_savings WHERE saving_per_quarter[2] > saving_per_quarter[4]; 

修改數組

修改數組的一個例子以下所示。

 

UPDATE monthly_savings SET saving_per_quarter = '{25000,25000,27000,27000}' WHERE name = 'Manisha'; 

或數組表達式語法:

 

UPDATE monthly_savings SET saving_per_quarter = ARRAY[25000,25000,27000,27000] WHERE name = 'Manisha'; www.yiibai.com 

尋找ARRAYS

搜索數組的一個例子以下所示。

SELECT * FROM monthly_savings WHERE saving_per_quarter[1] = 10000 OR saving_per_quarter[2] = 10000 OR saving_per_quarter[3] = 10000 OR saving_per_quarter[4] = 10000; 

若是數組的大小是已知的上述搜索方法均可以使用。不然,下面的例子說明如什麼時候要搜索的大小是不知道的。

 

SELECT * FROM monthly_savings WHERE 10000 = ANY (saving_per_quarter); 

複合類型

此類型表明一個字段名和數據類型,即結構的一個表中的行或記錄列表。

 

複合類型聲明

下面的例子演示如何聲明一個複合類型:

 

CREATE TYPE inventory_item AS ( name text, supplier_id integer, price numeric ); www.yiibai.com 

此數據類型可用於在建立表以下所示: yiibai.com

CREATE TABLE on_hand ( item inventory_item, count integer ); 

複合值輸入

複合值能夠插入文字常量,封裝領域括號內的值,並用逗號將它們隔開。一個例子是以下:

 

INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000); yiibai.com 

此有效的定義同上的inventory_item的。行關鍵字其實是可選的表達式中,只要有一個以上的字段。

訪問複合類型

要訪問一個複合列的字段,字段名,使用點很像選擇字段從一個表名。例如,要選擇一些子字段,on_hand示例表的查詢將以下所示:

 

SELECT (item).name FROM on_hand WHERE (item).price > 9.99; 

甚至能夠使用表名(例如,在一個多表查詢),像這樣: yiibai.com

SELECT (on_hand.item).name FROM on_hand WHERE (on_hand.item).price > 9.99; 

範圍類型

範圍類型的數據類型,採用了一系列數據。範圍類型能夠是離散的範圍(例如,全部的整數值1到10)或連續範圍(例如任什麼時候間點的上午10:00到上午11:00)。

內置的範圍類型範圍包括:

  • int4range - Range of integer

     

  • int8range - Range of bigint

     

  • numrange - Range of numeric

     

  • tsrange - Range of timestamp without time zone

     

  • tstzrange - Range of timestamp with time zone yiibai.com

  • daterange - Range of date

     

能夠建立自定義的範圍類型,作出新的類型的適用範圍,如使用int類型爲基礎的IP地址範圍,或者使用浮點數據類型爲基礎的浮動範圍。

 

範圍類型支持包容性和排他性的範圍邊界分別使用[]和()個字符,例如: [4,9]'表明全部從包括4但不包括9的整數。

對象標識符類型

對象標識符(OID)內部使用PostgreSQL做爲各類系統表的主鍵。 OIDS IfWITH指定或default_with_oids配置變量,只有在這樣的狀況下啓用的OID被添加到用戶建立的表。下表列出了幾個別名類型。 OID別名類型有沒有本身的操做,除了專門的輸入和輸出過程。

 

Name References Description Value Example
oid any numeric object identifier 564182
regproc pg_proc function name sum
regprocedure pg_proc function with argument types sum(int4)
regoper pg_operator operator name +
regoperator pg_operator operator with argument types *(integer,integer) or -(NONE,integer)
regclass pg_class relation name pg_type
regtype pg_type data type name integer
regconfig pg_ts_config text search configuration english
regdictionary pg_ts_dict text search dictionary simple

僞類型

PostgreSQL類型系統包含了一些特殊用途的統稱爲僞類型的項。一個僞類型不能被用做列的數據類型,但它能夠用來聲明一個函數的參數或結果類型。下表列出了現有的僞類型。

 

名稱 描述
any Indicates that a function accepts any input data type.
anyelement Indicates that a function accepts any data type.
anyarray Indicates that a function accepts any array data type.
anynonarray Indicates that a function accepts any non-array data type.
anyenum Indicates that a function accepts any enum data type.
anyrange Indicates that a function accepts any range data type.
cstring Indicates that a function accepts or returns a null-terminated C string.
internal Indicates that a function accepts or returns a server-internal data type.
language_handler A procedural language call handler is declared to return language_handler.
fdw_handler A foreign-data wrapper handler is declared to return fdw_handler.
record Identifies a function returning an unspecified row type.
trigger A trigger function is declared to return trigger.
void Indicates that a function returns no value.
相關文章
相關標籤/搜索