简体   繁体   English

SQL 多个表的外键

[英]SQL foreign key to multiple tables

I am creating a health and safety app for 30 different forms;我正在为 30 个不同的 forms 创建一个健康和安全应用程序; for every question in each document, I need to allow users to add multiple images and enable them to add various actions.对于每个文档中的每个问题,我都需要允许用户添加多个图像并允许他们添加各种操作。

Furthermore, each form needs to be linked to a project.此外,每个表单都需要链接到一个项目。

So far, I have created a Project table, a Site_Weekly_Inspection table (one of the forms) with a foreign key to the project, an Action table with a foreign key site_weekly_inspection and an image table with a foreign key to site_weekly_inspection.到目前为止,我已经创建了一个 Project 表、一个带有项目外键的 Site_Weekly_Inspection 表(其中一种形式)、一个带有外键 site_weekly_inspection 的 Action 表和一个带有外键 site_weekly_inspection 的图像表。

Additionally, in the action and image table, I used action_category and image_category to be able to link the images and actions to the individual questions.此外,在动作和图像表中,我使用了 action_category 和 image_category 来将图像和动作链接到各个问题。

My database so far到目前为止我的数据库

I don't want to create a separate image and action table for each form.我不想为每个表单创建单独的图像和操作表。 What would be the MOST CORRECT way of making this database?制作这个数据库的最正确方法是什么?

Thanks in advance for any suggestions.在此先感谢您的任何建议。

You will need table(s) in the middle.您将需要中间的桌子。

  1. Inspection_Action table - lists all possible actions Inspection_Action_id Inspection_Action 表 - 列出所有可能的操作 Inspection_Action_id

  2. Inspection_Image table (or just Image )- lists all images Inspection_Image_id (PK) Inspection_Image 表(或只是 Image )- 列出所有图像 Inspection_Image_id (PK)

  3. Site_Weekly_Inspection_Action Site_Weekly_Inspection_Action_id Inspection_Action_id (FK points to Inspection_Action) Site_Weekly_Inspection_Action Site_Weekly_Inspection_Action_id Inspection_Action_id(FK 指向 Inspection_Action)

  4. If only one image per site / weekly add to Site_Weekly_Inspection table Inspection_Image_id (FK points to Inspection_Image)如果每个站点/每周只有一个图像添加到 Site_Weekly_Inspection 表 Inspection_Image_id(FK 指向 Inspection_Image)

  5. If more than one image per site /weekly add table Site_Weekly_Inspection_Images Site_Weekly_Inspection_Image_id (PK) Inspection_Image_id (FK points to Inspection_Image)如果每个站点/每周超过一个图像添加表 Site_Weekly_Inspection_Images Site_Weekly_Inspection_Image_id (PK) Inspection_Image_id (FK 指向 Inspection_Image)

You will need to figure out how to share images and actions in UI您需要弄清楚如何在 UI 中共享图像和操作

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

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