当前位置:操作系统 > Unix/Linux >>

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

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

 
正确答案:ACD
 
 
根据题库,操作如下:
 
A答案能删除:
[html] 
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后直接跟表名,而不能用*)
[html] 
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答案能删除:
[html] 
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答案也能删除:
[html] 
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")。
 看下面红色字体:
 
[html] 
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,  
        <span style="color:#ff0000;"> CONSTRAINT "ORDER_PK" PRIMARY KEY ("ORDER_ID")</span>  
  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,并加上了主外键可以级联删除。
看下面红色字体:
[html] 
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,  
         <span style="color:#ff0000;">CONSTRAINT "ORDER_ITEMS_ORDER_ID_FK" FOREIGN KEY ("ORDER_ID")  
          REFERENCES "OE"."ORDERS" ("ORDER_ID") ON DELETE CASCADE ENABLE NOVALIDATE,</span>  
         CONSTRAINT "ORDER_ITEMS_PRODUCT_ID_FK" FOREIGN KEY ("PRODUCT_ID")  
          REFERENCES "OE"."PRODUCT_INFORMATION" ("PRODU
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,