一对一关系的数据库设计 [英] Database Design for One to One relationships

查看:241
本文介绍了一对一关系的数据库设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成我的项目的数据模型的设计,我很难弄清楚要用哪种方法。

I'm trying to finalize my design of the data model for my project, and am having difficulty figuring out which way to go with it.

我有一个用户表,以及适用于该用户的未确定数量的属性。属性几乎在所有情况下都是可选的,因此允许使用空值。这些属性中的每一个对于用户是一对一的。我应该把它们放在同一个表上,并在添加属性时添加列(使用户表相当宽),或者我应该将每个属性放在一个单独的表上,用户表的外键。

I have a table of users, and an undetermined number of attributes that apply to that user. The attributes are in almost every case optional, so null values are allowed. Each of these attributes are one to one for the user. Should I put them on the same table, and keep adding columns when attributes are added (making the user table quite wide), or should I put each attribute on a separate table with a foreign key to the user table.

我已决定不使用EAV模式。

I have decided against using the EAV model.

谢谢!

编辑

属性包括婚姻状况,性别,年龄,姓氏,职业等。 p>

Properties include thing like marital status, gender, age, first and last name, occupation, etc. All are optional.

推荐答案

您能提供一些例子来说明您想要添加到用户表中的属性类型吗?

Could you give some examples of what kind of properties you'd want to add to the user table? As long as you stay below roughly 50 columns, it shouldn't be a big deal.

然而,一种方法是分割数据:

How ever, one way would be to split the data:

用户名,hashed_pa​​ssword,last_login,last_ip,current_ip等的一个表(用户),display_name,birth_day等的另一个表(配置文件)。

One table (users) for username, hashed_password, last_login, last_ip, current_ip etc, another table (profiles) for display_name, birth_day etc.

您可以通过相同的id属性将其链接,也可以向其他表格添加user_id列。

You'd link them either via the same id property or you'd add an user_id column to the other tables.

这篇关于一对一关系的数据库设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