📅  最后修改于: 2020-11-30 01:32:26             🧑  作者: Mango
数据类型指定要在表字段中存储的数据类型。创建表时,对于每一列,您必须使用数据类型。它标识特定类型的数据,例如整数,布尔值,浮点数,等等。
在本节中,我们将讨论PostgreSQL中使用的各种数据类型。
在PostgreSQL中,每个数据库表都有许多列,并且每一列都有精确的数据类型。它支持广泛的数据类型。
此外,用户可以使用CREATE TYPE SQL命令创建其自定义数据类型。这些数据类型具有以下优点:
在PostgreSQL中,我们有许多数据类型集。让我们一一理解它们:
数字数据类型用于将数字数据指定到表中。它包含以下内容:
下表包含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代表通用唯一标识符,它是由算法创建的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 |
PostgreSQL提供了两种数据类型来存储JSON(JavaScript对象符号)数据。
杰森
它是具有JSON验证的文本数据类型的扩展。在这种情况下,我们可以快速插入数据,但是数据检索相对较慢。它以包含空格的方式保存输入的数据。它还正在对数据检索进行重新处理。
强生
它是JSON数据的二进制表示形式。它还与索引兼容,并且还改善了空白以使检索更快。在这种情况下,插入速度较慢,但数据检索较快,并且需要对数据检索进行重新处理。
位字符串数据类型包含两类字符串,分别为1和0 。可以在这些字符串的帮助下存储位掩码。在此,我们有两种SQL位,例如:
在此, n是正整数。
在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类型本身本身几乎没有可以转换为整数的运算,并且可以使用标准整数运算符。
下表包含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数据类型可用于存储日志序列号(LSN)数据,它是XLOG中某个位置的指针。它用于表示XLogRecPtr和PostgreSQL的内部系统类型。所述pg_lsn类型与标准比较运算符,如>和=兼容。
注意:
在使用数据类型时,我们可以参考以下几点: