title: How can i delete data from database with foreign key constraint in mysql using php pdo
body: <p>I have three Tables in database. </p>
  
  <p>table 1 </p>
  
  <p><strong>projects</strong></p>
  
  <pre><code>PK ProjectID
  </code></pre>
  
  <p>table 2</p>
  
  <p><strong>students</strong></p>
  
  <pre><code>PK RegNo
  FK ProjectID
  </code></pre>
  
  <p>table 3</p>
  
  <p><strong>progress</strong></p>
  
  <pre><code>FK RegNo
  </code></pre>
  
  <p>Now the thing I want to perform a delete operation when I delete the record from a project it should be deleted from students, as students primary key is also present as foreign key progress table, so it should also delete RegNo from progress table. How I can achieve this as the best possible way. Thanks in advance.</p>
  
  <pre><code>$query = "DELETE students, progress from students inner join progress on progress.RegNo=students.RegNo where students.ProjectID='$id';DELETE FROM projects where projects.ProjectID='$id'";
  
  //$conn-&gt;exec($query);
  $stmt = $conn-&gt;prepare($query);
  $stmt-&gt;execute();
  </code></pre>
  
  <p>it gives foreign key constraint violation</p>