[每日一题] OCP1z0-047 :2013-08-28 DELETE..........................................................160_javascript

[每日一题] OCP1z0-047 :2013-08-28 DELETE..........................................................160_级联删除_02

[每日一题] OCP1z0-047 :2013-08-28 DELETE..........................................................160_javascript_03

 

正确答案:ACD


根据题库,操作如下:

A答案能删除:

 

oe@OCM> delete from order_items
  2  WHERE order_id IN (SELECT order_id FROM orders
  3  WHERE order_status IN(0,1));

110 rows deleted.

oe@OCM> rollback;

Rollback complete.


B答案不能删除:没有这样的语法(DELETE * FROM,即DELETE后直接跟表名,而不能用*)

 

 

oe@OCM> DELETE * 
  2  FROM order_items
  3  WHERE order_id IN(SELECT order_id
  4  FROM orders
  5  WHERE orders
  6  WHERE order_status IN(0,1));
DELETE *
       *
ERROR at line 1:
ORA-00903: invalid table name


C答案能删除:

 

 

oe@OCM> delete from order_items i
  2  WHERE order_id=(SELECT order_id FROM orders o
  3  WHERE i.order_id=o.order_id AND
  4  order_status IN(0,1));

110 rows deleted.

oe@OCM> rollback;

Rollback complete.


D答案也能删除:

 

 

oe@OCM> delete from(select * from order_items i,orders o
  2  WHERE i.order_id=o.order_id AND order_status in(0,1));


110 rows deleted.


分析D能删除的原因:


1、ORDERS表有主键ORDER_ID:CONSTRAINT "ORDER_PK" PRIMARY KEY ("ORDER_ID")。
看下面红色字体:

 

 

oe@OCM> select dbms_metadata.get_ddl('TABLE','ORDERS') from dual;

DBMS_METADATA.GET_DDL('TABLE','ORDERS')
--------------------------------------------------------------------------------

  CREATE TABLE "OE"."ORDERS"
   (    "ORDER_ID" NUMBER(12,0),
        "ORDER_DATE" TIMESTAMP (6) WITH LOCAL TIME ZONE CONSTRAINT "ORDER_DATE_NN" NOT NULL ENABLE,
        "ORDER_MODE" VARCHAR2(8),
        "CUSTOMER_ID" NUMBER(6,0) CONSTRAINT "ORDER_CUSTOMER_ID_NN" NOT NULL ENABLE,
        "ORDER_STATUS" NUMBER(2,0),
        "ORDER_TOTAL" NUMBER(8,2),
        "SALES_REP_ID" NUMBER(6,0),
        "PROMOTION_ID" NUMBER(6,0),
         CONSTRAINT "ORDER_MODE_LOV" CHECK (order_mode in ('direct','online')) ENABLE,
         CONSTRAINT "ORDER_TOTAL_MIN" CHECK (order_total >= 0) ENABLE,
         CONSTRAINT "ORDER_PK" PRIMARY KEY ("ORDER_ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS NOLOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "EXAMPLE"  ENABLE,
         CONSTRAINT "ORDERS_SALES_REP_FK" FOREIGN KEY ("SALES_REP_ID")
          REFERENCES "HR"."EMPLOYEES" ("EMPLOYEE_ID") ON DELETE SET NULL ENABLE,
         CONSTRAINT "ORDERS_CUSTOMER_ID_FK" FOREIGN KEY ("CUSTOMER_ID")
          REFERENCES "OE"."CUSTOMERS" ("CUSTOMER_ID") ON DELETE SET NULL ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS NOLOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "EXAMPLE"



2、ORDER_TIEMS表有外键ORDER_ID:CONSTRAINT "ORDER_ITEMS_ORDER_ID_FK" FOREIGN KEY ("ORDER_ID")
          REFERENCES "OE"."ORDERS" ("ORDER_ID") ON DELETE CASCADE ENABLE NOVALIDATE,并加上了主外键可以级联删除。
看下面红色字体:

 

 

oe@OCM> select dbms_metadata.get_ddl('TABLE','ORDER_ITEMS') from dual;

DBMS_METADATA.GET_DDL('TABLE','ORDER_ITEMS')
--------------------------------------------------------------------------------

  CREATE TABLE "OE"."ORDER_ITEMS"
   (    "ORDER_ID" NUMBER(12,0),
        "LINE_ITEM_ID" NUMBER(3,0) NOT NULL ENABLE,
        "PRODUCT_ID" NUMBER(6,0) NOT NULL ENABLE,
        "UNIT_PRICE" NUMBER(8,2),
        "QUANTITY" NUMBER(8,0),
         CONSTRAINT "ORDER_ITEMS_PK" PRIMARY KEY ("ORDER_ID", "LINE_ITEM_ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS NOLOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "EXAMPLE"  ENABLE,
         CONSTRAINT "ORDER_ITEMS_ORDER_ID_FK" FOREIGN KEY ("ORDER_ID")
          REFERENCES "OE"."ORDERS" ("ORDER_ID") ON DELETE CASCADE ENABLE NOVALIDATE,
         CONSTRAINT "ORDER_ITEMS_PRODUCT_ID_FK" FOREIGN KEY ("PRODUCT_ID")
          REFERENCES "OE"."PRODUCT_INFORMATION" ("PRODUCT_ID") ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS NOLOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "EXAMPLE"
  

 

         删除了子表上的记录:

 

oe@OCM> select count(*) from orders;    --主表

  COUNT(*)
----------
       105

oe@OCM> select count(*) from order_items;  --子表

  COUNT(*)
----------
       665

oe@OCM> delete from(select * from order_items i,orders o
  2     WHERE i.order_id=o.order_id AND order_status in(0,1));

110 rows deleted.

oe@OCM> select count(*) from orders;   --记录没变

  COUNT(*)
----------
       105

oe@OCM> select count(*) from order_items;  --记录少了110条

  COUNT(*)
----------
       555

 

 

          有些人D答案百思不得期解,模拟这个操作时一直报错(ERROR at line 1:
ORA-01752: cannot delete from view without exactly one key-preserved table)原因就在于自己去建了两个表order和order_items时没有在这两个表上建约束主外键,并且加上主外键可以级联删除(CONSTRAINT "ORDER_ITEMS_ORDER_ID_FK" FOREIGN KEY ("ORDER_ID") REFERENCES "OE"."ORDERS" ("ORDER_ID") ON DELETE CASCADE ENABLE NOVALIDATE)。
这个我就不再测试了,太简单了。

 

 

 

 

学习交流QQ群:
DSI&Core Search  Ⅰ 群:127149411(技术:已满)
DSI&Core Search  Ⅱ 群:177089463(技术:未满)
DSI&Core Search  Ⅲ 群:284596437(技术:未满)
DSI&Core Search  Ⅳ 群:192136702(技术:未满)
DSI&Core Search  Ⅴ 群:285030382(闲聊:未满)