在 Postgres 中管理枚举
Postgres 中的枚举(Enums)是一种自定义数据类型。它们允许你定义一组列可以持有的值(或标签)。当某一列的可能值是固定的一组时,它们非常有用。
🌐 Enums in Postgres are a custom data type. They allow you to define a set of values (or labels) that a column can hold. They are useful when you have a fixed set of possible values for a column.
创建枚举 #
🌐 Creating enums
你可以使用 create type 语句来定义一个 Postgres 枚举。来看一个例子:
🌐 You can define a Postgres Enum using the create type statement. Here's an example:
1create type mood as enum (2 'happy',3 'sad',4 'excited',5 'calm'6);在这个例子中,我们创建了一个叫做“mood”的枚举,有四个可能的值。
🌐 In this example, we've created an Enum called "mood" with four possible values.
什么时候使用枚举 #
🌐 When to use enums
枚举和外键之间有很多重叠之处。两者都可以用来为列定义一组值。不过,使用枚举有一些优势:
🌐 There is a lot of overlap between Enums and foreign keys. Both can be used to define a set of values for a column. However, there are some advantages to using Enums:
- 性能:你可以查询单个表,而不是从查找表中找值。
- 简单性:通常 SQL 更容易阅读和编写。
使用枚举也有一些缺点:
🌐 There are also some disadvantages to using Enums:
- 有限的灵活性:添加和删除值需要修改数据库结构(比如使用迁移),而不是直接在表里加数据。
- 维护开销:枚举类型需要持续维护。如果你的应用需求经常变化,维护枚举可能会变得很麻烦。
一般来说,你应该只在值的列表很小、固定,并且不太可能经常变化时使用枚举。像“洲的列表”或“部门的列表”这样的东西是使用枚举的好例子。
🌐 In general you should only use Enums when the list of values is small, fixed, and unlikely to change often. Things like "a list of continents" or "a list of departments" are good candidates for Enums.
在表格中使用枚举 #
🌐 Using enums in tables
要在表格中使用 Enum,你可以定义一个 Enum 类型的列。例如:
🌐 To use the Enum in a table, you can define a column with the Enum type. For example:
1create table person (2 id serial primary key,3 name text,4 current_mood mood5);这里,current_mood 列只能有来自“mood”枚举的值。
🌐 Here, the current_mood column can only have values from the "mood" Enum.
使用枚举插入数据 #
🌐 Inserting data with enums
你可以通过指定枚举值之一来向有枚举列的表插入数据:
🌐 You can insert data into a table with Enum columns by specifying one of the Enum values:
1insert into person2 (name, current_mood)3values4 ('Alice', 'happy');使用枚举查询数据 #
🌐 Querying data with enums
查询数据时,你可以像平常一样过滤和比较枚举值:
🌐 When querying data, you can filter and compare Enum values as usual:
1select * 2from person 3where current_mood = 'sad';管理枚举 #
🌐 Managing enums
你可以使用 alter type 语句来管理你的枚举。这里有一些例子:
🌐 You can manage your Enums using the alter type statement. Here are some examples:
更新枚举值 #
🌐 Updating enum values
你可以更新枚举列的值:
🌐 You can update the value of an Enum column:
1update person2set current_mood = 'excited'3where name = 'Alice';添加枚举值 #
🌐 Adding enum values
要向现有的 Postgres 枚举添加新值,你可以使用 ALTER TYPE 语句。方法如下:
🌐 To add new values to an existing Postgres Enum, you can use the ALTER TYPE statement. Here's how you can do it:
假设你有一个现有的枚举叫 mood,你想要添加一个新值 content:
🌐 Say you have an existing enum called mood, and you want to add a new value, content:
1alter type mood add value 'content';移除枚举值 #
🌐 Removing enum values
虽然这是可能的,但在枚举值创建后将其移除是不安全的。最好保留枚举值不动。
🌐 Even though it is possible, it is unsafe to remove enum values once they have been created. It's better to leave the enum value in place.
阅读 Postgres 邮件列表 了解更多信息:
🌐 Read the Postgres mailing list for more information:
Postgres里没有ALTER TYPE DELETE VALUE。即使你删除表中每一个枚举值的出现(并清理那些行),目标值仍可能存在于上层索引页中。如果你删除pg_enum条目,你会破坏索引。
🌐 There is no ALTER TYPE DELETE VALUE in Postgres. Even if you delete every occurrence of an Enum value within a table (and vacuumed away those rows), the target value could still exist in upper index pages. If you delete the pg_enum entry you'll break the index.
获取枚举值列表 #
🌐 Getting a list of enum values
通过查询 enum_range 函数来检查你现有的枚举值:
🌐 Check your existing Enum values by querying the enum_range function:
1select enum_range(null::mood);资源 #
🌐 Resources
- 官方 Postgres 文档:枚举类型