Skip to Content
PostgreSQL:快速入门,第3版
book

PostgreSQL:快速入门,第3版

by Regina O. Obe, Leo S. Hsu
May 2025
Intermediate to advanced
314 pages
4h 10m
Chinese
O'Reilly Media, Inc.
Content preview from PostgreSQL:快速入门,第3版

第 5 章 数据类型 数据类型

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

PostgreSQL 支持所有数据库的主力数据类型:数字、字符串、日期、时间和布尔。不过,PostgreSQL还在不断进步,增加了对数组、时区感知日期、时间间隔、范围、JSON、XML等的支持。如果这还不够,你还可以发明自定义类型。在本章中,我们不打算涵盖所有数据类型。关于这一点,您可以参阅手册。我们将展示 PostgreSQL 独有的数据类型,以及 PostgreSQL 如何处理常见数据类型的细微差别。

没有函数和操作符的支持,任何数据类型都不会有用。PostgreSQL 拥有大量这样的函数和操作符。我们将在本章介绍比较常用的函数。

提示

当我们使用函数一词时,,我们指的是形式为f(x) 的东西。当我们使用运算符时,所指的是符号型运算符,可以是一元运算符(有一个参数),也可以是二元运算符(有两个参数),如+,-,*/ 。在使用运算符时,请记住,相同的符号在应用于不同的数据类型时可能会有不同的含义。例如,加号对数字表示相加,而对范围表示联合。

数字

您可以在 PostgreSQL 中找到日常整数、小数和浮点数。在数字类型中,我们要讨论的是序列数据类型和一个快速生成整数算术级数的巧妙函数。

连续出版物

序列及其更大的同胞兄弟 bigserial 是自动递增的 整数,通常用作表的主键,在表中没有明显的自然键。这种数据类型在不同的数据库产品中有不同的名称,其中autonumber是最常见的替代名称。创建表格并指定列为序列时,PostgreSQL 会首先创建一个整数列,然后创建一个名为 table_name_column_name_seq 的序列对象。然后,它会设置新整数列的默认值,以便从序列中读取其值。如果删除列,PostgreSQL 也会删除相应的序列对象。

在 PostgreSQL 中,序列类型本身就是一种数据库资产。您可以使用 SQL ALTER SEQUENCE 命令或 PGAdmin 检查和编辑序列。您可以设置当前值、边界值(上界和下界),甚至每次递增多少个数字。虽然递减很少见,但可以通过将增量值设置为负数来实现。由于序列是独立的数据库资产,因此可以使用 CREATE SEQUENCE命令将序列与表分开创建,并可在多个表中使用同一序列。在数据库中分配通用键时,跨表共享同一序列就派上用场了。

要在后续表中使用现存序列,请在表中新建一列整数或 bigint 而不是序列,然后使用 函数设置该列的默认值,如例 5-1 所示。 nextval(sequence_name) 函数设置列的默认值,如例 5-1 所示。

例 5-1. 将现有序列用于新表格
CREATE SEQUENCE s START 1;
CREATE TABLE stuff(id bigint DEFAULT nextval('s') PRIMARY KEY, name text);
警告

如果重命名一个序列基于序列的表,PostgreSQL 不会自动重命名序列对象。为避免混淆,应重新命名序列对象。

生成系列函数

PostgreSQL 有一个别致的函数,名为 generate_series功能,这是其他数据库产品所没有的。该函数有两种形式。一种是数字版本,用于创建一个按某个值递增的整数序列;另一种是数字版本,用于创建一个按某个时间间隔递增的日期或时间戳序列。generate_series 之所以如此方便,是因为它可以有效地模仿 SQL 中的 for 循环。例 5-2演示了数字版本。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Docker:入门与实践,第三版

Docker:入门与实践,第三版

Sean P. Kane, Karl Matthias
AI工程

AI工程

Chip Huyen

Publisher Resources

ISBN: 9798341658431