mysql> desc courses;
+--------------------+---------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------------------+------+-----+---------+----------------+
| course_id | int(11) | NO | PRI | NULL | auto_increment |
| course_name | varchar(50) | NO | UNI | NULL | |
| course_description | text | NO | | NULL | |
| course_added | datetime | NO | | NULL | |
| status | enum('Active','Inactive') | NO | | Active | |
+--------------------+---------------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> desc papers;
+-------------------+---------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------------+------+-----+---------+----------------+
| paper_id | int(11) | NO | PRI | NULL | auto_increment |
| course_id | int(11) | NO | | NULL | |
| paper_name | varchar(50) | NO | | NULL | |
| paper_description | text | NO | | NULL | |
| paper_added | datetime | NO | | NULL | |
| status | enum('Active','Inactive') | NO | | Active | |
+-------------------+---------------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> desc question_sets;
+-----------------+---------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------------+------+-----+---------+----------------+
| set_id | int(11) | NO | PRI | NULL | auto_increment |
| paper_id | int(11) | NO | | NULL | |
| set_name | varchar(100) | NO | | NULL | |
| set_description | text | NO | | NULL | |
| set_maxtime | time | NO | | NULL | |
| created_date | datetime | NO | | NULL | |
| status | enum('Active','Inactive') | NO | | Active | |
+-----------------+---------------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> desc questions;
+---------------+---------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------------------------------------+------+-----+---------+----------------+
| question_id | int(11) | NO | PRI | NULL | auto_increment |
| set_id | int(11) | NO | | NULL | |
| question_text | varchar(50) | NO | | NULL | |
| option_1 | varchar(50) | NO | | NULL | |
| option_2 | varchar(50) | NO | | NULL | |
| option_3 | varchar(50) | NO | | NULL | |
| option_4 | varchar(50) | NO | | NULL | |
| answer | enum('option_1','option_2','option_3','option_4') | NO | | NULL | |
| status | enum('Active','Inactive') | NO | | Active | |
+---------------+---------------------------------------------------+------+-----+---------+----------------+
這些是表格課程連接的四個表格論文表格連接到question_sets和question_set連接到問題如果我刪除表格course中的course_id = 1行表格對應course_id應該得到刪除並且行對應表中的paper_id應該被刪除,並且該行對應於表中的question_id應該刪除我已經厭倦了下面的查詢它從論文表中刪除記錄是否有可能在所有的刪除操作中執行刪除操作表單查詢?如何從mysql中的四個表中刪除記錄?
delete from papers where course_id IN (select course_id from courses where course_id=7);
注:上面的查詢將刪除文件表中的行
使用'外鍵' – Daan
是否可以對單個查詢中的所有表執行刪除操作? –
相似的問題:http://stackoverflow.com/questions/2914936/mysql-foreign-key-constraints-cascade-delete – newman