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问题应该删除我已经厌倦了以下查询它从纸张表中删除记录是否有可能在所有表单执行删除操作查询?
delete from papers where course_id IN (select course_id from courses where course_id=7);
注意:上述查询会删除纸质表格中的行
答案 0 :(得分:1)
为什么要从单个查询中的四个表中删除?四个删除语句有什么问题?如果您需要确保所有删除都是以原子方式进行的,那么请在事务中包装四个删除。
答案 1 :(得分:0)
DELETE FROM courses, papers, question_sets , questions
USING courses JOIN papers JOIN question_sets JOIN questions
WHERE courses.course_id= 1
AND papers.course_id= 1
AND question_sets.paper_id=1
AND questions.set_id=1
答案 2 :(得分:-2)
您可以使用以下查询禁用外键检查,然后尝试删除或删除表
SET FOREIGN_KEY_CHECKS = 0;
drop table if exists courses;
drop table if exists papers;
drop table if exists question_sets;
SET FOREIGN_KEY_CHECKS = 1;