Trying to delete from multiple tables using SQL

被刻印的时光 ゝ 提交于 2019-12-09 21:14:17

问题


I have 4 tables in our application:

  • User
  • usession
  • upklist
  • projshare

The last three tables contain a field called session_id.

In the code below, the section in parenthesis works to get all session_id values from usession table for user "awpeople".

The problem is how do I read this result set into an array and delete from all three tables where session_id is in the array results.

Code:

DELETE FROM usession, 
            upklist, 
            projshar 
WHERE  session_id = (SELECT session_id 
                     FROM   usession 
                     WHERE  delete_session_id IS NULL 
                            AND user_id = (SELECT user_id 
                                           FROM   users 
                                           WHERE  REGEXP_LIKE(USER_NAME, 
                                                  'awpeople', 'i'))); 

回答1:


delete can only handle one table at a time, so you'd need three statements:

DELETE FROM upklist 
WHERE  session_id = (SELECT session_id 
                     FROM   usession 
                     WHERE  delete_session_id IS NULL 
                            AND user_id = (SELECT user_id 
                                           FROM   users 
                                           WHERE  REGEXP_LIKE(USER_NAME, 
                                                  'awpeople', 'i'))); 

DELETE FROM projshar 
WHERE  session_id = (SELECT session_id 
                     FROM   usession 
                     WHERE  delete_session_id IS NULL 
                            AND user_id = (SELECT user_id 
                                           FROM   users 
                                           WHERE  REGEXP_LIKE(USER_NAME, 
                                                  'awpeople', 'i'))); 

DELETE FROM usession 
WHERE  session_id = (SELECT session_id 
                     FROM   usession 
                     WHERE  delete_session_id IS NULL 
                            AND user_id = (SELECT user_id 
                                           FROM   users 
                                           WHERE  REGEXP_LIKE(USER_NAME, 
                                                  'awpeople', 'i'))); 

Note that since the inner query relies on usersession, you should delete from it last.




回答2:


If usession has a unique or primary key on session_id, and the other tables have foreign key relationships to it, then you can just delete the row from usession and have the database cascade it to the child tables.




回答3:


Multiple-table syntax:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] tbl_name[.] [, tbl_name[.]] ... FROM table_references [WHERE where_condition]

Or:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name[.] [, tbl_name[.]] ... USING table_references [WHERE where_condition]

This comes from the oracle DELETE documentation.



来源:https://stackoverflow.com/questions/24089695/trying-to-delete-from-multiple-tables-using-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!