我已经创建了一个从三个表中删除数据的过程,这三个表都与外键链接但是没有运行。这是程序。
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”。“CUSTOMER_ID”:无效的identifierCompilation失败,第32行
答案 0 :(得分: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;