Duplications of a relational table

二次信任 提交于 2019-12-13 06:59:59

问题


After this question, I think I will go for Entity-relationship modelling to solve the issue of my image table that serves many other tables.

But now I have another bigger problem as there are groups/ categories in each uploaded/ inserted image, page, user, etc.

The reason I have these category tables is to allow an admin to change the category name and add more categories in whenever is need.

I use category tables to categorise each of them, for instance,

categories for image,

category_id   category_name
1             Primary image
2             Secondary image
...

for users (I duplicate the category table off image and just give the table a different name),

category_id   category_name
1             Primary user
2             Secondary user
...

And carry on duplicating the table off each other!

They looks similar inside and very redundant when I need a new category table to be added. For instance, categories for a contact list table,

category_id   category_name
1             Primary contact
2             Secondary contact
3             School
4             government
...

How can I solve this redundancy? And how the database diagram would look like?


回答1:


The names of categories for pictures are generally different from the names of categories for users, and both those are different from the names of categories for pages. That suggests the values are drawn from different domains. Different domains mean different tables.

Create one table for each kind of category. Use foreign keys.

Redundant is a technical term in database design. It doesn't mean "these two tables look a lot alike". It means the tables have the same values, and those values have the same meaning.

It's clear that "Primary image" doesn't have the same value as "Primary user". But let's say, for the sake of argument, that both those tables had the row (1, Primary). That still not redundant, because those two values have different meanings. In the one case, it means that whatever image is tagged with it is a primary image. In the other case, it means that whatever user is tagged with it is a primary user. Images are not users. Different meanings.



来源:https://stackoverflow.com/questions/9433330/duplications-of-a-relational-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!