[英]Is there a multivalued field type available in PostgreSQL?
I want to know if it is possible to store multiple values in a field in PostgreSQL.我想知道是否可以在 PostgreSQL 中的一个字段中存储多个值。
I have a table called Token
with the columns id
, text
and category
.我有一个名为Token
的表,其中包含列id
、 text
和category
。 category
is a multivalued field. category
是一个多值字段。 Is it necessary to create a separate table for it or is there a way to store it in the Token
table?是不是需要单独为它创建一个表,或者有没有办法将它存储在Token
表中?
There are arrays in PostgreSQL. PostgreSQL中有数组 。 For example: 例如:
CREATE TABLE "token" (
"id" integer PRIMARY KEY,
"text" text,
"category" text[]
);
Now you can insert multiple categories for each row into token
: 现在,您可以将每行的多个类别插入到token
:
INSERT INTO "token" ("id", "text", "category")
VALUES (1, 'some text', ARRAY['cate1', 'cate2']);
You can find the rows like: 您可以找到以下行:
SELECT * FROM "token" WHERE 'cate1' = ANY ("category");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.