📜  PostgreSQL数据类型

📅  最后修改于: 2020-11-30 01:32:26             🧑  作者: Mango

PostgreSQL数据类型

数据类型指定要在表字段中存储的数据类型。创建表时,对于每一列,您必须使用数据类型。它标识特定类型的数据,例如整数,布尔值,浮点数,等等。

在本节中,我们将讨论PostgreSQL中使用的各种数据类型。

PostgreSQL中,每个数据库表都有许多列,并且每一列都有精确的数据类型。它支持广泛的数据类型。

此外,用户可以使用CREATE TYPE SQL命令创建其自定义数据类型。这些数据类型具有以下优点:

  • 性能:如果我们正确有效地使用这些数据类型来存储数据值,它将提高我们的性能。
  • 验证:数据类型的正确使用涉及数据的验证和数据类型范围之外的数据的删除。
  • 紧凑性:它可以高效地存储,因为列可以存储一种类型的值。
  • 一致性:与相同数据类型的列相矛盾的活动可提供可靠的结果,通常是最快的。

在PostgreSQL中,我们有许多数据类型集。让我们一一理解它们:

  • 数值数据类型
  • 字符数据类型
  • 日期/时间数据类型
  • 货币数据类型
  • 二进制数据类型
  • 布尔数据类型
  • 枚举数据类型
  • 几何数据类型
  • 文字搜索数据类型
  • UUID数据类型
  • 网络地址类型
  • JSON数据类型
  • 位字符串类型
  • XML数据类型
  • 范围数据类型
  • 数组
  • 复合数据类型
  • 对象标识符类型
  • 伪数据类型
  • pg-Isn数据类型

数值数据类型

数字数据类型用于将数字数据指定到表中。它包含以下内容:

  • 四字节和八字节浮点数
  • 二,四和八字节整数
  • 可选精度的小数

下表包含PostgreSQL支持的所有数字数据类型:

name description storage size range
smallint stores whole numbers, small range. 2 bytes -32768 to +32767
integer stores whole numbers.use this when you want to store typical integers. 4 bytes -2147483648 to +2147483647
bigint stores whole numbers, large range. 8 bytes -9223372036854775808 to 9223372036854775807
decimal user-specified precision, exact variable up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.
numeric user-specified precision, exact variable
up to 131072 digits before the decimal point; up to 16383 digits after the decimal point.
real
variable-precision, inexact
4 bytes 6 decimal digits precision.
double precision variable-precision, inexact 8 bytes 15 decimal digits precision
serial auto incrementing integer 4 bytes 1 to 2147483647
bigserial large auto incrementing integer 8 bytes 1 to 9223372036854775807

字符数据类型

在PostgreSQL中,我们有各种通用字符数据类型,这些数据类型用于表示字符类型值。

下表包含PostgreSQL支持的所有字符数据类型:

Datatype Explanation
char(size)
Here size is the number of characters to store. Fixed-length strings. Space padded on right to equal size characters.
character(size) Here size is the number of characters to store. Fixed-length strings. Space padded on right to equal size characters.
varchar(size) Here size is the number of characters to store. Variable-length string.
character varying(size) Here size is the number of characters to store. Variable-length string.
text Variable-length string.

日期/时间数据类型

PostgreSQL支持完整的SQL日期和时间数据类型集。日期/时间数据类型用于使用日期和时间值表示列。根据公历计算日期。

下表包含PostgreSQL支持的所有日期/时间数据类型:

Name Description Storage size Minimum value Maximum value Resolution
timestamp [ (p) ] [ without time zone ] both date and time (no time zone) 8 bytes 4713 bc 294276 ad 1 microsecond / 14 digits
timestamp [ (p) ] with time zone both date and time, with time zone 8 bytes 4713 bc 294276 ad 1 microsecond / 14 digits
date date (no time of day) 4 bytes 4713 bc 5874897 ad 1 day
time [ (p) ] [ without time zone ] time of day (no date) 8 bytes 00:00:00 24:00:00 1 microsecond / 14 digits
time [ (p) ] with time zone times of day only, with time zone 12 bytes 00:00:00+1459 24:00:00-1459 1 microsecond / 14 digits
interval [ fields ] [ (p) ] time interval 12 bytes -178000000 years 178000000 years 1 microsecond / 14 digits

货币类型:

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

二进制数据类型

如下表所示, bytea数据类型允许存储二进制字符串:

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

布尔型

PostgreSQL包含标准的SQL类型boolean ;布尔类型具有许多状态,例如true和false ,以及由SQL null值表示的第三个状态unknown。

Name Description Storage size
boolean it specifies the state of true or false. 1 byte

布尔数据类型输入函数接收以下关于true和false状态的字符串演示:

枚举类型

在PostgreSQL中,枚举数据类型包括一组静态的有序值。

它类似于与各种编程语言兼容的枚举类型。枚举数据类型在表中用外键表示,以确保数据完整性。

例如:

CREATE TYPE fruits_name AS ENUM
('Mango','Apple','Orange','Strawberry')

几何数据类型

几何数据类型表示二维空间对象。最基本的类型(一点)构成了所有其他类型的基础。

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中,全文搜索数据类型用于搜索自然语言文档的集合。我们有两种与全文搜索兼容的数据类型。

Data types Description
tsvector It is used to display a document in a form, which enhance text search.
tsquery It is used to represent a text query.

UUID数据类型

UUID代表通用唯一标识符,它是由算法创建的128位数量。这是最适合主键的数据类型。 UUID通过由连字符分隔的多个集合写为一组小写的十六进制数字。

例如

a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11

