postgresql的数据类型

PostgreSQL数据类型

 

本章讨论PostgreSQL数据类型。在创建表的同时,要求每列都要指定数据类型,即什么样的数据要存储在表中的字段。

 

这使几个好处: yiibai.com

  • 一致性: 对相同的数据类型的列的操作给出了一致的结果,通常是最快的。

     

  • 验证: 正确使用的数据类型表示数据和拒绝的范围之外的数据类型的数据格式验证。

     

  • 压缩: 作为一列可以存储单一类型的值,它被存储在一个紧凑的方式。 yiibai.com

  • 性能: 适当地使用的数据类型给出最有效的数据存储。存储的值可以被快速处理,从而提高性能。

     

PostgreSQL支持一系列广泛的数据类型。此外,用户可以使用SQL命令CREATE TYPE创建自己的自定义数据类型。在PostgreSQL中有不同类别的数据类型。如下:

 

数值类型

数值类型由两个字节,4字节和8字节的整数,4字节和8字节的浮点数和可选精度的小数。下表列出了可用的类型。 www.yiibai.com

NameStorage SizeDescriptionRange
smallint2 bytessmall-range integer-32768 to +32767
integer4 bytestypical choice for integer-2147483648 to +2147483647
bigint8 byteslarge-range integer-9223372036854775808 to 9223372036854775807
decimalvariableuser-specified precision,exactup to 131072 digits before the decimal point; up to 16383 digits after the decimal point
numericvariableuser-specified precision,exactup to 131072 digits before the decimal point; up to 16383 digits after the decimal point
real4 bytesvariable-precision,inexact6 decimal digits precision
double precision8 bytesvariable-precision,inexact15 decimal digits precision
smallserial2 bytessmall autoincrementing integer1 to 32767
serial4 bytesautoincrementing integer1 to 2147483647
bigserial8 byteslarge autoincrementing integer1 to 9223372036854775807

货币类型

货币类型存储的货币金额与一个固定的分数精度。可以转换为金钱的数字,int和bigint数据类型的值。不推荐使用浮点数来处理金钱的潜力,由于舍入误差。

 

NameStorage SizeDescriptionRange
money8 bytescurrency amount-92233720368547758.08 to +92233720368547758.07

字符类型

下表列出了可在PostgreSQL通用字符类型。

 

名称描述
character varying(n), varchar(n)variable-length with limit
character(n), char(n)fixed-length, blank padded
textvariable unlimited length

二进制数据类型

bytea数据类型允许存储二进制字符串,如下面的表格中说明。

 

NameStorage SizeDescription
bytea1 or 4 bytes plus the actual binary stringvariable-length binary string

日期/时间类型

PostgreSQL支持全套的SQL日期和时间类型,列于下表。根据公历日期计算。在这里,所有的类型有日期类型以外,其分辨率为day1微秒/14位的解析度。

 

NameStorage SizeDescriptionLow ValueHigh Value
timestamp [(p)] [without time zone ]8 bytesboth date and time (no time zone)4713 BC294276 AD
timestamp [(p) ] with time zone8 bytesboth date and time, with time zone4713 BC294276 AD
date4 bytesdate (no time of day)4713 BC5874897 AD
time [ (p)] [ without time zone ]8 bytestime of day (no date)00:00:0024:00:00
time [ (p)] with time zone12 bytestimes of day only, with time zone00:00:00+145924:00:00-1459
interval [fields ] [(p) ]12 bytestime interval-178000000 years178000000 years

布尔类型

PostgreSQL提供了标准的SQL类型布尔值。布尔类型可以有几种状态:truefalse,和第三状态null,这是SQL空值表示。

 

NameStorage SizeDescription
boolean1 bytestate of true or false

枚举类型

枚举(枚举)类型的数据类型,包括静态,有序设置的值。在许多编程语言支持枚举类型,它们是相等。

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:

 

<span style="color:#000000">CREATE TYPE week AS ENUM <span style="color:#666600">(</span><span style="color:#008800">'Mon'</span><span style="color:#666600">,</span> <span style="color:#008800">'Tue'</span><span style="color:#666600">,</span> <span style="color:#008800">'Wed'</span><span style="color:#666600">,</span> <span style="color:#008800">'Thu'</span><span style="color:#666600">,</span> <span style="color:#008800">'Fri'</span><span style="color:#666600">,</span> <span style="color:#008800">'Sat'</span><span style="color:#666600">,</span> <span style="color:#008800">'Sun'</span><span style="color:#666600">);</span>  </span>

枚举一旦产生,它们可以像任何其他类型。

几何类型

几何数据类型表示二维空间对象。最根本的不同点是形成的所有其他类型的基础。

 

NameStorage SizeRepresentationDescription
point16 bytesPoint on a plane(x,y)
line32 bytesInfinite line (not fully implemented)((x1,y1),(x2,y2))
lseg32 bytesFinite line segment((x1,y1),(x2,y2))
box32 bytesRectangular box((x1,y1),(x2,y2))
path16+16n bytesClosed path (similar to polygon)((x1,y1),...)
path16+16n bytesOpen path[(x1,y1),...]
polygon40+16nPolygon (similar to closed path)((x1,y1),...)
circle24 bytesCircle<(x,y),r> (center point and radius)

网络地址类型

PostgreSQL提供的数据类型来存储的IPv4IPv6的地址和MAC地址。这是更好地使用这些类型,而不是纯文本类型存储网络地址,因为这些类型提供输入错误检查和特殊的操作和函数。

 

NameStorage SizeDescription
cidr7 or 19 bytesIPv4 and IPv6 networks
inet7 or 19 bytesIPv4 and IPv6 hosts and networks
macaddr6 bytesMAC addresses

位串类型

位串类型用于存储位掩码。他们要么是0或1。 SQL位类型有两种:(n)的位而变位(n)的,其中n是一个正整数 www.yiibai.com

文本搜索类型

这个类型支持全文检索,这是通过自然语言文档的集合的搜索,找到那些最符合查询活动。这有两种数据类型:

名称描述
tsvectorThis is a sorted list of distinct words that have been normalized to merge different variants of the same word, called as "lexemes".
tsqueryThis 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如下:

 

