简体   繁体   English

如何在oracle中创建触发器,以确保如果卫星日期早于当前日期,则不会删除任何记录?

[英]how to create a trigger in oracle which will ensure that no records is deleted if satrt date is earlier than current date?

I have a table Movie. 我有一桌电影。

create table Movie
(
mv_id number(5),
mv_name varchar2(30),
startdate date
)
insert into Movie values(1,'AVATAR','8-MAR-2012');
insert into Movie values(2,'MI3','20-MAR-2012');
insert into Movie values(3,'BAD BOYS','10-Feb-2012');

I want to create a trigger which will ensure that no records is deleted if satrt date is earlier than current date. 我想创建一个触发器,以确保如果卫星日期早于当前日期,则不会删除任何记录。 My trigger code is -- 我的触发代码是-

create  or replace  trigger trg_1
before delete
on Movie
for each row
when(old.startdate < sysdate)
begin
raise_application_error(-20001, 'Records can not be deleted');
end;

The trigger is created.When I execute this code to delete data -- 创建触发器。执行此代码以删除数据时-

delete from Movie where mv_id=1;

Then the trigger fires but with errors, I don't know why I am getting such error. 然后触发触发器但出现错误,我不知道为什么会收到这样的错误。 I dont want any error, i want only message. 我不要任何错误,我只想要消息。 This is the error -- 这是错误-

delete from Movie where mv_id=1
            *
ERROR at line 1: 
ORA-20010: Records can not be deleted
ORA-06512: at "OPS$0924769.TRG_1", line 3 
ORA-04088: error during execution of trigger 'OPS$0924769.TRG_1' 

I want to get rid of this error. 我想摆脱这个错误。

The only way for a trigger on a table to prevent any operation is throw an exception. 在表上触发触发器以防止任何操作的唯一方法是引发异常。 You cannot have a trigger that prevents a DELETE and prints a message. 您不能使用阻止DELETE并显示消息的触发器。

You can write a trigger that attempts to display a message and that allows the delete to happen 您可以编写一个触发器来尝试显示一条消息,并允许进行删除

create  or replace  trigger trg_1
  before delete
  on Movie
  for each row
  when(old.startdate < sysdate)
begin
  dbms_output.put_line( 'Records can not be deleted');
end;

If the client happens to be configured to display the data written to the DBMS_OUTPUT buffer (most clients will not), this will display a message. 如果客户端恰好配置为显示写入DBMS_OUTPUT缓冲区的数据(大多数客户端不会),这将显示一条消息。 But it will also allow the delete to be successful which does not sound like what you want. 但是,这也将使删除操作听起来像您想要的那样成功。

Add exception handling in your delete statement. 在您的delete语句中添加异常处理。

All you need to do is write the delete statement in its own BEGIN and END block, and handle the exception there which is thrown from trigger. 您需要做的就是将delete语句写入其自己的BEGIN和END块中,并处理从触发器引发的异常。 No row would be deleted if it matches the condition; 如果符合条件,则不会删除任何行; however, the message to deny record deletion would be displayed using DBMS_OUTPUT command instead. 但是,拒绝记录删除的消息将改为使用DBMS_OUTPUT命令显示。

The below code does what you want: 以下代码可满足您的需求:

BEGIN
DELETE FROM MOVIE WHERE MV_ID = 1;

EXCEPTION
  WHEN OTHERS
    THEN DBMS_OUTPUT.PUT_LINE('Records can not be deleted.');

END;

You can check the message in Output window. 您可以在“输出”窗口中查看消息。 Below is the output I got in PL/SQL developer after executing above block of query. 下面是执行上述查询块后在PL / SQL开发人员中获得的输出。

Records can not be deleted. 记录无法删除。

I hope it helps. 希望对您有所帮助。

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

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