Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DELETE FROM <subquery>

DELETE FROM (SELECT * FROM orders1 o,order_item1 i
WHERE i.order_id = o.order_id );

above query has not mentioned particular to delete rows.Instead of i have mentioned a sub query. But this query executes without any error and deletes order_item1 table rows.

How oracle server decide which table rows should be deleted? why orders1 table rows cannot be deleted?

like image 672
TNK Avatar asked Jan 25 '14 07:01

TNK


People also ask

Can I use the table I delete in a subquery?

Update: As answered below, MySQL does not allow the table you're deleting from be used in a subquery for the condition. Attention: Good answer at the bottom stackoverflow.com/a/4471359/956397 simply add the table alias after DELETE t FROM table t ...

What is a subquery in SQL?

Ignacio L. Bisso sql learn sql subquery Did you know that subqueries can also be used in UPDATE, INSERT, and DELETE statements? Subqueries in SQL are like building blocks that can be used in a variety of statements, not just SELECT. If you find subqueries useful in SELECT, read this article to find out how useful they can be in other instructions.

How to delete records using subqueries with Alias in SQL?

the following SQL statement can be used: SQL Code: DELETE FROM agent1 da WHERE EXISTS( SELECT * FROM customer cu WHERE grade=3 AND da.agent_code<>cu.agent_code); Output: SQL delete records using subqueries with alias and IN

Is it possible to delete duplicates from subqueries in SQL Server?

Deleting from subqueries and CTEs is fully supported and very efficient, particularly for removing duplicates. I also seem to recall using it on older versions of SQL Server.


1 Answers

In your case, the subquery's table orders1 has most certainly a primary key (order_id ) and the table order_item1 has the corresponding foreign key order_id.

Thus, Oracle turns the table order_item1 into a key preserved table and is able to delete from this table in the outer delete statement.

The concept of key preserved tables is further explained at This tahiti documentation link.

If you want to find out what is deleted you can go like so

These are the tables that are joined in your subquery:

create table tq84_orders1 (
   id     number primary key,
   col_1  varchar2(10),
   col_2  date
);

create table tq84_order_item1 (
  order_id references tq84_orders1,
  col_3    varchar2(10),
  col_4    date
);

This view emulates the subquery:

create view tq84_orders_v as (
  select * 
  from
    tq84_orders1     o,
    tq84_order_item1 i
  where
    o.id = i.order_id
);

This query (on user_updateable_columns) now finds which columns actually get deleted (or can be deleted):

select
  table_name,
  column_name,
--updatable,
--insertable,
  deletable
from
  user_updatable_columns
where
  table_name = 'TQ84_ORDERS_V';

The result shows that in effect the three columns ORDER_ID, COL_3 and COL_4 can be deleted, all of which stem from TQ84_ORDER_ITEM1.

TABLE_NAME                     COLUMN_NAME                    DEL
------------------------------ ------------------------------ ---
TQ84_ORDERS_V                  ID                             NO
TQ84_ORDERS_V                  COL_1                          NO
TQ84_ORDERS_V                  COL_2                          NO
TQ84_ORDERS_V                  ORDER_ID                       YES
TQ84_ORDERS_V                  COL_3                          YES
TQ84_ORDERS_V                  COL_4                          YES
like image 53
René Nyffenegger Avatar answered Sep 22 '22 17:09

René Nyffenegger