Postgresql 在postgres中存储多个URL?

Postgresql 在postgres中存储多个URL?,postgresql,Postgresql,我正在构建一个产品数据库,并想知道如何在我的产品行中存储产品图像URL(一些产品具有单个图像URL,其他产品具有多个图像URL) 为每个特定的图像URL创建单独的列是否有缺点?是否可以在一列中存储多个URL(作为一个数组?),如果可以,这是否有利?我将创建一个名为product_images的表,该表的结构将非常简单,并将与您的products表连接,如下所示: create table product_images ( id serial primary key, product_

我正在构建一个产品数据库,并想知道如何在我的产品行中存储产品图像URL(一些产品具有单个图像URL,其他产品具有多个图像URL)


为每个特定的图像URL创建单独的列是否有缺点?是否可以在一列中存储多个URL(作为一个数组?),如果可以,这是否有利?

我将创建一个名为product_images的表,该表的结构将非常简单,并将与您的products表连接,如下所示:

create table product_images (
   id serial primary key,
   product_id int not null references products(id),
   url text not null
);
这样,如果您选择在数据库中添加图像而不是URL,则可以相应地修改此结构,而不必影响products表

例如:

create table products (
  id serial primary key,
  name varchar(100)
);

create table product_images (
  id serial primary key,
   product_id int not null references products(id),
   url text not null
);

insert into products (name) values ('iPad'), ('Surface Pro');
insert into product_images (product_id, url) values 
('1', 'http://apple.com'),
('1', 'http://buy.apple.com'),
('2', 'http://microsoft.com'),
('2', 'http://msdn.microsoft.com');

我将创建一个名为product_images的表,其结构将非常简单,并将与您的products表连接,如下所示:

create table product_images (
   id serial primary key,
   product_id int not null references products(id),
   url text not null
);
这样,如果您选择在数据库中添加图像而不是URL,则可以相应地修改此结构,而不必影响products表

例如:

create table products (
  id serial primary key,
  name varchar(100)
);

create table product_images (
  id serial primary key,
   product_id int not null references products(id),
   url text not null
);

insert into products (name) values ('iPad'), ('Surface Pro');
insert into product_images (product_id, url) values 
('1', 'http://apple.com'),
('1', 'http://buy.apple.com'),
('2', 'http://microsoft.com'),
('2', 'http://msdn.microsoft.com');

我可能会选择只为图像的每个链接设置一个URL数组,类似这样的方式可以工作:

CREATE TABLE products
(
   id integer,
   image_url text[]
);

我可能会选择只为图像的每个链接设置一个URL数组,类似这样的方式可以工作:

CREATE TABLE products
(
   id integer,
   image_url text[]
);

谢谢@CraigKerstiens——我也很想知道,但在postgres关于数组的文档顶部,似乎表明域不受支持。我不确定这是否完全正确?谢谢@CraigKerstiens——我也很想知道,但在postgres关于数组的文档顶部,似乎表明域不受支持。我不确定这是否完全正确?谢谢@zedfoxus-在单独的product_image表中,您是否可以为每个单独的URL创建一列?您是否会尝试将它们存储为数组(请参见下面的答案)?我添加了一个如何存储它们的示例(每行一个url)。在获取产品时,我们如何获取所有这些图像?我试过了,一个产品有多行。@Tom,如果你的数据库每个产品只存储一个图像,那么你只会得到一个图像。如果您的数据库每个产品存储2+个图像,那么您必须决定是要哪一个图像,还是要所有图像。这取决于您的实现。谢谢@zedfoxus-在单独的product_image表中,您能为每个单独的URL创建一列吗?您是否会尝试将它们存储为数组(请参见下面的答案)?我添加了一个如何存储它们的示例(每行一个url)。在获取产品时,我们如何获取所有这些图像?我试过了,一个产品有多行。@Tom,如果你的数据库每个产品只存储一个图像,那么你只会得到一个图像。如果您的数据库每个产品存储2+个图像,那么您必须决定是要哪一个图像,还是要所有图像。这取决于您的实现。