2013-04-29 32 views
0

我創建了一個從三個表中刪除數據的過程,這些表都與外鍵鏈接,但不會運行。這裏是程序。刪除程序不會運行並給出錯誤?

PROCEDURE remove_customer (customer_id VARCHAR2) IS 
BEGIN 
DELETE FROM order_line 
WHERE order_line.order_id = placed_order.order_id; 
DELETE FROM placed_order 
WHERE placed_order.customer_id = customer.customer_id; 
DELETE FROM customer 
WHERE customer.customer_id = remove_customer.customer_id; 
total_customers := total_customers - 1; 
END; 

這裏是三個表格的結構。任何人知道我爲什麼會得到錯

customer(
    Customer_ID INTEGER NOT NULL, 
    Country VARCHAR(15), 
    First_Name VARCHAR(30), 
    Last_Name VARCHAR(30), 
    CONSTRAINT pk_customer PRIMARY KEY (Customer_ID) 
); 


    placed_order(
    order_id INTEGER NOT NULL, 
    order_date VARCHAR(15), 
    fk1_Customer_ID INTEGER NOT NULL, 
    fk2_Employee_ID INTEGER NOT NULL, 
    fk3_order_type_id INTEGER NOT NULL, 
    CONSTRAINT pk_placed_order PRIMARY KEY (order_id) 
); 


    order_line(
    order_line_id INTEGER NOT NULL, 
    fk1_order_id INTEGER NOT NULL, 
    fk2_product_id INTEGER NOT NUL, 
    CONSTRAINT pk_order_line PRIMARY KEY (order_line_id) 
); 

這是兩個錯誤的迴歸。

ORA-00904: 「PLACED_ORDER」 「ORDER_ID」:無效identifierCompilation失敗,行30

ORA-00904: 「用戶」, 「CUSTOMER_ID」:無效identifierCompilation失敗,行32

+0

如果你還沒有,我建議你在你的表上定義真正的外鍵。 – 2013-04-29 15:12:04

+0

H,是的那裏已經定義,但感謝提醒 – 2013-04-29 15:36:14

回答

2

你不能只需在DELETE語句中引用其他表中的列即可。每條語句都需要從參數值customer_id開始。這應該做你想要的:

PROCEDURE remove_customer (customer_id VARCHAR2) IS 
BEGIN 

    DELETE FROM order_line 
    WHERE order_line.order_id in 
    (SELECT order_id FROM placed_order 
    WHERE placed_order.customer_id = remove_customer.customer_id 
); 

    DELETE FROM placed_order 
    WHERE placed_order.customer_id = remove_customer.customer_id; 

    DELETE FROM customer 
    WHERE customer.customer_id = remove_customer.customer_id; 

    total_customers := total_customers - 1; 

END; 
+0

嗨,感謝您的意思,有一件事我得到一個SQL命令沒有正確結束,它是否缺少';'在第一次刪除? – 2013-04-29 13:11:37

+0

是的 - 現在增加 – 2013-04-29 13:12:11

+0

當它沒有的時候,它相當奇怪;它只是有這個錯誤,現在我已經添加它給我加載或錯誤? ORA-00904: 「ORDER_LINE」 「ORDER_ID」:無效identifierCompilation失敗 PLS-00302:組件 'CUSTOMER_ID' 必須declaredCompilation失敗 PL/SQL:ORA-00904: 「PLACED_ORDER」「CUSTOMER_ID 「:無效的標識符編譯失敗 有什麼想法? – 2013-04-29 13:16:58

相關問題