title: Unique constraints across a many to many relationship table
body: <p><strong>Is there any way to add a constraint to ensure that an entry of X in one column can then only allow an entry of Y in another column?</strong></p>
  
  <p>Say I have two tables, stripped down to minimal columns, tbl_1 has a pk. tbl_2 has 2 columns - a pk and a text string.</p>
  
  <p>These tables are joined by a 3rd relationship table because they're many to many and it uses pk from tbl1 and tbl2. </p>
  
  <pre><code>t1_pk     t2_pk | t2_str     t1fk | t2fk
    x         1       AAA        x      1
    y         2       BBB        x      2
    z         3       AAA        y      3  
              4       BBB        y      4
                                 z      1
                                 z      2
  </code></pre>
  
  <p>All entries above are allowed, but now I'm trying to figure out how I can constrict the relationship table so that the string attached to t2_pk can only tie in to the t1_pk ONCE. E.g. in the 3rd table:</p>
  
  <pre><code> t1fk | t2fk
     x      3
  </code></pre>
  
  <p>would not be allowed because x-1 exists and both 1 and 3 have the string AAA attached. One way I can think without making 3 more tables and going round in circles, is to move the string to the relationship table and add a constraint so if the t2fk number exists in the table already it'll only allow the number again if accompanied by the same string.</p>
  
  <p><strong>is there a background process for this I can declare like adding a unique constraint, or would it simply need to be imposed by a stored procedure?</strong></p>