title: SQL code to check for Many-To-Many relationships
body: <p>I have been researching non-stop and have only managed to find out - how to check for a 1-To-Many relationship in SQL. I want to know if anyone can help me find out on how to check for a Many-To-Many relationship in SQL code.  The tables in the database are in correct normal form.</p>
  
  <p>The scenario I have is:</p>
  
  <ul>
  <li>A user will select three tables from the database.</li>
  <li>Once the three tables have been selected, I need to check if those tables have a Many-To-Many relationship.</li>
  <li>So I need to check if the three tables that the user has selected are associated with a primary key and the intersection table has the two reference keys that point to the two other tables.</li>
  </ul>
  
  <p><code></code></p>
  
  <pre><code>SELECT K_Table = FK.TABLE_NAME,
         FK_Column = CU.COLUMN_NAME,
         PK_Table = PK.TABLE_NAME,
         PK_Column = PT.COLUMN_NAME,
         Constraint_Name = C.CONSTRAINT_NAME
  FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
  INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK 
          ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
  INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK 
          ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
  INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU 
          ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
  INNER JOIN (SELECT i1.TABLE_NAME, 
                     i2.COLUMN_NAME
              FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
              INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 
                      ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
              WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY') PT 
          ON PT.TABLE_NAME = PK.TABLE_NAME
  WHERE PK.TABLE_NAME = '" + ActionM2M.Table2 + "'  AND 
        FK.TABLE_NAME = '" + ActionM2M.ITableNames1 + "' AND 
        exists (SELECT K_Table = FK.TABLE_NAME,
                       FK_Column = CU.COLUMN_NAME,
                       PK_Table = PK.TABLE_NAME,
                       PK_Column = PT.COLUMN_NAME,
                       Constraint_Name = C.CONSTRAINT_NAME
                FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
                INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK 
                        ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
                INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK 
                        ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
                INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU 
                        ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
                INNER JOIN (SELECT i1.TABLE_NAME, i2.COLUMN_NAME
                            FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
                            INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 
                                    ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
                            WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY') PT
                        ON PT.TABLE_NAME = PK.TABLE_NAME
                WHERE PK.TABLE_NAME = '" + ActionM2M.Table1 + "'  AND 
                      FK.TABLE_NAME = '" + ActionM2M.ITableNames1 + "')
  </code></pre>