2016-02-04 68 views
2
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); 

注:上面的查詢將刪除文件表中的行

+0

使用'外鍵' – Daan

+0

是否可以對單個查詢中的所有表執行刪除操作? –

+0

相似的問題:http://stackoverflow.com/questions/2914936/mysql-foreign-key-constraints-cascade-delete – newman

回答

-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; 
+0

詢問有關從表中刪除記錄,而不是關於刪除它。 – newman

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 
+0

顯示此錯誤#1066 - 非唯一表/別名:'論文' –

+0

course_id,paper_id,set_id可能有所不同 –

+0

例如:我想從表中刪除和填充filde course_id表中的行課程表是外鍵在表格文件和filde paper_id表的論文是一個外鍵在表question_sets question_sets.paper_id = papers.paper_id和filde set_id表question_sets是一個外鍵在表問題questions.set_id = question_sets.set_id –

1

爲什麼你想從一個查詢中的四個表中刪除嗎?四條刪除語句有什麼問題?如果您需要確保所有刪除操作都是以原子方式進行的,請將這四個刪除操作包裝在一個事務中。

+0

即時通訊將執行此刪除操作在PHP中,如果我使用四個刪除語句它是一個漫長的過程 –