Oracle SQL : delete from (query), delete which table's row?

简介:
今天群里面一位朋友问了一个问题如下 :
                                 DELETE FROM 
                                (SELECT * FROM EPAD_MENU EM, MSG_DESC MD 
                                WHERE MD.MD_TABLE = 'epad_menu' AND MD.MD_FIELD = 'em_id' 
                                AND MD.MD_VALUE = EM.EM_ID AND EM.EM_BOOK_CODE = 'aa')

为什么删除的是 MSG_DESC的数据?

于是找了个测试库测试一下 :
SQL> create table t1(id int primary key,info varchar2(10));

Table created.

SQL> create table t2(id int primary key,info varchar2(10));

Table created. 
SQL> insert into t1 values (1,'digoal');

1 row created.

SQL> insert into t1 values (2,'digoal');

1 row created.

SQL> insert into t2 values (1,'digoal');

1 row created.

SQL> insert into t2 values (2,'digoal');

1 row created.
SQL> commit;

Commit complete.

下面来写个类似的delete语句 :
SQL> delete from (select * from t1,t2 where t1.id=1 and t2.id=t1.id and t2.info='digoal');

1 row deleted.

SQL> select * from t2;

        ID INFO
---------- ----------
         1 digoal
         2 digoal

SQL> select * from t1;

        ID INFO
---------- ----------
         2 digoal

把t1.数据删了, t2的没有动.

下面把t1和t2的顺序换一下 :
SQL> rollback;

Rollback complete.

SQL> delete from (select * from t2,t1 where t1.id=1 and t2.id=t1.id and t2.info='digoal');

1 row deleted.

SQL> select * from t1;

        ID INFO
---------- ----------
         2 digoal
         1 digoal

SQL> select * from t2;

        ID INFO
---------- ----------
         2 digoal

SQL> rollback;

结果把t2的数据删了, t1没动.

来从执行计划看看是啥原因 :
SQL> explain plan for delete from (select * from t2,t1 where t1.id=1 and t2.id=t1.id and t2.info='digoal');

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1934688782

----------------------------------------------------------------------------------------------
| Id  | Operation                     | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------
|   0 | DELETE STATEMENT              |              |     1 |    33 |     1   (0)| 00:00:01 |
|   1 |  DELETE                       | T2           |       |       |            |          |
|   2 |   NESTED LOOPS                |              |     1 |    33 |     1   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS BY INDEX ROWID| T2           |     1 |    20 |     1   (0)| 00:00:01 |
|*  4 |     INDEX UNIQUE SCAN         | SYS_C0065664 |     1 |       |     1   (0)| 00:00:01 |
|*  5 |    INDEX UNIQUE SCAN          | SYS_C0065663 |     1 |    13 |     0   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - filter("T2"."INFO"='digoal')
   4 - access("T2"."ID"=1)
   5 - access("T1"."ID"=1)

19 rows selected.

SQL> explain plan for delete from (select * from t1,t2 where t1.id=1 and t2.id=t1.id and t2.info='digoal');

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2064908203

----------------------------------------------------------------------------------------------
| Id  | Operation                     | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------
|   0 | DELETE STATEMENT              |              |     1 |    33 |     2   (0)| 00:00:01 |
|   1 |  DELETE                       | T1           |       |       |            |          |
|   2 |   NESTED LOOPS                |              |     1 |    33 |     2   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN          | SYS_C0065663 |     1 |    13 |     1   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS BY INDEX ROWID| T2           |     1 |    20 |     1   (0)| 00:00:01 |
|*  5 |     INDEX UNIQUE SCAN         | SYS_C0065664 |     1 |       |     0   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("T1"."ID"=1)
   4 - filter("T2"."INFO"='digoal')
   5 - access("T2"."ID"=1)

19 rows selected.


目录
相关文章
|
7天前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
14 1
|
1月前
|
SQL 安全 关系型数据库
关系型数据库SQL server DELETE 语句
【8月更文挑战第3天】
59 10
|
2月前
|
SQL Oracle 关系型数据库
CREATE TABLE 时的 SQL FOREIGN KEY 约束
【7月更文挑战第24天】CREATE TABLE 时的 SQL FOREIGN KEY 约束。
40 5
|
2月前
|
SQL Oracle 关系型数据库
ALTER TABLE 时的 SQL PRIMARY KEY 约束
【7月更文挑战第24天】ALTER TABLE 时的 SQL PRIMARY KEY 约束。
23 3
|
2月前
|
SQL Oracle 关系型数据库
CREATE TABLE 时的 SQL FOREIGN KEY 约束
【7月更文挑战第19天】CREATE TABLE 时的 SQL FOREIGN KEY 约束
33 8
|
2月前
|
SQL Oracle 关系型数据库
CREATE TABLE 时的 SQL PRIMARY KEY 约束
【7月更文挑战第24天】CREATE TABLE 时的 SQL PRIMARY KEY 约束。
34 2
|
2月前
|
SQL 关系型数据库 MySQL
SQL CREATE TABLE 语句
【7月更文挑战第18天】SQL CREATE TABLE 语句。
33 6
|
2月前
|
SQL Oracle 关系型数据库
|
2月前
|
SQL 索引
SQL DELETE 实例
【7月更文挑战第12天】SQL DELETE 实例。
38 12
|
2月前
|
SQL Oracle 关系型数据库
ALTER TABLE 时的 SQL PRIMARY KEY 约束
【7月更文挑战第19天】ALTER TABLE 时的 SQL PRIMARY KEY 约束。
30 3

推荐镜像

更多
下一篇
DDNS