備考ocp_SQL專題之DELETE,備考ocp_sqldelete

來源:互聯網
上載者:User

備考ocp_SQL專題之DELETE,備考ocp_sqldelete

原創作品,出自 “深藍的blog” 部落格,歡迎轉載,轉載時請務必註明以下出處,否則追究著作權法律責任。

深藍的blog:http://blog.csdn.net/huangyanlong/article/details/43957113

 

delete示範:

1、查詢實驗資料

2、刪除表資料

3、該會話下查詢資料

4、復原資料

5、復原後,再次查看資料

題庫類比:

 

167、DELETE

167. Evaluate the following DELETE statement:

DELETE FROM sales;

There are no other uncommitted transactions on the SALES table.

Which statement is true about the DELETE statement?

A. It would not remove the rows if the table has a primary key.

B. It removes all the rows as well as the structure of the table.

C. It removes all the rows in the table and deleted rows can be rolled back.

D. It removes all the rows in the table and deleted rows cannot be rolled back.

答案:C,注意對於DML操作的理解(delete命令),未提交的語句是可以復原的。

 

168、DELETE

168. View the Exhibit and examine the description of SALES and PROMOTIONS tables.

You want to delete rows from the SALES table, where the PROMO_NAME column in the PROMOTIONS table has either blowout sale or everyday low price as values.

Which DELETE statements are valid? (Choose all that apply.)

A. DELETE

FROM sales

WHERE promo_id = (SELECT promo_id

FROM promotions

WHERE promo_name = 'blowout sale')

AND promo_id = (SELECT promo_id

FROM promotions

WHERE promo_name = 'everyday low price');

 

B. DELETE

FROM sales

WHERE promo_id = (SELECT promo_id

FROM promotions

WHERE promo_name = 'blowout sale')

OR promo_id = (SELECT promo_id

FROM promotions

WHERE promo_name = 'everyday low price');

 

C. DELETE

FROM sales

WHERE promo_id IN (SELECT promo_id

FROM promotions

WHERE promo_name = 'blowout sale'

OR promo_name = 'everyday low price');

 

D. DELETE

FROM sales

WHERE promo_id IN (SELECT promo_id

FROM promotions

WHERE promo_name IN ('blowout sale','everyday low price'));

答案:BCD,注意題幹中的“either A or B”表示“是A或者是B”。BCD的範圍都兩者滿足一個即可。A使用了and表示並且的意思,顯然不對。

169、DELETE

169. View the Exhibit and examine the description for the PRODUCTS and SALES table.

PROD_ID is a primary key in the PRODUCTS table and foreign key in the SALES table. You want to remove all the rows from the PRODUCTS table for which no sale was done for the last three years.

Which is the valid DELETE statement?

A. DELETE

FROM products

WHERE prod_id = (SELECT prod_id

FROM sales

WHERE time_id - 3*365 = SYSDATE );

 

B. DELETE

FROM products

WHERE prod_id = (SELECT prod_id

FROM sales

WHERE SYSDATE >= time_id - 3*365 );

 

C. DELETE

FROM products

WHERE prod_id IN (SELECT prod_id

FROM sales

WHERE SYSDATE - 3*365 >= time_id);

 

D. DELETE

FROM products

WHERE prod_id IN (SELECT prod_id

FROM sales

WHERE time_id >= SYSDATE - 3*365 );

答案:C,只有C滿足,這道題,其實考察了對於時間的理解,題幹中說是三年前的商品,就是表示系統時間(sysdate)減去3年的時間,要大於商品的ID時間(time_id)。另外一點,正如網友所說,這道題忽略對於約束條件的考察,這麼操作是違反約束條件的,是無法完成刪除操作的。我們可以做下面這個實驗:

 

--建立實驗表1,pk_test

createtable pk_test

(pk_date varchar2(32),

PK_ID varchar2(32)

);

--建立實驗表2,fk_test

createtable fk_test

(pk_date varchar2(32),

FK_ID varchar2(32)

);

--建立實驗表1的主鍵

-- Create/Recreate primary, unique and foreign key constraints

altertable PK_TEST

  addconstraint PK_PK_TESTprimarykey (PK_DATE);

--建立實驗表2的主鍵、外鍵(外鍵欄位為實驗表1的主鍵欄位)

-- Create/Recreate primary, unique and foreign key constraints

altertable FK_TEST

  addconstraint pk_fk_testprimarykey (FK_ID);

altertable FK_TEST

  addconstraint fk_pk_testforeignkey (PK_DATE)

  references pk_test (PK_DATE);

--向實驗表1插入實驗資料

insertinto pk_testvalues('pk_1','1');

commit;

--向實驗表2插入實驗資料

insertinto fk_testvalues('pk_1','1001');

commit;

--查詢實驗表1中的資料

select *from pk_test;

1  pk_1  1

--查看實驗表2中的資料

select *from fk_test;

1  pk_1  1001

--刪除實驗表1中的資料

deletefrom pk_test;

--報錯:有外鍵存在,子記錄被發現

--刪除實驗表2中的外鍵約束

-- Drop primary, unique and foreign key constraints

altertable FK_TEST

  dropconstraint FK_PK_TEST;

--再次刪除實驗表1中的資料,成功刪除

deletefrom pk_test;

commit;

--查詢實驗表1中的資料為空白

select *from pk_test;

小結:

        這說明當存在外鍵時,是無法直接對父表做刪除資料操作的。解決這個,可以採用將約束去掉,或連同SALES表中的資料一併刪除。

 

170、DELETE

170. Which two statements are true regarding the DELETE and TRUNCATE commands? (Choose two.)

A. DELETE can be used to remove only rows from only one table at a time.

B. DELETE can be used to remove only rows from multiple tables at a time.

C. DELETE can be used only on a table that is a parent of a referential integrity constraint.

D. DELETE can be used to remove data from specific columns as well as complete rows.

E. DELETE and TRUNCATE can be used on a table that is a parent of a referential integrity constraint having ON DELETE rule.

 

答案:AE

對於完整性條件約束的父表,需要在建表時使用ON DELETE CASCADE或ON DELETE CASCAD SET NULL條件子句,才可以使用delete或truncate對完整性條件約束的父表進行刪除操作。

關於 ON DELETE CASCADE或ON DELETE CASCAD SET NULL這兩個參數,在網上查看其它資料是這麼解釋的:

ON DELETE CASCADE(當刪除父表資料時,子表資料也一起刪除)

ON DELETE CASCAD SET NULL(當刪除父表資料時,子表相關的列設定為NULL)

可以在SQL Language Reference裡的Constraints部分尋找到ON DELETE子句。

 

單詞釋義:

regarding:關於

referential:指示的,用作參考的

integrity:完整

constraint:約束

 

原創作品,出自 “深藍的blog” 部落格,歡迎轉載,轉載時請務必註明以下出處,否則追究著作權法律責任。

深藍的blog:http://blog.csdn.net/huangyanlong/article/details/43957113

 

 

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.