oracle怎么删除联结的表,SEE HERE:找到连接表更新和删除的答案了!

我试了许多次,这样就可以哦:

这是我的两个表结构:

SQL> desc test1;

Name Type         Nullable Default Comments

---- ------------ -------- ------- --------

NUM  NUMBER

NAME VARCHAR2(20) Y

SQL> desc test2;

Name   Type         Nullable Default Comments

------ ------------ -------- ------- --------

NUM    NUMBER

NAME   VARCHAR2(20) Y

RECORD NUMBER       Y        0

其中主键都是NUM字段,然后运行这样的语句:

SQL> delete (select a.num a1,a.name a2,b.num b1,b.name b2,b.record b3

2    from test1 a,test2 b where a.num = b.num)

3  where a1 = 704;

delete (select a.num a1,a.name a2,b.num b1,b.name b2,b.record b3

from test1 a,test2 b where a.num = b.num)

where a1 = 704

ORA-01752: 不能从没有一个键值保存表的视图中删除

说明其中的连接表的主键有问题,事实上,这个临时表中有两个重复的主键,都是NUM字段。好象这样就不行的,可以看看临时表的

结构:

SQL>

SQL> select a.num a1,a.name a2,b.num b1,b.name b2,b.record b3

2    from test1 a,test2 b where a.num = b.num

3  ;

A1 A2                           B1 B2                           B3

---------- -------------------- ---------- -------------------- ----------

701 zhangming                   701 zhangming                     1

702 chenhua                     702 chenhua                       2

704 penghui                     704 pe454434i                     3

709 xiaojun                     709 xi455un                       4

721 likaixia                    721 likaixia                      5

然后我更换了TEST1表中的主键为NAME字段:

SQL> alter table test1 drop primary key;

Table altered

SQL> alter table test1 add primary key (name);

Table altered

再次执行DELETE语句,成功!

SQL>

SQL> delete (select a.num a1,a.name a2,b.num b1,b.name b2,b.record b3

2    from test1 a,test2 b where a.num = b.num)

3  where a1 = 704;

1 row deleted

再看看那个临时表的数据:

SQL>

SQL> select a.num a1,a.name a2,b.num b1,b.name b2,b.record b3

2  from test1 a,test2 b where a.num = b.num

3  ;

A1 A2                           B1 B2                           B3

---------- -------------------- ---------- -------------------- ----------

701 zhangming                   701 zhangming                     1

702 chenhua                     702 chenhua                       2

709 xiaojun                     709 xi455un                       4

721 likaixia                    721 likaixia                      5

其中含有A1=704的一行已经没有了,这就是rainy说的,系统只

删除了非驱动表中满足条件的那一行,这样,系统觉得临时表中

就没有了符合条件的行,认为执行正常且满足用户目的。但是实

际上没有,因为我们的目的是删除两个表中同时符合NUM=704的

那些行,所以会出现没有删除TEST2中的那些行,显示如下:

SQL> select * from test1;

NUM NAME

---------- --------------------

701 zhangming

702 chenhua

709 xiaojun

721 likaixia

SQL> select * from test2;

NUM NAME                     RECORD

---------- -------------------- ----------

701 zhangming                     1

702 chenhua                       2

704 pe454434i                     3

709 xi455un                       4

721 likaixia                      5

904 penghui                       6

909 xi32545un                     7

921 likaixia                      8

8 rows selected

不好意思,不才只能做到这一步了,我反复找了许多个测试用例,

也就是这些结果了。希望能够对大家有些帮助,这样也不白忙活

两天!

如果有什么新的发现,还要来这里发帖哦!谢谢大家对不才的

帮助!!! :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值