简体   繁体   English

多对多链接表的替代方法

[英]Alternatives for a many-to-many link table

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. 我已经看到多对多链接表回答了类似的问题,但是我想不出该如何解决这个问题。

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. 如果您不清楚,请告诉我,我将为您创建图表并上传。

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 这样桌子就不在乎它是哪种硬件

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

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