多对多链接表的替代方案

时间:2022-05-20 00:13:25

I am working on a project where I want to allow the end user to basically add an unlimited amount of resources when creating a hardware device listing.

我正在开发一个项目,我想让最终用户在创建硬件设备列表时基本上添加无限量的资源。

In this scenario, they can store both the quantity and types of hard-drives. The hard-drive types are already stored in a MySQL Database table with all of the potential options, so they have the options to set quantity, choose the drive type (from dropdown box), and add more entries as needed.

在这种情况下,他们可以存储硬盘的数量和类型。硬盘驱动器类型已存储在具有所有潜在选项的MySQL数据库表中,因此它们可以选择设置数量,选择驱动器类型(从下拉框中),并根据需要添加更多条目。

As I don't want to create a DB with "drive1amount", "drive1typeid", "drive2amount", "drive2typeid", and so on, what would be the best way to do this?

因为我不想创建一个带有“drive1amount”,“drive1typeid”,“drive2amount”,“drive2typeid”等的数据库,所以最好的方法是什么?

I've seen similar questions answered with a many-to-many link table, but can't think of how I could pull this off with that.

我已经看到类似的问题用多对多链接表回答,但是想不出我怎么能用它来解决这个问题。

2 个解决方案

#1


0  

Something like this?

像这样的东西?

CREATE TABLE `hardware` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(256) NOT NULL,
 `quantity` int(11) NOT NULL,
 `hardware_type_id` int(11) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `type_id` (`hardware_type_id`),
 CONSTRAINT `hardware_ibfk_1` FOREIGN KEY (`hardware_type_id`) REFERENCES `hardware_type` (`id`)
) ENGINE=InnoDB 

hardware_type_id is a foreign key to your existing table

hardware_type_id是现有表的外键

This way the table doesnt care what kind of hardware it is

这样表格并不关心它是什么类型的硬件

#2


0  

Your answer relies a bit on your long term goals with this project. If you want to posses a data repository which has profiles all different types of hardware devices with their specifications i suggest you maintain a hardware table of each different types of hardware. For example you will have a harddisk table which consist of all different models and types of hardisks out there. Then you can assign a record from this specific table to the host configuration table. You can build the dataset as you go from the input from user.

您的答案依赖于您在此项目中的长期目标。如果您想拥有一个数据存储库,其中包含所有不同类型的硬件设备及其规格的配置文件,我建议您维护每种不同类型硬件的硬件表。例如,您将拥有一个硬盘表,其中包含所有不同型号和类型的硬盘。然后,您可以将此特定表中的记录分配给主机配置表。您可以根据用户的输入构建数据集。

If this is not clear to you let me know i will create a diagram and upload for you.

如果您不清楚这一点,请告诉我,我将为您创建一个图表并上传。

#1


0  

Something like this?

像这样的东西?

CREATE TABLE `hardware` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(256) NOT NULL,
 `quantity` int(11) NOT NULL,
 `hardware_type_id` int(11) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `type_id` (`hardware_type_id`),
 CONSTRAINT `hardware_ibfk_1` FOREIGN KEY (`hardware_type_id`) REFERENCES `hardware_type` (`id`)
) ENGINE=InnoDB 

hardware_type_id is a foreign key to your existing table

hardware_type_id是现有表的外键

This way the table doesnt care what kind of hardware it is

这样表格并不关心它是什么类型的硬件

#2


0  

Your answer relies a bit on your long term goals with this project. If you want to posses a data repository which has profiles all different types of hardware devices with their specifications i suggest you maintain a hardware table of each different types of hardware. For example you will have a harddisk table which consist of all different models and types of hardisks out there. Then you can assign a record from this specific table to the host configuration table. You can build the dataset as you go from the input from user.

您的答案依赖于您在此项目中的长期目标。如果您想拥有一个数据存储库,其中包含所有不同类型的硬件设备及其规格的配置文件,我建议您维护每种不同类型硬件的硬件表。例如,您将拥有一个硬盘表,其中包含所有不同型号和类型的硬盘。然后,您可以将此特定表中的记录分配给主机配置表。您可以根据用户的输入构建数据集。

If this is not clear to you let me know i will create a diagram and upload for you.

如果您不清楚这一点,请告诉我,我将为您创建一个图表并上传。