title: Multiple Column Foreign Key: Set single column to Null "ON DELETE" instead of all
body: <p><strong>General</strong>: Given a foreign key over several columns, some of them might be NULL.<br>
  By default (MATCH SIMPLE) MySQL/MariaDB InnoDB does not check the foreign key as long as at least one column of a multi column foreign key is NULL.</p>
  
  <p><strong>Requirement</strong>: If a row is deleted from the parent one column of the corresponding child should be set to NULL, but not both columns in the foreign key.</p>
  
  <p><strong>Example/Description</strong>: A student might be listed for a lecture, and optionally for one of the lectures groups as well. If the lecture is deleted all student listing should be removed (Works) and all its groups (Works). If only a single group is deleted, then the students should still be listed for the lecture, but they should not be assigned to a group any more (Problem).</p>
  
  <p><strong>Example/SQL</strong>: The following SQL illustrates this example, but the last statement will not work, as the last FOREIGN KEY requires both lectureId and groupId to be NULLable, but making both NULLable will imply that deleting a group will also set the lectureId to NULL.</p>
  
  <pre><code>CREATE TABLE lectures (
    lectureId INT NOT NULL,
    title VARCHAR(10) NOT NULL,
    PRIMARY KEY (lectureId)
   );
  
  CREATE TABLE groups (
    lectureId INT NOT NULL,
    groupNo INT NOT NULL,
    title VARCHAR(10) NOT NULL,
    PRIMARY KEY (lectureId,groupNo),
    FOREIGN KEY (lectureId) REFERENCES lectures (lectureId)
      ON UPDATE CASCADE ON DELETE CASCADE
   );
  
  CREATE TABLE studentListed (
    studentId INT NOT NULL,
    lectureId INT NOT NULL,
    groupNo INT NULL,
    PRIMARY KEY (studentId,lectureId),
    FOREIGN KEY (lectureId) REFERENCES lectures (lectureId)
      ON UPDATE CASCADE ON DELETE CASCADE,
    FOREIGN KEY (lectureId,groupNo) REFERENCES groups (lectureId,groupNo)
      ON UPDATE CASCADE ON DELETE SET NULL
   );
  </code></pre>