Oracle 多表联合删除?--转

oracle和mysql多表删除数据的方法一大把,好多都是没经过证实的,你很可能已经被错误信息误导了,下面我以mysql两张表删除数据为例,来让给为注意到这一点,我在mysql中新建了两张表,分别是用户表和国家表,如下所示。

用户表users:

QQ截图20171116215656.jpg

国家表country,如图:

QQ截图20171116215705.jpg

当你看到这两张mysql表的时候,你一定认为多表数据删除的语句是这样的,其实这样是错误的!,如下。

delete from users u,country c where u.id = c.userId and u.id = 20

mysql多表删除用上面的语句会报sql syntax语法错误的,报错如下。

[SQL]

delete from users u,country c where u.id = c.userId and u.id = 20

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'u,country c where u.id = c.userId and u.id = 20' at line 1

mysql多表删除数据正确的方法应该使用内连接来删除,如下两条语句,可任选一句。

//语句一
delete u,c from users u INNER JOIN country c on u.id = c.userId and u.id = 20

//语句二
delete u,c from users u INNER JOIN country c where u.id = c.userId and u.id = 10

这个时候你一定会认为,oracle删除多表数据能否用上面这两条语句?答案是:不行!它会报如下错误。

“ORA-00933:SQL命令未正确使用”

说明oracle使用上面的两条语句多表删除数据是不行的,那oracle多表删除数据该怎么写呢?命令如下。

//oracle中只能分开执行
delete from users where users.id = 20
delete from country where country.userId = 20

在oracle中不能进行多表关联删除,这可能跟oracle数据库的安全机制有关,你只能把上面的语句分成两条sql语句来执行才可以实现oracle的多表删除。

转自 :http://www.tpyyes.com/a/mysql_oracle/2017/1116/383.html

原文地址:https://www.cnblogs.com/Mr-Simple001/p/10966562.html