Sql – Designing an SQL Table and getting it right the first time

database-designsql

I currently working on an issue tracker for my company to help them keep track of problems that arise with the network. I am using C# and SQL.

Each issue has about twenty things we need to keep track of(status, work loss, who created it, who's working on it, etc). I need to attach a list of teams affected by the issue to each entry in my main issue table. The list of teams affected ideally contains some sort of link to a unique table instance, just for that issue, that shows the list of teams affected and what percentage of each teams labs are affected.

So my question is what is the best way to impliment this "link" between an entry into the issue table and a unique table for that issue? Or am I thinking about this problem wrong.

Best Solution

What you are describing is called a "many-to-many" relationship. A team can be affected by many issues, and likewise an issue can affect many teams.

In SQL database design, this sort of relationship requires a third table, one that contains a reference to each of the other two tables. For example:

CREATE TABLE teams (
  team_id INTEGER PRIMARY KEY
  -- other attributes
);

CREATE TABLE issues (
  issue_id INTEGER PRIMARY KEY
  -- other attributes
);

CREATE TABLE team_issue (
  issue_id INTEGER NOT NULL,
  team_id INTEGER NOT NULL,
  FOREIGN KEY (issue_id) REFERENCES issues(issue_id),
  FOREIGN KEY (team_id) REFERENCES teams(team_id),
  PRIMARY KEY (issue_id, team_id)
);