title: Create a trigger in MYSQL using FK
body: <p>I seek to insert the name of the team, avoiding the repetition of the clear record.
  Later with your ID register the match with its markers.</p>
  <p>Ex:</p>
  <p><strong>My input data</strong></p>
  <pre><code>1, 'Team A', 'Team W', 0, 1, '12/02/2021'
  2, 'Team B', 'Team x', 2, 1, '12/10/2021'
  ...
  n, 'Team ??', 'Team ??', 3, 1, '14/12/2021'
  </code></pre>
  <p>Table Team</p>
  <pre><code>Team(1, 'Team A')
  Tema(2, 'Team B')
  Tema(3, 'Team W')
  Tema(4, 'Team X')
  </code></pre>
  <p>Table Match</p>
  <pre><code>Matches(1, 1, 2, 0, 1, '12/02/2021')
  Matches(2, 3, 2, 2, 1, '12/10/2021')
  Matches(3, 4, 3, 3, 1, '14/12/2021')
  </code></pre>
  <p>But first, look for 'Team A' in the 'Teams' table to obtain ID 10, if there is no such team, register. The same would happen with 'Team W'.
  Or you can enter the teams and then the matches table records.</p>
  <p>My problem is that I can't remember how to do this because all the examples I see are with fields that this table has, but in this case they are FK not the data itself.</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TRIGGER `searchFkTeam` Befor Insert ON `matches` FOR EACH ROW
  begin
   SET xteam = 'Team W'
   INSERT INTO team (name) VALUES (xteam)
   WHERE NOT EXISTS (
      SELECT id FROM teams WHERE name=xteam
   );
  end;
  DELIMITER ;
  </code></pre>
  <p>The declaration of the variable does not know how to do it because said field <code>new.team</code> there are no matches in the table, there is its FK</p>
  <p>Table Team</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>name</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Team 1</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Team 2</td>
  </tr>
  <tr>
  <td>3</td>
  <td>Team 3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Table Macthes (<strong>Expected result</strong>)</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>idP</th>
  <th>Team_A</th>
  <th>Team_B</th>
  <th>Goal_A</th>
  <th>Goal_B</th>
  <th>Date</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>3</td>
  <td>2</td>
  <td>0</td>
  <td>11/10/2021</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2</td>
  <td>1</td>
  <td>3</td>
  <td>1</td>
  <td>12/10/2021</td>
  </tr>
  <tr>
  <td>3</td>
  <td>3</td>
  <td>2</td>
  <td>1</td>
  <td>2</td>
  <td>13/10/2021</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>P.S .: I hope I have explained myself well and thanks for any help</p>