Select from SQL: 4 Apr - 10 Apr 2020(Quiz on UPDATE statement)

原題鏈接:
點擊訪問🔗

Minimum Version:
Oracle Database 11g Release 2

題目:

I have a table of countries partitioned by region:

create table qz_countries (
   country     varchar2(20) not null
 , region      varchar2(4)  not null
                  check (region in ('AMER', 'ASOC', 'EMEA'))
 , manager     varchar2(10) not null
)
partition by list (region) (
   partition part_amer values ('AMER')
 , partition part_asoc values ('ASOC')
 , partition part_emea values ('EMEA')
);

insert into qz_countries values ('Canada'   , 'AMER', 'James'  );
insert into qz_countries values ('Brazil'   , 'AMER', 'James'  );
insert into qz_countries values ('China'    , 'ASOC', 'Wang'   );
insert into qz_countries values ('Thailand' , 'ASOC', 'Mai-Lin');
insert into qz_countries values ('Australia', 'ASOC', 'Mai-Lin');
insert into qz_countries values ('Sweden'   , 'EMEA', 'Heidi'  );
insert into qz_countries values ('Germany'  , 'EMEA', 'Heidi'  );

commit;
I want to update all of the countries in region ASOC except China to have a new manager: Janice.

Which of the choices performs such an update without raising errors, so that after choice execution the following test query returns the shown output:

select country, manager
from qz_countries
where region = 'ASOC'
order by country;

COUNTRY              MANAGER
-------------------- ----------
Australia            Janice
China                Wang
Thailand             Janice

題目中創建一個一個表格 qz_countries ,然後以 region列 爲分區鍵創建列分區
要求將所有 region爲ASOC 且 country不是China 的 manager字段值更新爲 Janice,選出不會報錯的選項。

Choice 1

update qz_countries c
set c.manager = 'Janice'
where c.region = 'ASOC'
and   c.country != 'China';

Nice and simple update and the optimizer will almost certainly access only the PART_ASOC partition.
最簡便的操作。

Choice 2

update qz_countries partition part_asoc c
set c.manager = 'Janice'
where c.country != 'China';

It is allowed to specify partition in the UPDATE statement, but what we should specify is a list of partitions (that might then have just one partition in the list) within parentheses. Here we are missing the parentheses, so the parser expects something else and raises error: ORA-00971: missing SET keyword.
partition 需指定的是括號內的分區列表,沒有括號將會報錯:ORA-00971: missing SET keyword

Choice 3

update qz_countries partition (part_asoc) c
set c.manager = 'Janice'
where c.country != 'China';

Adding parentheses fixes the mistake in the previous choice. Now it is a list (albeit just with one member) of partitions.
parentheses 表示括號的意思,這裏加了括號是正確的選項。

Choice 4

update qz_countries partition for ('ASOC') c
set c.manager = 'Janice'
where c.country != 'China';

Instead of specifying a partition name, we can also use PARTITION FOR and specify a value. This is good, as we might not always be in control of the partition names - for example if we had used automatic list partitioning.
PARTITION FOR 加特定的value,可以在AUTO分列時,沒有指定的列名稱也可以使用。

Choice 5

update (
   select *
   from qz_countries
   where region = 'ASOC'
) c
set c.manager = 'Janice'
where c.country != 'China';

Yes, this works too, but it’s not very logical to do it like this instead of choice 1. But if the inline view had been a real view instead of inline, then this shows how that could have worked too.
我沒有選擇這個選項,然而這裏的查詢語句是可以成功的。
與選項一類似,-這裏只是將條件語句拆開成兩步,首先根據條件region = 'ASOC'將查詢的結果作爲表c,然後再根據條件c.country != 'China'更新manager的值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章