简体   繁体   English

如何将Junction表链接到主表

[英]How can I link my Junction table to my main table

I have a SQL database with the main table called Results. 我有一个SQL数据库,其主表名为Results。 This table stores a record of results of tests that are run nightly. 该表存储每晚运行的测试结果的记录。

The Results table has many fields but for arguments say lets just say for now it looks like this: “结果”表具有许多字段,但是对于参数来说,让我们暂时说它看起来像这样:

  • ResultID (Unique key field generated upon insert) ResultID(插入时生成的唯一键字段)
  • Result (nvchar10) 结果(nvchar10)

What I wanted to be able to record was a list of tags used in the tests that were run. 我希望能够记录的是在运行的测试中使用的标签列表。 The tags may be different for each result and an array of them are stored. 每个结果的标签可能不同,并且存储了它们的数组。

I created a junction table as shown below called Tags: 我创建了一个联结表,如下所示,称为“标签”:

  • TagID (int key field unique generated at runtime) TagID(在运行时生成的唯一键字段)
  • ResultID (int) ResultID(整数)
  • ScenarioTag (nvchar128) ScenarioTag(nvchar128)
  • FeatureTag (nvchar128) FeatureTag(nvchar128)

So what im looking to do is to link these 2 together. 所以我想做的就是将这两个链接在一起。 I'm not so great with databases ill be honest. 老实说,我对数据库不是很好。

I was thinking that when I save the test results with my normal SQL query immediately after I would loop through each tag and save the tags to this new table but maybe i'm wrong here? 我当时在想,当我遍历每个标签并将标签保存到新表中后,立即用普通的SQL查询保存测试结果时,也许我错了吗?

Psuedocode: 伪代码:

//Returned from previous SQL statement that inserted results values into the DB
int ResultID = SQLQueryReturnValue;

Foreach TAG in TAGS
{
    string SQLQuery = "INSERT INTO TAGS (ResultID, ScenarioTag, FeatureTag)(@ResultID, @ScenarioTag, @FeatureTag)";
    CmdSql.Parameters.AddWithValue("@ResultID", ResultID);
    CmdSql.Parameters.AddWithValue("@ScenarioTag", TAG.Scenario);
    CmdSql.Parameters.AddWithValue("@FeatureTag", TAG.Feature);
    CmdSql.CommandText = SQLQuery;
    CmdSql.Execute();
}

Heres an example of what each table might actually look like: 以下是每个表格的实际示例:

Results Table
|ResultID | Result |
| 10032   | Pass   |
| 10031   | Fail   |
| 10030   | Fail   |

Tags Table
| TagID | ResultID | ScenarioTag   | FeatureTag |
| 6     | 10032    | Cheque        | Trading    |
| 5     | 10032    | GBP           | Sales      |
| 4     | 10031    | Direct Credit | Trading    |
| 3     | 10031    | GBP           | Purchase   |
| 2     | 10030    | Wire          | Dividends  |
| 1     | 10030    | USD           | Payments   |

So finally onto my question...Is there a way that I can physically link this new "Tags" table to my results table. 所以最后我的问题...是一种可以将新的“标签”表物理链接到结果表的方法。 Its informally linked in a way using the ResultID but theres no physical link. 它使用ResultID的形式进行非正式链接,但没有物理链接。

Is it this you're looking for? 您要寻找的吗? (Assumption: This query is looking from results. They do not necessarily have to have Tags...) (假设:此查询从结果中查找。它们不一定必须具有标签...)

SELECT *
FROM Results
LEFT JOIN Tags ON Results.ResultID=Tags.ResultID

EDIT: Maybe I did not understand, what you mean by "physically". 编辑:也许我听不懂,“物理地”是什么意思。 You could add a foreign key constraint: 您可以添加外键约束:

ALTER TABLE Tags ADD CONSTRAINT FK_Tags_Results FOREIGN KEY (ResultID) REFERENCES Results(ResultID);  

This constraint adds a relation to these tables, making sure, that only values existing in Results are allowed in Tags as "ResultID". 此约束向这些表添加了一个关系,请确保“标记”中仅允许结果中存在的值作为“ ResultID”。 On the other hand you cannot delete a Result row with existing children in Tags... 另一方面,您不能删除“标签”中现有子项的“结果”行。

If you do this you could alter the top query to: 如果执行此操作,则可以将顶部查询更改为:

SELECT *
FROM Tags
INNER JOIN Results ON Results.ResultID=Tags.ResultID

Now you are looking from Tags (leading table) and you know, that each tag must have a ResultID (INNER JOIN). 现在,您正在从“标记”(前导表)中查找,并且您知道每个标记都必须具有一个ResultID(“内部联接”)。

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

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