怎么理解数据库集合元素的删除

这篇文章主要介绍“怎么理解数据库集合元素的删除”,在日常操作中,相信很多人在怎么理解数据库集合元素的删除问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么理解数据库集合元素的删除”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

荔城ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

我创建了一个嵌套表类型和过程:

CREATE OR REPLACE TYPE plch_numbers_t IS TABLE OF NUMBER;
/
CREATE OR REPLACE PROCEDURE plch_show_numbers (
   title_in     IN VARCHAR2
, numbers_in   IN plch_numbers_t)
IS
BEGIN
   DBMS_OUTPUT.put_line (title_in);
   FOR rec IN (SELECT COLUMN_VALUE FROM TABLE (numbers_in))
   LOOP
      DBMS_OUTPUT.put_line (rec.COLUMN_VALUE);
   END LOOP;
END;
/

下面的选项用来代替下列代码块的 /*BODY*/ 注释:

DECLARE
   l_numbers   plch_numbers_t
       := plch_numbers_t (12, 23, 34, 45, 56
                        , 67, 78, 89, 90, 100);
BEGIN
   /*BODY*/
   plch_show_numbers ('AFTER DELETE', l_numbers);
END;
/

哪些选项使得这个块执行之后会显示如下文本:

AFTER DELETE
23
45
67
89

换而言之,所有偶数元素都被删除了。

(A)

FOR indx IN 1 .. l_numbers.COUNT
LOOP
   IF MOD (indx, 2) = 0
   THEN
      l_numbers.delete (indx);
   END IF;
END LOOP;
SQL> DECLARE
  2    l_numbers plch_numbers_t := plch_numbers_t(12,
  3                                               23,
  4                                               34,
  5                                               45,
  6                                               56,
  7                                               67,
  8                                               78,
  9                                               89,
 10                                               90,
 11                                               100);
 12  BEGIN
 13    FOR indx IN 1 .. l_numbers.COUNT LOOP
 14      IF MOD(indx, 2) = 0 THEN
 15        l_numbers.delete(indx);
 16      END IF;
 17    END LOOP;
 18  
 19    plch_show_numbers('AFTER DELETE', l_numbers);
 20  END;
 21  /
AFTER DELETE
12
34
56
78
90
PL/SQL procedure successfully completed
SQL>

(B)

FOR indx IN 1 .. l_numbers.COUNT
LOOP
   IF MOD (l_numbers (indx), 2) = 0
   THEN
      l_numbers.delete (indx);
   END IF;
END LOOP;
SQL> DECLARE
  2    l_numbers plch_numbers_t := plch_numbers_t(12,
  3                                               23,
  4                                               34,
  5                                               45,
  6                                               56,
  7                                               67,
  8                                               78,
  9                                               89,
 10                                               90,
 11                                               100);
 12  BEGIN
 13    FOR indx IN 1 .. l_numbers.COUNT LOOP
 14      IF MOD(l_numbers(indx), 2) = 0 THEN
 15        l_numbers.delete(indx);
 16      END IF;
 17    END LOOP;
 18  
 19    plch_show_numbers('AFTER DELETE', l_numbers);
 20  END;
 21  /
AFTER DELETE
23
45
67
89
PL/SQL procedure successfully completed
SQL>

(C)

DELETE FROM TABLE (l_numbers)
      WHERE MOD (index_value, 2) = 0;
SQL> DECLARE
  2    l_numbers plch_numbers_t := plch_numbers_t(12,
  3                                               23,
  4                                               34,
  5                                               45,
  6                                               56,
  7                                               67,
  8                                               78,
  9                                               89,
 10                                               90,
 11                                               100);
 12  BEGIN
 13    DELETE FROM TABLE(l_numbers) WHERE MOD(index_value, 2) = 0;
 14  
 15    plch_show_numbers('AFTER DELETE', l_numbers);
 16  END;
 17  /
DECLARE
  l_numbers plch_numbers_t := plch_numbers_t(12,
                                             23,
                                             34,
                                             45,
                                             56,
                                             67,
                                             78,
                                             89,
                                             90,
                                             100);
BEGIN
  DELETE FROM TABLE(l_numbers) WHERE MOD(index_value, 2) = 0;
  plch_show_numbers('AFTER DELETE', l_numbers);
END;
ORA-06550: 第 13 行, 第 15 列: 
PL/SQL: ORA-00903: 表名无效
ORA-06550: 第 13 行, 第 3 列: 
PL/SQL: SQL Statement ignored
SQL>

(D)

FOR rec IN (SELECT *
              FROM TABLE (l_numbers)
             WHERE MOD (COLUMN_VALUE, 2) = 0)
LOOP
   l_numbers.delete (rec.COLUMN_VALUE);
END LOOP;
SQL> DECLARE
  2    l_numbers plch_numbers_t := plch_numbers_t(12,
  3                                               23,
  4                                               34,
  5                                               45,
  6                                               56,
  7                                               67,
  8                                               78,
  9                                               89,
 10                                               90,
 11                                               100);
 12  BEGIN
 13    FOR rec IN (SELECT * FROM TABLE(l_numbers) WHERE MOD(COLUMN_VALUE, 2) = 0) LOOP
 14      l_numbers.delete(rec.COLUMN_VALUE);
 15    END LOOP;
 16  
 17    plch_show_numbers('AFTER DELETE', l_numbers);
 18  END;
 19  /
AFTER DELETE
12
23
34
45
56
67
78
89
90
100
PL/SQL procedure successfully completed
SQL>

实测答案为B

到此,关于“怎么理解数据库集合元素的删除”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


标题名称:怎么理解数据库集合元素的删除
链接分享:http://ybzwz.com/article/pjsshd.html