【发布时间】:2021-11-27 12:38:02
【问题描述】:
我一直在尝试使用 SQL 请求在 Postgresql 中获取类型为枚举数组 (ENUM[]) 的列的所有可能值。
使用表 information_schema.columns 中的字段 data_type 的常用方法不起作用,因为它只返回 ARRAY。
【问题讨论】:
标签: arrays postgresql enums
我一直在尝试使用 SQL 请求在 Postgresql 中获取类型为枚举数组 (ENUM[]) 的列的所有可能值。
使用表 information_schema.columns 中的字段 data_type 的常用方法不起作用,因为它只返回 ARRAY。
【问题讨论】:
标签: arrays postgresql enums
以下工作:
SELECT enumlabel
FROM pg_enum
INNER JOIN pg_type ON enumtypid = typelem
INNER JOIN information_schema.columns ON typname = udt_name
INNER JOIN pg_namespace ON pg_namespace.oid = typnamespace
WHERE table_name = 'TABLE_NAME'
AND column_name = 'COLUMN_NAME'
AND nspname = 'public'
);
【讨论】:
使用psql,您可以简单地使用\dT+:
\dT+ t_enum
List of data types
Schema │ Name │ Internal name │ Size │ Elements │ Owner │ Access privileges │ Description
═════════╪════════╪═══════════════╪══════╪══════════╪═════════╪═══════════════════╪═════════════
laurenz │ t_enum │ t_enum │ 4 │ one ↵│ laurenz │ │
│ │ │ │ two ↵│ │ │
│ │ │ │ three │ │ │
(1 row)
【讨论】: