为用户功能创建单独的表

时间:2015-09-14 13:18:44

标签: mysql database database-design

我正在开展一个我有普通用户(买家)和卖家的项目。 该项目必须是未来的证据,以处理数百万条记录。 我想知道将这个存储在数据库中的最佳方法是什么,因为卖家有一些普通用户没有的字段。 例如银行号码,公司名称,关于我,为什么是我等等。

目前我有一个用户表和一个user_profile数据透视表。

您认为为卖家创建一个单独的表是一个更好的想法,结合Seller_profile数据透视表或将卖家字段添加到user_profile表并检查用户的角色?

明智的选择是什么?

2 个答案:

答案 0 :(得分:0)

任何数据库设计都应该首先问问自己:实体是什么?

对我而言,买家和卖家似乎不同。他们都有其他属性。卖家有一系列要出售的物品。买方应该下订单,也许可以组合多个卖家的商品。因此,他们以另一种方式行事。

我认为他们应该有单独的表格。

答案 1 :(得分:0)

Basically you have two entities Buyer and Seller.Lets see each case:
Separate Tables with separate attributes in each :

Table Structure :
Buyer(id,name,address,b1,b2,b3);
Seller(id,name,address,s1,s2,s3,s4 );
(Note b and s are column names);

Buyer and Seller can login to your system. If you put both in separate table you need to check in both tables. Similarly for signup ,you need to check for existence of user in both tables. So here you are hitting database twice. Imagine if you have billions of records.This may slower down performance.

3 Separate Tables:
Table Structure:
User(id,username,password,name,address)
Buyer(id,b1,b2,b3,fk_to_user);
Seller(id,s1,s2,s3,s4 ,fk_to_user);
(Note b and s are column names);

We have just taken out common field into User table. Fields specific to Buyer and Seller are shifted to Buyer and Seller table respectively. This design helps to solve two problems:
1.If there are additional attributes are added to Buyer or Seller table , it will not affect overall User table or overall design.
2.This is optimized as it hits database once while login to system.


[Note: I have taken case of login as example.Once user logs in to system you will have its details like "id" you can fetch any records corresponding to that user. ]