<span style="color:#000000">XMLPARSE <span style="color:#666600">(</span>DOCUMENT <span style="color:#008800">'<?xml version="1.0"?>
<tutorial>
<title>PostgreSQL Tutorial </title>
   <topics>...</topics>
</tutorial>'</span><span style="color:#666600">)</span>

XMLPARSE <span style="color:#666600">(</span>CONTENT <span style="color:#008800">'xyz<foo>bar</foo><bar>foo</bar>'</span><span style="color:#666600">)</span>  </span>

JSON类型

JSON数据类型可以用来存储JSON(JavaScript对象符号)数据。这样的数据也可以被存储为文本,但json数据类型具有的优点是检查每个存储的值是否为有效的JSON值。也有相关的支持功能可以直接用来处理JSON数据类型,如下所示:

 

ExampleExample 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

数组类型可以声明为:

 

<span style="color:#000000">CREATE TABLE monthly_savings <span style="color:#666600">(</span>
   name text<span style="color:#666600">,</span>
   saving_per_quarter integer<span style="color:#666600">[],</span>
   scheme text<span style="color:#666600">[][]</span>
<span style="color:#666600">);</span>  </span>

或通过使用关键字“ARRAY”:

 

<span style="color:#000000">CREATE TABLE monthly_savings <span style="color:#666600">(</span>
   name text<span style="color:#666600">,</span>
   saving_per_quarter integer ARRAY<span style="color:#666600">[</span><span style="color:#006666">4</span><span style="color:#666600">],</span>
   scheme text<span style="color:#666600">[][]</span>
<span style="color:#666600">);</span> <span style="color:#FFFFFF">www.yiibai.com</span> </span>

插入值

数组的值可以插入一个文本常量,内附大括号内的元素值,并用逗号将它们隔开。例子如下:

 

<span style="color:#000000">INSERT INTO monthly_savings
VALUES <span style="color:#666600">(</span><span style="color:#008800">'Manisha'</span><span style="color:#666600">,</span>
<span style="color:#008800">'{20000, 14600, 23500, 13250}'</span><span style="color:#666600">,</span>
<span style="color:#008800">'{{"FD", "MF"}, {"FD", "Property"}}'</span><span style="color:#666600">);</span>  </span>

访问数组

用于访问阵列的一个例子如下所示。下面的命令将选择人员,他们存储在第二,第四个。 yiibai.com

<span style="color:#000000">SELECT name FROM monhly_savings WHERE saving_per_quarter<span style="color:#666600">[</span><span style="color:#006666">2</span><span style="color:#666600">]</span> <span style="color:#666600">></span> saving_per_quarter<span style="color:#666600">[</span><span style="color:#006666">4</span><span style="color:#666600">];</span>  </span>

修改数组

修改数组的一个例子如下所示。

 

<span style="color:#000000">UPDATE monthly_savings SET saving_per_quarter <span style="color:#666600">=</span> <span style="color:#008800">'{25000,25000,27000,27000}'</span>
WHERE name <span style="color:#666600">=</span> <span style="color:#008800">'Manisha'</span><span style="color:#666600">;</span>  </span>

或数组表达式语法:

 

<span style="color:#000000">UPDATE monthly_savings SET saving_per_quarter <span style="color:#666600">=</span> ARRAY<span style="color:#666600">[</span><span style="color:#006666">25000</span><span style="color:#666600">,</span><span style="color:#006666">25000</span><span style="color:#666600">,</span><span style="color:#006666">27000</span><span style="color:#666600">,</span><span style="color:#006666">27000</span><span style="color:#666600">]</span>
WHERE name <span style="color:#666600">=</span> <span style="color:#008800">'Manisha'</span><span style="color:#666600">;</span> <span style="color:#FFFFFF">www.yiibai.com</span> </span>

寻找ARRAYS

搜索数组的一个例子如下所示。

<span style="color:#000000">SELECT <span style="color:#666600">*</span> FROM monthly_savings WHERE saving_per_quarter<span style="color:#666600">[</span><span style="color:#006666">1</span><span style="color:#666600">]</span> <span style="color:#666600">=</span> <span style="color:#006666">10000</span> OR
saving_per_quarter<span style="color:#666600">[</span><span style="color:#006666">2</span><span style="color:#666600">]</span> <span style="color:#666600">=</span> <span style="color:#006666">10000</span> OR
saving_per_quarter<span style="color:#666600">[</span><span style="color:#006666">3</span><span style="color:#666600">]</span> <span style="color:#666600">=</span> <span style="color:#006666">10000</span> OR
saving_per_quarter<span style="color:#666600">[</span><span style="color:#006666">4</span><span style="color:#666600">]</span> <span style="color:#666600">=</span> <span style="color:#006666">10000</span><span style="color:#666600">;</span>  </span>

如果数组的大小是已知的上述搜索方法都可以使用。否则,下面的例子说明如何时要搜索的大小是不知道的。

 

<span style="color:#000000">SELECT <span style="color:#666600">*</span> FROM monthly_savings WHERE <span style="color:#006666">10000</span> <span style="color:#666600">=</span> ANY <span style="color:#666600">(</span>saving_per_quarter<span style="color:#666600">);</span>  </span>

复合类型

此类型代表一个字段名和数据类型,即结构的一个表中的行或记录列表。

 

复合类型声明

下面的例子演示如何声明一个复合类型:

 

<span style="color:#000000">CREATE TYPE inventory_item AS <span style="color:#666600">(</span>
   name text<span style="color:#666600">,</span>
   supplier_id integer<span style="color:#666600">,</span>
   price numeric
<span style="color:#666600">);</span> <span style="color:#FFFFFF">www.yiibai.com</span> </span>

此数据类型可用于在创建表如下所示: yiibai.com

<span style="color:#000000">CREATE TABLE on_hand <span style="color:#666600">(</span>
   item inventory_item<span style="color:#666600">,</span>
   count integer
<span style="color:#666600">);</span>  </span>

复合值输入

复合值可以插入文字常量,封装领域括号内的值,并用逗号将它们隔开。一个例子是如下:

 

<span style="color:#000000">INSERT INTO on_hand VALUES <span style="color:#666600">(</span>ROW<span style="color:#666600">(</span><span style="color:#008800">'fuzzy dice'</span><span style="color:#666600">,</span> <span style="color:#006666">42</span><span style="color:#666600">,</span> <span style="color:#006666">1.99</span><span style="color:#666600">),</span> <span style="color:#006666">1000</span><span style="color:#666600">);</span> <span style="color:#FFFFFF">yiibai.com</span> </span>

此有效的定义同上的inventory_item的。行关键字实际上是可选的表达式中,只要有一个以上的字段。

访问复合类型

要访问一个复合列的字段,字段名,使用点很像选择字段从一个表名。例如,要选择一些子字段,on_hand示例表的查询将如下所示:

 

<span style="color:#000000">SELECT <span style="color:#666600">(</span>item<span style="color:#666600">).</span>name FROM on_hand WHERE <span style="color:#666600">(</span>item<span style="color:#666600">).</span>price <span style="color:#666600">></span> <span style="color:#006666">9.99</span><span style="color:#666600">;</span>  </span>

甚至可以使用表名(例如,在一个多表查询),像这样: yiibai.com

<span style="color:#000000">SELECT <span style="color:#666600">(</span>on_hand<span style="color:#666600">.</span>item<span style="color:#666600">).</span>name FROM on_hand WHERE <span style="color:#666600">(</span>on_hand<span style="color:#666600">.</span>item<span style="color:#666600">).</span>price <span style="color:#666600">></span> <span style="color:#006666">9.99</span><span style="color:#666600">;</span>  </span>

范围类型

范围类型的数据类型,采用了一系列数据。范围类型可以是离散的范围(例如,所有的整数值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别名类型有没有自己的操作,除了专门的输入和输出过程。

 

NameReferencesDescriptionValue Example
oidanynumeric object identifier564182
regprocpg_procfunction namesum
regprocedurepg_procfunction with argument typessum(int4)
regoperpg_operatoroperator name+
regoperatorpg_operatoroperator with argument types*(integer,integer) or -(NONE,integer)
regclasspg_classrelation namepg_type
regtypepg_typedata type nameinteger
regconfigpg_ts_configtext search configurationenglish
regdictionarypg_ts_dicttext search dictionarysimple

伪类型

PostgreSQL类型系统包含了一些特殊用途的统称为伪类型的项。一个伪类型不能被用作列的数据类型,但它可以用来声明一个函数的参数或结果类型。下表列出了现有的伪类型。

名称描述
anyIndicates that a function accepts any input data type.
anyelementIndicates that a function accepts any data type.
anyarrayIndicates that a function accepts any array data type.
anynonarrayIndicates that a function accepts any non-array data type.
anyenumIndicates that a function accepts any enum data type.
anyrangeIndicates that a function accepts any range data type.
cstringIndicates that a function accepts or returns a null-terminated C string.
internalIndicates that a function accepts or returns a server-internal data type.
language_handlerA procedural language call handler is declared to return language_handler.
fdw_handlerA foreign-data wrapper handler is declared to return fdw_handler.
recordIdentifies a function returning an unspecified row type.
triggerA trigger function is declared to return trigger.
voidIndicates that a function returns no value.

 

文章转载自:易百教程 [http://www.yiibai.com]
本文标题:PostgreSQL数据类型
转载请保留原文链接:http://www.yiibai.com/html/postgresql/2013/080435.html

上一篇:PostgreSQL语法      下一篇:PostgreSQL创建数据库

随机推荐

  • PostgreSQL安装和环境设置 PostgreSQL教程 PostgreSQL创建表 PostgreSQL DELETE查询 PostgreSQL语法 PostgreSQL数据类型 PostgreSQL创建数据库 PostgreSQL概述 PostgreSQL模式Schema

相关文章

 

易百教程提供的内容仅用于培训。我们不保证内容的正确性。通过使用本站内容而可能带来的风险与本站无关。易百教程的所有内容仅供测试,对任何法律问题及风险不承担任何责任。 当使用本站时,代表您已接受了本站的使用条款和隐私条款。版权所有,保留一切权利!

Copyright © 2012-2015. 易百教程版权所有    琼ICP备13001417号-3   联系我们:yiibai.com#gmail.com(请把# 替换成 @ )

免责声明

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值