title: How to configure a table to support cascading deletes when either of its related tables are deleted?
body: <p>I am designing a database for an online learning platform (with PostgreSQL). This is what the database currently looks like:</p>
  <pre><code>Users &lt;- Course_Memberships -&gt; Courses -&gt; Modules -&gt; Lectures
                  ^                                       ^
                  |________User_Watched_Lectures__________|
  </code></pre>
  <p><code>Course_Memberships</code> is a pivot table for the many-to-many relationship between <code>Users</code> and <code>Courses</code>, which keeps track of the courses a user is enrolled in.</p>
  <p>I want to keep track of the lectures that have been watched by users, which will be a table called <code>User_Watched_Lectures</code>.</p>
  <p>However - this is where I am getting stuck - I'm trying to make sure that when a student leaves a course, or the lecture is deleted/replaced by an admin, that the student's record of having viewed the lecture is deleted. This would happen with a cascading delete.</p>
  <p>How could I establish this relationship?</p>
  <p>If there is a better way to store this data, I am also open to suggestions to improve my design.</p>