简体   繁体   English

MYSQL一对一关系

[英]MYSQL one-to-one relationship

I started transforming whole database to a normalised database. 我开始将整个数据库转换为规范化数据库。 But there is one thing which I dont understand about relationships: 但是我对关系一件事不了解:

Let's say I have 2 tables: 假设我有2张桌子:

Users
-userID (INT-PK)
-userName (varchar)
_favColor (int)

And: 和:

Colors
-colorID (INT-PK)
-colorName (varchar)

Now obviously I have to create a relationship, the question is: Should I make relationship between Colors Table and Users Table, or between Users Table and Colors Table? 现在显然我必须创建一个关系,问题是:我应该在颜色表和用户表之间,还是在用户表和颜色表之间建立关系?

What I've noticed is that when creating a relationship, the relationship does not appear in both tables, it appears in just one of them, and this makes me confused. 我注意到的是,在创建关系时,该关系未同时出现在两个表中,而是仅出现在其中一个表中,这使我感到困惑。

For this example I would recommend a 1:M relationship, going from colours to users. 对于此示例,我建议使用1:M的关系,从颜色到用户。

This is because the users table requires information from the colours table, the reason why you'd have a 1:M relationship is because different users may have the same favourite colour. 这是因为“用户”表需要颜色表中的信息,您之所以具有1:M关系是因为不同的用户可能具有相同的喜欢的颜色。

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

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