简体   繁体   English

SQL表设计

[英]SQL Table Design

I have a table called Project. 我有一个名为Project的表。 For simplicity, lets assume it has one column called projectId. 为了简单起见,假设它有一列称为projectId。

Projects
---------
projectId    PK

I have another table. 我还有一张桌子。 It is called SubProjects. 它称为子项目。 It has two columns, one is called projectId, and the other is called subProjectId. 它有两列,一列称为projectId,另一列称为subProjectId。 Both columns make up the primary key. 两列都构成主键。

Sub Projects
--------
projectId       PK
subProjectId    PK

They way this works is you have a bunch of projects listed in the projects table. 他们的工作方式是,您在项目表中列出了一堆项目。 Some of these projects can be sub projects of projects. 其中一些项目可以是项目的子项目。 For example, if the projects table had 3 rows like so 例如,如果项目表具有3行,例如

Projects
-------
projectId
    1
    2
    3

Projects 2 and 3 could be considered sub projects of project 1.This is where the SubProject table comes in. To represent this, Subprojects would look like this 项目2和3可以被视为项目1的子项目。这是SubProject表的出现位置。为了表示这一点,子项目看起来像这样

Sub Projects
--------
1 ,2
1 ,3

Meaning that projects 2 and projects 3 are sub projects of project 1. 意味着项目2和项目3是项目1的子项目。

I am having issues creating a model for this. 我在为此创建模型时遇到了问题。 In the SubProjects table, I need both projectId and subProjectId to reference a projectId in the Projects table. 在SubProjects表中,我需要projectId和subProjectId都引用Projects表中的projectId。 But, the key thing is that they could be referencing different projects. 但是,关键是他们可能会引用不同的项目。 I supose I should do this using foregin keys? 我认为我应该使用foregin键执行此操作吗? Does this make sense? 这有意义吗? Is this allowed? 可以吗? Is there a better way to do this? 有一个更好的方法吗?

I am using MySql and MySql workbench, but cant seem to pull this off. 我正在使用MySql和MySql工作台,但似乎无法实现这一目标。 Any ideas? 有任何想法吗?

EDIT A project can be a sub project of multiple tables. 编辑一个项目可以是多个表的子项目。

Doing it like that should be fine. 那样做应该没问题。 Can you be more specific about why you can't set it up? 您能否更具体地说明为什么无法设置它?

If a project is only a sub-project of one project, another way to do it would be to add a "parent project" column to the projects table, with a FK pointing to "itself". 如果一个项目只是一个项目的子项目,那么另一种方法是在项目表中添加一个“父项目”列,其中FK指向“自身”。 I think your queries most likely will end up being simpler if you do it that way. 我认为,如果这样做,您的查询很可能最终会变得更简单。

I would create one table with hierarchy using columns, such as: 我将使用列创建具有层次结构的表,例如:

project_id    parent_project_id
-------------------------------
1             null
2             1
3             1

Then you can just say if a project's parent_project_id is null, it is not a sub-project of any other project. 然后,您可以说一个项目的parent_project_id为null,则它不是任何其他项目的子项目。 Otherwise, the project is a sub-project of the project listed in parent_project_id . 否则,该项目是parent_project_id列出的项目的子项目。

Why don't you add a new field in table Projects, named as ParentProjectID. 为什么不在表Projects中添加一个名为ParentProjectID的新字段。 For projects, it will be null, and for subprojects, it will have its parent projectID there. 对于项目,它将为null,对于子项目,它将具有其父项目ID。

How you considered creating one table with a hierarchy? 您如何考虑使用层次结构创建一个表? Using your example it would be something like 用你的例子,就像

Projects
----------

ProjectID | ParentID
----------|----------
   1      |   NULL
   2      |   1
   3      |   1

You can add foreign key constraints to both columns in "Sub Projects" referring back to the ProjectId column in the "Projects" table. 您可以将外键约束添加到“子项目”中的两个列中,并返回到“项目”表中的ProjectId列。 Doing so will not force both columns to have the same value on the row in "Sub Projects". 这样做不会强制两列在“子项目”中的行上具有相同的值。 When you insert a new row to "Sub Projects" (lets say 2,3), the server will check: 当您在“子项目”中插入新行时(例如说2,3),服务器将检查:

  • That there is a row in the Projects table which has a ProjectId value 2 在Projects表中有一行,其ProjectId值为2
  • That there is a row in the Projects table which has a ProjectId value 3 在项目表中有一行,其ProjectId值为3
  • The there is not already an identical row in "Sub Projects" 在“子项目”中已经没有相同的行

This kind of model will work but it will not guarantee that the project hierarchy is "sane". 这种模型可以使用,但不能保证项目层次结构是“合理的”。 There is nothing in the database itself to stop you from inserting both (2,3) and (3,2) into the "Sub Projects" table. 数据库本身没有阻止您将(2,3)和(3,2)都插入“子项目”表的功能。 Or (1,1) for that matter. 或(1,1)。 So you will have to enforce that in your application logic instead. 因此,您将不得不在应用程序逻辑中强制执行该操作。

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

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