4

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'))); 
3
  • 1
    Could you clarify what you are trying to achieve? Your mention of reading the results into an array sounds like an attempted solution rather than the actual problem. If you need to delete from 3 tables, you just need to run 3 delete statements. Commented Jun 6, 2014 at 20:02
  • 3
    You can only delete from a single table at any given time - there's on way to delete from multiple tables at once Commented Jun 6, 2014 at 20:20
  • 1
    Added oracle tag because of the regexp_like Commented Jun 6, 2014 at 22:43

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I could be wrong, but I believe that you can delete from multiple tables at the same time. See also: docs.oracle.com/cd/E17952_01/refman-5.1-en/delete.html
@diadyne That page is part of the Oracle MySQL documentation. Oracle database's delete, which OP asks about, does not support multiple table expressions.
1

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.

2 Comments

I have done this in MySQL, do you know if it will work the same way in Oracle?
Yes: it's pretty much a standard RDBMS feature to cascade deletes on a foreign key
-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 Oracle's MySQL 5.1 DELETE documentation.

1 Comment

The link (it leads to error 404 now) appear to be for MySQL, while the question is about Oracle DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.