zoukankan      html  css  js  c++  java
  • PostgreSQL 数据类型

    数值类型

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

    NameStorage SizeDescriptionRange
    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数据类型的值。不推荐使用浮点数来处理金钱的潜力,由于舍入误差。

    NameStorage SizeDescriptionRange
    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数据类型允许存储二进制字符串,如下面的表格中说明。

    NameStorage SizeDescription
    bytea 1 or 4 bytes plus the actual binary string variable-length binary string

    日期/时间类型

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

    NameStorage SizeDescriptionLow ValueHigh 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空值表示。

    NameStorage SizeDescription
    boolean 1 byte state 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:

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

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

    几何类型

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

    NameStorage SizeRepresentationDescription
    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地址。这是更好地使用这些类型,而不是纯文本类型存储网络地址,因为这些类型提供输入错误检查和特殊的操作和函数。

    NameStorage SizeDescription
    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数据类型,如下所示:

    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

    数组类型可以声明为:

    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别名类型有没有自己的操作,除了专门的输入和输出过程。

    NameReferencesDescriptionValue 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.

  • 相关阅读:
    在存储过程中如何拼接字符串参数
    NHibernate学习(二)测试NHibernate如何工作
    GridView模板中用Checkbox实现全选删除
    自定义DataList,GridView通过DataBinder.Eval方法绑定的数据
    Asp.net中使用基于角色role的Forms验证
    GridView中对数据编辑打开一个新页面
    NHibernate学习()在NHibernate框架新建项目与配置文件
    Select for update nowait
    Oracle:Virtual Columns in Oracle Database 11g Release 1
    Oracle:PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1
  • 原文地址:https://www.cnblogs.com/linguoguo/p/10709448.html
Copyright © 2011-2022 走看看