dougaxing8673 2013-06-29 14:19
浏览 28
已采纳

多对多链接表的替代方案

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.

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?

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条回答 默认 最新

  • douhunbei0166 2013-06-29 15:02
    关注

    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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据