Mysql 是否建议将(图像)表格与(用户)、(产品)、(公司)和任何独立表格关联?

Mysql 是否建议将(图像)表格与(用户)、(产品)、(公司)和任何独立表格关联?,mysql,database,database-design,relational-database,database-schema,Mysql,Database,Database Design,Relational Database,Database Schema,我正在考虑使用一个图像表来存储任何其他独立表的图像,如用户,产品等等。。。(当然,独立表的任何单个实例(例如作为用户的John Smith,以及作为产品的laptop)可能有0个、1个或多个图像 图像表具有id、标题和文件名 我在想一个imagetable表,它将图像与它们相应的图像所有者s关联起来,就像用户一样,有以下字段:图像id,表id和表 某些条目可能如下所示: image_id | table_id | table ----------------------------- 1

我正在考虑使用一个
图像
表来存储任何其他独立表的图像,如
用户
产品
等等。。。(当然,独立表的任何单个实例(例如作为
用户的
John Smith
,以及作为
产品的
laptop
)可能有0个、1个或多个
图像

图像
表具有
id
标题
文件名

我在想一个
imagetable
表,它将
图像
与它们相应的
图像所有者
s关联起来,就像
用户
一样,有以下字段:
图像id
表id

某些条目可能如下所示:

image_id | table_id | table
-----------------------------
1        | 1        | user
2        | 1        | user

3        | 2        | user
4        | 2        | user

5        | 1        | product
6        | 1        | product
7        | 1        | product

8        | 2        | product

9        | 3        | product
10       | 3        | product

11       | 4        | product
现在的问题是:

是否建议使用此数据库设计?处理此请求的最佳方法是什么?


当然,另一种方法是使用
user\u image
product\u image
company\u image
表,而不是单个
image\u表。

否,因为这样您就失去了外键的优势

使用连接表:

create table product (
  product_id bigserial primary key,
  name citext not null unique
);

create table user (
  user_id bigserial primary key,
  name citext not null unique
);

-- personally, I would store the file in the db and use incremental backups
-- pedantically, I prefer "picture" over "image" as "image" has 2 meanings in computers
create table picture (
  picture_id bigserial primary key,
  filename citext not null,
  ...
);

create table product_picture (
  product_id bigint references product(product_id),
  picture_id bigint references picture(picture_id),
  primary key (product_id, picture_id)
);

create table user_picture (
  user_id bigint references user(user_id),
  picture_id bigint references picture(picture_id),
  primary key (user_id, picture_id)
);

谢谢。但是关于第二句话,根据要求,我们没有多对多的关系。这是一个简单的一对多关系。这样我认为我不应该使用多对多的设计和连接表。你对此有何感想?重要的是我要知道你对我公司的想法(当然你看起来更有经验)。作为第二个问题,这看起来更麻烦。用户、公司和产品的三个表意味着三套
控制器
s、
模型
s,(但不是
视图
s,因为图像将加载到其父实体的详细视图中).如果另一个实体需要像prev 3那样的图像怎么办?从另一个方面来说,我应该说我直到现在才使用外键(而且没有问题).一般来说,使用外键是一种耻辱,不是吗?更新了我的答案。您将必须更新您的模型,但不必添加更多控制器。非常感谢。而且,您是对的。使用此架构,甚至不应添加控制器。