备考ocp_SQL专题之DELETE
原创作品,出自 “深蓝的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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。