数值类型由两个字节,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类型布尔值。布尔类型能够有几种状态:true,false,和第三状态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提供的数据类型来存储的IPv4,IPv6的地址和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(通用惟一标识符)写成小写的十六进制数字序列,由连字号,特别是一组8位数字,而后由三组4位数字,而后由一组12位数字分开几组,总32位,128位表明。
一个UUID的例子是: 550e8400-e29b-41d4-a716-446655440000
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(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的机会定义为可变长度的多维数组的列一个表。任何内置或用户定义的基本类型数组,枚举类型,或者能够建立复合型。
数组类型能够声明为:
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
搜索数组的一个例子以下所示。
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. |