简体   繁体   English

有人可以帮我在 PL/SQL Oracle 中实现以下触发器吗?

[英]Can someone help me implement the following trigger in PL/SQL Oracle?

So,所以,

I have 4 tables:我有4张桌子:

Suppliers( id_sup, name, city)

Products (id_prod, name, city)

Companies (id_co, name, city)

Deliveries (id_sup, id_prod, id_co)

I need a trigger so that if I want to update the city of a Supplier, I am not allowed if that supplier has a delivery where the product it delivers and the company it delivers to have the same city as it.我需要一个触发器,以便如果我想更新供应商的城市,如果该供应商的交货地点与其交付的产品和交付的公司具有相同的城市,则不允许我这样做。

This is what I've tried so far, but it's not working:这是我迄今为止尝试过的,但它不起作用:

CREATE OR REPLACE TRIGGER secure_suppliers

BEFORE UPDATE ON Suppliers

BEGIN

IF UPDATING ('city') THEN

IF (suppliers.id_sup IN (SELECT id_sup FROM Deliveries) AND suppliers.city = (Select p.city From Products p INNER JOIN Deliveries d ON (p.id_prod = d.id_prod)) AND suppliers.city = (Select c.city From Companies c INNER JOIN Deliveries d ON (c.id_co = d.id_co))) THEN

RAISE_APPLICATION_ERROR(-20500, 'Can't update city!');

End if;

End;

You can use one query to find if product , supplier and company are in one city with new value of the supplier then proceed with error handling in the trigger as following:您可以使用一个查询来查找productsuppliercompany是否在一个城市中,并使用supplier新值,然后在trigger进行错误处理,如下所示:

CREATE OR REPLACE TRIGGER secure_suppliers
    For each row -- row level trigger
    BEFORE UPDATE OF city ON Suppliers -- trigger will execute on update of column city only

DECLARE
cnt NUMBER := 0;

BEGIN    
select count(1)
Into cnt
From deliveries d
Join companies c on d.id_co = c.id_co
Join products p on d.id_prod = p.id_prod
Where p.city = c.city
And p.city = :new.city;

If cnt > 0 then
RAISE_APPLICATION_ERROR(-20500, 'Can''t update city!'); 
End if;

End;
/

Cheers!!干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM