我试了许多次,这样就可以哦:
这是我的两个表结构:
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
不好意思,不才只能做到这一步了,我反复找了许多个测试用例,
也就是这些结果了。希望能够对大家有些帮助,这样也不白忙活
两天!
如果有什么新的发现,还要来这里发帖哦!谢谢大家对不才的
帮助!!! :)