How can I link my Junction table to my main table

Festivejelly

I have a SQL database with the main table called 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)
  • Result (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)
  • ResultID (int)
  • ScenarioTag (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?

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.

Shnugo

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". 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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I delete rows in the main table that match the ID in my temp table?

From Dev

How can I "treefy" my table?

From Dev

How can I "treefy" my table?

From Dev

How can I join table with criteria in grails, if i have not link fields in my domain class

From Dev

How can I force my table width into the width of my container?

From Dev

C++ how can I link my main class with my header file?

From Dev

How to link esimakin pagination with my table?

From Dev

How do I link the File Table with my existing table in SQL server

From Dev

How can I add an image to my table cell in swift?

From Dev

How can I avoid words wrapping in my table <td> cells?

From Dev

How can I update my SQL table in C#?

From Dev

How can I make a table inside my shiny box with RandomIcon()

From Dev

How can I get the className of my table in cakephp 3?

From Dev

How can I update my table after X minutes?

From Dev

React - how can I get updated data to show in my table?

From Dev

How can I add an object to my table with multiple values?

From Dev

How can I query my table to group it by 2 fields in mySQL?

From Dev

How best can I to pivot my redshift table?

From Dev

How can I update my table in C# with OOP?

From Dev

How can I find Offline Dealers in my table?

From Dev

How can I update my table after X minutes?

From Dev

How can I add the name of a contact to my table view cell?

From Dev

How can I display an amount of units in my table with the same value?

From Dev

How can I display different code if my posts table is empty?

From Dev

How can I understand my table is either transaction or lock?

From Dev

PHP - How can I print out my database table?

From Dev

How can I make my DB images be displayed in the table?

From Dev

How can I get rid of these extra entries in my lookup table?

From Dev

How can I update my Table Controller filtering with a Search Controller?

Related Related

  1. 1

    How can I delete rows in the main table that match the ID in my temp table?

  2. 2

    How can I "treefy" my table?

  3. 3

    How can I "treefy" my table?

  4. 4

    How can I join table with criteria in grails, if i have not link fields in my domain class

  5. 5

    How can I force my table width into the width of my container?

  6. 6

    C++ how can I link my main class with my header file?

  7. 7

    How to link esimakin pagination with my table?

  8. 8

    How do I link the File Table with my existing table in SQL server

  9. 9

    How can I add an image to my table cell in swift?

  10. 10

    How can I avoid words wrapping in my table <td> cells?

  11. 11

    How can I update my SQL table in C#?

  12. 12

    How can I make a table inside my shiny box with RandomIcon()

  13. 13

    How can I get the className of my table in cakephp 3?

  14. 14

    How can I update my table after X minutes?

  15. 15

    React - how can I get updated data to show in my table?

  16. 16

    How can I add an object to my table with multiple values?

  17. 17

    How can I query my table to group it by 2 fields in mySQL?

  18. 18

    How best can I to pivot my redshift table?

  19. 19

    How can I update my table in C# with OOP?

  20. 20

    How can I find Offline Dealers in my table?

  21. 21

    How can I update my table after X minutes?

  22. 22

    How can I add the name of a contact to my table view cell?

  23. 23

    How can I display an amount of units in my table with the same value?

  24. 24

    How can I display different code if my posts table is empty?

  25. 25

    How can I understand my table is either transaction or lock?

  26. 26

    PHP - How can I print out my database table?

  27. 27

    How can I make my DB images be displayed in the table?

  28. 28

    How can I get rid of these extra entries in my lookup table?

  29. 29

    How can I update my Table Controller filtering with a Search Controller?

HotTag

Archive