注意:PostgreSQL还同意采用其他形式的UUID输入,例如不使用连字符,大写字母,大括号等。

网络地址数据类型

PostgreSQL提供了用于存储Mac地址,IPv4和IPv6的数据类型,如下表所示。除了提供纯文本类型以外,它还增强了这些类型以包含网络地址,因为它们提供了输入错误检查以及特定的函数和运算符。

下表包含PostgreSQL支持的所有网络地址数据类型:

Data type Description Storage Size
inet It stores the IPv4 and IPv6 hosts and networks. 7 or 19 bytes
cidr It is used to store the IPv4 and IPv6 networks. 7 or 19 bytes
macaddr It stores the MAC addresses. 6 bytes

JSON数据类型

PostgreSQL提供了两种数据类型来存储JSON(JavaScript对象符号)数据。

  • JSON格式
  • JSONB

杰森

它是具有JSON验证的文本数据类型的扩展。在这种情况下,我们可以快速插入数据,但是数据检索相对较慢。它以包含空格的方式保存输入的数据。它还正在对数据检索进行重新处理。

强生

它是JSON数据的二进制表示形式。它还与索引兼容,并且还改善了空白以使检索更快。在这种情况下,插入速度较慢,但数据检索较快,并且需要对数据检索进行重新处理。

位字符串类型

位字符串数据类型包含两类字符串,分别为1和0 。可以在这些字符串的帮助下存储位掩码。在此,我们有两种SQL位,例如:

  • 位变化( n )
  • 位( n )

在此, n是正整数。

XML类型

在PostgreSQL中,XML数据类型用于存储XML数据。 XML数据类型的函数是检查输入XML的格式是否正确,并且还具有支持功能以对其执行类型安全的操作。

例如:

XMLPARSE (DOCUMENT 'PostgreSQL...')

范围类型

这些数据类型用于显示某些元素类型的值的范围,称为范围的子类型。它还表示单个范围值中的几个值元素。在此,我们还可以创建我们的范围类型。

在PostgreSQL中,我们具有以下内置范围类型:

Built-in range types Description
tsrange Range of timestamp without time zone
tstzrange Range of timestamp with time zone
daterange Range of date
int4range Range of integer
int8range Range of bigint
numrange Range of numeric

数组类型

在这种情况下,PostgreSQL提供了一列表作为可变长度和多维数组。我们可以创建任何用户定义的基本类型,内置,复合和枚举类型数组。

在这里,我们可以对数组执行各种操作,例如声明,插入,访问,修改和搜索。

复合类型

在PostgreSQL中,复合数据类型用于表示行的结构或记录为文件名和数据类型的列表。

伪数据类型

在PostgreSQL中,数据类型是伪类型,用于包含许多特殊用途的条目。并且它用于声明结果类型或函数的参数,但是与用作列数据类型不兼容。

下表包含一些在PostgreSQL中支持的常用伪数据类型:

Pseudo data types Description
any It shows that a function accepts any input data type.
anyelement It accepts any data type.
anyarray It shows a function that accepts any array data type
anyenum It accepts any enum data type
anyrange It accepts any range of data type
cstring It is used to specify that a function accepts or returns a null-terminated C string.
language_handler A procedural language call handler is declared to return language_handler.
fdw_handler The fdw (foreign-data wrapper) handler is declared to return fdw_handler.
record It is used to specify a function which is taking or returning an unspecified row type.
trigger It is declared to return the trigger.
pg_ddl_command It is used to represent the DDL commands that are available to event triggers.
void It is used to specify that a function returns no value.

对象标识符(OID)类型

这些类型的数据类型用作几个系统表的主键。 oid类型代表对象标识符,当前实现为无符号的四字节整数。在大型数据库甚至大型个人表中,它不足以提供整个数据库范围的个性。

对象标识符用于引用系统表。除了比较之外,oid类型本身本身几乎没有可以转换为整数的运算,并且可以使用标准整数运算符。

下表包含PostgreSQL支持的所有对象标识符数据类型:

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

pg_lsn类型

pg_lsn数据类型可用于存储日志序列号(LSN)数据,它是XLOG中某个位置的指针。它用于表示XLogRecPtr和PostgreSQL的内部系统类型。所述pg_lsn类型与标准比较运算符,如>和=兼容

注意:

在使用数据类型时,我们可以参考以下几点:

  • 如果我们有IEEE 754数据源,则可以使用float数据类型
  • 对于整数数据类型,我们可以使用int
  • 永远不要使用char
  • 如果要限制输入,可以应用文本数据类型。
  • 当我们拥有大量数据时,我们只能使用bigint

总览

  • 字符数据类型用于存储文本值。
  • 网络地址类型用于优化网络数据的存储。
  • 二进制字符串是字节或八位字节的分类。
  • PostgreSQL提供两种不同类型的数字,例如浮点数和整数
  • 范围数据类型用于显示某些元素类型的值的范围。
  • 布尔数据类型具有三个值,例如True,False和Null。
  • UUID数据类型是128位的数量,它是通过算法生成的。
  • 复合数据类型用于表示行或记录的结构。
  • PostgreSQL(枚举数据类型)很少用于演示修改后的信息,例如分支ID或国家/地区代码。
  • 为了定义几种格式的日期和时间信息,PostgreSQL提供了日期和时间数据类型。
  • 对象标识符数据类型表示对象标识符。
  • PostgreSQL设计了文本搜索数据以支持全文搜索。
  • Geometric数据类型用于表示二维空间对象。
  • 在PostgreSQL中,伪类型用于许多特殊用途的条目。
  • pg_lsn数据类型用于存储日志序列号(LSN)数据。