postgresql - 数据类型

4 阅读2分钟
数据类型描述示例是否支持索引
integer存储整数值(4字节)。INSERT INTO users (age) VALUES (30);
bigint存储大整数值(8字节)。INSERT INTO users (balance) VALUES (10000000000);
smallint存储小整数值(2字节)。INSERT INTO users (level) VALUES (5);
decimal(p, s)存储精确的小数,p 为总位数,s 为小数位数。INSERT INTO products (price) VALUES (19.99);
numeric(p, s)与 decimal 相同,存储精确的小数。INSERT INTO products (discount) VALUES (0.15);
real存储单精度浮点数(4字节)。INSERT INTO measurements (temperature) VALUES (23.5);
double precision存储双精度浮点数(8字节)。INSERT INTO measurements (pressure) VALUES (1013.25);
serial自动递增的整数,通常用于主键。CREATE TABLE users (id SERIAL PRIMARY KEY);
bigserial自动递增的大整数。CREATE TABLE orders (id BIGSERIAL PRIMARY KEY);
boolean存储布尔值(TRUE 或 FALSE)。INSERT INTO settings (is_active) VALUES (TRUE);
char(n)固定长度的字符字符串。INSERT INTO codes (code) VALUES ('A123');
varchar(n)可变长度的字符字符串,最多 n 个字符。INSERT INTO names (first_name) VALUES ('John');
text可变长度的字符字符串,没有长度限制。INSERT INTO descriptions (detail) VALUES ('This is a description.');
date存储日期(年、月、日)。INSERT INTO events (event_date) VALUES ('2025-02-27');
time存储时间(不含时区)。INSERT INTO events (event_time) VALUES ('14:30:00');
timestamp存储时间戳(不含时区)。INSERT INTO logs (log_time) VALUES ('2025-02-27 14:30:00');
timestamptz存储带时区的时间戳。INSERT INTO logs (log_time) VALUES ('2025-02-27 14:30:00+08');
interval存储时间间隔。INSERT INTO durations (duration) VALUES ('2 hours 30 minutes');
json存储 JSON 数据。INSERT INTO data (info) VALUES ('{"key": "value"}');
jsonb存储二进制 JSON 数据,支持索引。INSERT INTO data (info) VALUES ('{"key": "value"}'::jsonb);
xml存储 XML 数据。INSERT INTO documents (content) VALUES ('<root><element>value</element></root>');
bytea存储二进制数据。INSERT INTO files (data) VALUES (decode('DEADBEEF', 'hex'));
uuid存储 UUID(通用唯一标识符)。INSERT INTO users (user_id) VALUES (uuid_generate_v4());
cidr存储网络地址(IPv4 或 IPv6)。INSERT INTO networks (subnet) VALUES ('192.168.1.0/24');
inet存储单个 IP 地址(IPv4 或 IPv6)。INSERT INTO servers (ip_address) VALUES ('192.168.1.1');
macaddr存储 MAC 地址。INSERT INTO devices (mac) VALUES ('08:00:20:4C:7A:2D');
jsonpath存储 JSON 路径数据。INSERT INTO data (path) VALUES ('$.store.book[*].author');
tsvector存储文本搜索向量。INSERT INTO documents (search_vector) VALUES (to_tsvector('PostgreSQL is awesome'));
tsquery存储文本搜索查询。INSERT INTO searches (query) VALUES (to_tsquery('awesome'));