在删除客户上创建一个显示消息的触发器,"删除客户编号217"

时间:2018-04-26 23:50:23

标签: sql oracle ssh

这是一个家庭作业问题。 customer表包括:customer_num,customer_name,street,city,state,postal_code,balance,credit_limit,rep_num。该表将通过rep_num链接到rep表,并通过customer_num链接到orders表。我在customer表中插入了一行,因此我不会删除实际数据,所以插入行的代码是:

insert into customer
    values('217','Big Bird','123 Sesame
    Street','Pittsburgh','PA','15301',1.00,100.00,'15');

然后我将服务器输出设置为:

set serveroutput on;

这是我的代码,它让我拒绝,"警告:使用编译错误创建的触发器。":

create or replace trigger print_customer_deleted
after delete on customer for each row
begin
delete customer_num
set on customer = on.customer - new.customer_num
where customer_num = :new.customer_num;

dbms_output.put_line('Customer number '||customer_num||
                    ' is deleted');
end;
/

另外,这是我的触发器执行的方式吗? :

accept item_num prompt 'Enter customer number to be deleted: ';

execute print_customer_deleted('&customer_num');

1 个答案:

答案 0 :(得分:0)

我已经根据Scott的DEPT创建了一个TEST表。触发(在DEPT上删除后)从TEST中删除一行并显示一条消息。

SQL> select * From dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL> create table test as select * From dept;

Table created.

SQL> create or replace trigger trg_ad_dept
  2    after delete on dept
  3    for each row
  4  begin
  5    delete from test
  6      where deptno = :old.deptno;
  7
  8    dbms_output.put_line('Department #' || :old.deptno ||' is deleted');
  9  end;
 10  /

Trigger created.

让我们看看它是如何运作的:

SQL> delete from dept where deptno = 30;
Department #30 is deleted

1 row deleted.

SQL> select * From dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        40 OPERATIONS     BOSTON

SQL> select * From test;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        40 OPERATIONS     BOSTON

SQL>

似乎没问题 - 部门#30已从两个表格中删除了消息已显示。

将它应用到您的情况(我没有您的表,并且您没有提供测试用例)。