刪除語句 - 子查詢應該拋出錯誤

[英]Delete statement - Sub Query should throw error


I have created two tables, T1 and T2 with one column each, abc and xyz respectively. I have inserted 2 rows (numeric values 1 and 2) in each table.

我創建了兩個表,T1和T2,每個表分別有一列,abc和xyz。我在每個表中插入了2行(數值1和2)。

When I run the command "select abc from t2", it throws an error saying that column abc does not exist in the table T2. However, when I run the command "delete from t1 where abc in (SELECT abc from t2);", 2 rows are deleted.

當我運行命令“從t2選擇abc”時,它會拋出一個錯誤,表示表T2中不存在列abc。但是,當我運行命令“從t1刪除abc in(SELECT abc from t2);”時,刪除2行。

Shouldn't the delete fail as I have used the same statement which failed in the sub-query?

不應該刪除失敗,因為我使用了在子查詢中失敗的相同語句?

create table t1 (abc number); --Table created

create table t1(abc number); - 表創建

create table t2 (xyz number); --Table created

創建表t2(xyz數); - 表創建

insert into t1 values (1); --One row inserted

插入t1值(1); - 插入一行

insert into t1 values (2); --One row inserted

插入t1值(2); - 插入一行

insert into t2 values (1); --One row inserted

插入t2值(1); - 插入一行

insert into t2 values (2); --One row inserted

插入t2值(2); - 插入一行

SELECT abc from t2; --ORA-00904 -> Because column abc does not exist in t2

從t2中選擇abc; --ORA-00904 - >因為t2中不存在列abc

delete from t1 where abc in (SELECT abc from t2); --2 rows deleted

從t1刪除abc in(SELECT abc from t2); - 刪除了2行

2 个解决方案

#1


11  

If you use the table names as alias to make sure table t2 column is getting selected, you will get the error i.e.

如果您使用表名作為別名以確保選擇了表t2列,您​​將收到錯誤,即

 delete from t1 where abc in (SELECT t2.abc from t2); --ORA-00904 

Your original query is not failing because it's using abc column of table t1 since table t1 is visible in the subquery.

您的原始查詢沒有失敗,因為它使用表t1的abc列,因為表t1在子查詢中可見。

#2


1  

Your Delete statement is working because of the abc column name which u have used in Where condition. sub query is executing based on the where condition column, becz we d't use the table alias name.

您的Delete語句正在工作,因為您在Where條件中使用了abc列名。子查詢正在基於where條件列執行,因為我們使用表別名。

if u see these queries

如果你看到這些問題

select * from t1 where abc in (SELECT abc from t2); -- it 'll give 2 rows

select * from t1 where abc in(SELECT abc from t2); - 它會給2行

select * from t1 where abc in (SELECT 1 from t2); -- it 'll give 1 row

select * from t1 where abc in(SELECT 1 from t2); - 它會給出一排

select * from t1 where abc in (SELECT 2 from t2); -- it 'll retrieve 2nd row

select * from t1 where abc in(SELECT 2 from t2); - 它將檢索第二行

select * from t1 where abc in (SELECT 3 from t2); -- w't get d data

select * from t1 where abc in(SELECT 3 from t2); - 得到d數據

select * from t1 where abc in (SELECT hg from t2); -- Invalid Identifier

select * from t1 where abc in(SELECT hg from t2); - 不合法的識別符


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/12/26/729da59e5499cf3bc1ecdd61c3636aa2.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com