SQL一对多关系,但多对一表关系

时间:2021-05-07 09:59:06

I have 5 tables that can contain a phone field, but I want to have the option for multiple phone numbers for each table.

我有5个表可以包含一个电话字段,但我想为每个表选择多个电话号码。

Instead of creating 5 sub tables, it makes more sense to create one, and link it to the other 5 "parent" tables. But now I am thinking of how to link that table to the other 5.

创建一个子表更有意义,而不是创建5个子表,并将其链接到其他5个“父”表。但现在我在考虑如何将该表与其他表连接起来。

My first thought was to filter on 2 subjects. A field that stores the name of the linked table, and a generic foreign key that contains the key from one of the five tables. I'm still leaning this way.

我的第一个想法是过滤2个主题。存储链接表名称的字段,以及包含五个表之一的键的通用外键。我仍然倾向于这样。

Next is the idea of putting 5 foreign keys into the table, and allowing them to be null. I dislike both the idea of 4 null fields. Not to mention I have the same issue with another table that would link to 11 other tables in a similar manner.

接下来是将5个外键放入表中并允许它们为空的想法。我不喜欢4个空字段的想法。更不用说我有另一个表的问题,它会以类似的方式链接到其他11个表。

The third is a linking or junction table, again (I believe,) with 5 nullable foreign keys, but this doesn't seem to do anything more then option 2 with a little more complexity.

第三个是链接或联结表,再次(我相信),有5个可以为空的外键,但这似乎没有做任何更多的选项2,更复杂一点。

4th option, I'm using an auto-increment field for the primary key for most of my tables. I could switch to a UUID or GUID for these tables. I'm already set up to use UUID as primary for security reasons, but was using auto-increment for most tables for performance.

第四个选项,我使用自动增量字段作为我的大多数表的主键。我可以切换到这些表的UUID或GUID。出于安全原因,我已经将UUID设置为主要用途,但是为大多数表格使用自动增量来提高性能。

A 5th option is something I ran into where an auto-increment field was combined with a table name to create a new character field. (Only during creation of the table, so technically isn't a calculated field.) But again don't see the benefit of this beyond the first option.

我遇到了第五个选项,其中自动增量字段与表名组合以创建新的字符字段。 (仅在创建表时,因此技术上不是计算字段。)但是再次看不到超出第一个选项的好处。

Is one of these the best way to go, or is there another option I either missed, or do not understand?

这是最好的方式之一,还是有其他选择我要么错过了,要么不明白?

2 个解决方案

#1


0  

If you want to assign the same phone number to three records (form your 5 tables) do you want add three records with the same phone number to your "phone_numbers" table?

如果要将相同的电话号码分配给三个记录(从5个表格中分配),您是否要将具有相同电话号码的三个记录添加到“phone_numbers”表中?

It's a optimization issue, so there is no "good way" in SQL-semantics sense.

这是一个优化问题,因此在SQL语义学意义上没有“好方法”。

In my opinion the easiest to use in development is the first way: table_name and id in this table, but don't use varchar. Use enum or "table_name_id" TINYINT (ref to second table with names).

在我看来,最容易在开发中使用的是第一种方式:table_name和id在这个表中,但不要使用varchar。使用enum或“table_name_id”TINYINT(引用带名称的第二个表)。

#2


0  

Don't make it so complex while you can do this easily You can do this using single table. I am giving you an Example below :

当你可以轻松地做到这一点时,不要让它变得如此复杂你可以使用单个表来做到这一点。我给你一个例子如下:

UserId | PhoneNo | PhoneType | ParentId

UserId | PhoneNo | PhoneType |的ParentId

where UserId : is Id of user Phone no is raw phone/ mobile number PhoneType : phone/ mobile number Parent ID : is the main key point here by default it is 0 to treat it as a separate entry other wise same tabl's UserID to identify it as a child phone number..think it as a tree relation ship.. It's a simple and efficient way to do that as far as I know.

其中UserId:是用户的ID电话号码是原始电话/手机号码PhoneType:电话/手机号码父母ID:这里是主要的关键点,默认情况下它是0,将其视为一个单独的条目,其他相同的tabl的UserID用于识别它作为一个儿童电话号码......想象它是一个树关系船..就我所知,这是一种简单而有效的方法。

#1


0  

If you want to assign the same phone number to three records (form your 5 tables) do you want add three records with the same phone number to your "phone_numbers" table?

如果要将相同的电话号码分配给三个记录(从5个表格中分配),您是否要将具有相同电话号码的三个记录添加到“phone_numbers”表中?

It's a optimization issue, so there is no "good way" in SQL-semantics sense.

这是一个优化问题,因此在SQL语义学意义上没有“好方法”。

In my opinion the easiest to use in development is the first way: table_name and id in this table, but don't use varchar. Use enum or "table_name_id" TINYINT (ref to second table with names).

在我看来,最容易在开发中使用的是第一种方式:table_name和id在这个表中,但不要使用varchar。使用enum或“table_name_id”TINYINT(引用带名称的第二个表)。

#2


0  

Don't make it so complex while you can do this easily You can do this using single table. I am giving you an Example below :

当你可以轻松地做到这一点时,不要让它变得如此复杂你可以使用单个表来做到这一点。我给你一个例子如下:

UserId | PhoneNo | PhoneType | ParentId

UserId | PhoneNo | PhoneType |的ParentId

where UserId : is Id of user Phone no is raw phone/ mobile number PhoneType : phone/ mobile number Parent ID : is the main key point here by default it is 0 to treat it as a separate entry other wise same tabl's UserID to identify it as a child phone number..think it as a tree relation ship.. It's a simple and efficient way to do that as far as I know.

其中UserId:是用户的ID电话号码是原始电话/手机号码PhoneType:电话/手机号码父母ID:这里是主要的关键点,默认情况下它是0,将其视为一个单独的条目,其他相同的tabl的UserID用于识别它作为一个儿童电话号码......想象它是一个树关系船..就我所知,这是一种简单而有效的方法。