简体   繁体   English

关系数据库设计,一个元表可用于多个表?

[英]relational database design, one meta table for several tables?

i do not know much about database design, recently i got a project that promises to need a huge database and i'm confused about what is the best way of designing an standard database structure(mysql). 我对数据库设计了解不多,最近我得到一个承诺需要一个大型数据库的项目,而我对设计标准数据库结构(mysql)的最佳方法感到困惑。

for example let's say we have 3 tables: users,shops,products each of these(users,shops,products) may have one or several details, like social page links(for shops),phone numbers(for users), addresses(for shops and users), colors\\materials(for products),etc... 例如,假设我们有3个表格:用户,商店,产品(用户,商店,产品)中的每一个都可能具有一个或几个详细信息,例如社交页面链接(用于商店),电话号码(用于用户),地址(用于商店和用户),颜色\\材料(用于产品)等。

should i create only one meta table and store all of the details in it or should i create separate meta tables for each table?(user_meta,shop_meta,product_meta). 我应该只创建一个元表并在其中存储所有详细信息,还是应该为每个表创建单独的元表?(user_meta,shop_meta,product_meta)。 which one is a better approach? 哪个是更好的方法?

any advice and suggestions will be greatly appreciated. 任何意见和建议将不胜感激。

Edit 编辑

according to mr Branko Dimitrijevic every table should have it's own meta table. 根据Branko Dimitrijevic先生的说法,每个表都应该有自己的元表。

my problem is that user may have several types of details like: social pages url, user addresses,user role, etc... 我的问题是用户可能具有多种类型的详细信息,例如:社交页面网址,用户地址,用户角色等。

is it ok to design database like this: 这样设计数据库是否可以:

**Table: USERS**

userId | username | email | pass


**Table: USERS_META**

userId | metaType | metaValue

metaType will be the different types of details user can have like address,instagram page,facebook page,etc... metaType将是用户可以具有的详细信息的不同类型,例如地址,instagram页面,facebook页面等。

for example records in USERS_META table would be like: 例如,USERS_META表中的记录如下:

userId |   metaType   |       metaValue
---------------------------------------------------
2      |  address     | some address for user home
---------------------------------------------------
2      |  role        | 1(id of the role)
---------------------------------------------------
2      |  instagram   | instagram.com/userpage
---------------------------------------------------
3      |  address     | some address for user 3 home
---------------------------------------------------
3      |  facebook    | facebook.com/userpage
---------------------------------------------------
3      |  role        | 2(id of the role)
---------------------------------------------------
2      |  age         | 18
---------------------------------------------------
3      |  age         | 19

now if i want the address of user 2: 现在,如果我想要用户2的地址:

SELECT metaValue FROM USERS_META WHERE userId=2 AND metaType='adress'

and if i want users social pages link: 如果我希望用户社交页面链接:

$array=array('facebook','instagram','telegram','etc');
SELECT metaValue FROM USERS_META WHERE userId=2 AND metaType IN($array)

is this a good way or should i create separate tables for each meta(USER_ADDRESSES,USER_SOCIAL,USER_AGE,etc...)? 这是个好方法还是我应该为每个元数据创建单独的表(USER_ADDRESSES,USER_SOCIAL,USER_AGE等)?

In a nutshell: you shouldn't "merge" multiple tables. 简而言之:您不应该“合并”多个表。

Even if they happen to have same fields, they almost certainly don't have same constraints (eg foreign keys pointing to them from other tables). 即使它们碰巧具有相同的字段,也几乎可以肯定没有相同的约束 (例如,从其他表指向它们的外键)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM