在postgres中存储多个URL?

时间:2015-09-15 21:22:02

标签: postgresql

我正在建立一个产品数据库,并想知道如何在产品行中存储产品图片网址(某些产品有单个图片网址,其他产品有多个产品图片网址)。

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

2 个答案:

答案 0 :(得分:3)

我会创建一个名为product_images的表,其结构将很简单,并将与您的产品表连接起来:

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

这样,如果您选择在数据库中添加图像而不是URL,则可以相应地对此结构进行变形,而不必影响您的产品表。

示例:

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');

答案 1 :(得分:0)

我可能会选择为图像的每个链接添加一系列网址,这样的方法可行:

CREATE TABLE products
(
   id integer,
   image_url text[]
);
相关问题