oracle和mysql多表刪除數據的方法一大把,好多都是沒經過證實的,你很可能已經被錯誤信息誤導了,下面我以mysql兩張表刪除數據為例,來讓給為注意到這一點,我在mysql中新建了兩張表,分別是用戶表和國家表,如下所示。
用戶表users:
國家表country,如圖:
當你看到這兩張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的多表刪除。