Oracle PL / SQL触发器(在插入新行时触发)

时间:2018-10-15 06:42:03

标签: oracle plsql database-trigger

我有下表:

CREATE TABLE film (
  film_id NUMBER(5) NOT NULL,
  title varchar2(255),
  description varchar2(255),
  release_year NUMBER(4) DEFAULT NULL,
  language_id NUMBER(3) NOT NULL,
  original_language_id NUMBER(3) DEFAULT NULL,
  rental_duration NUMBER(3) DEFAULT 3 NOT NULL,
  rental_rate NUMBER(4,2) DEFAULT '4.99',
  length NUMBER(5) DEFAULT NULL,
  replacement_cost NUMBER(5,2) DEFAULT '19.99' NOT NULL,
  rating varchar2(8) DEFAULT 'G',
  special_features varchar2(255) DEFAULT NULL
);


CREATE TABLE language (
  language_id NUMBER(3) NOT NULL,
  name varchar2(20) NOT NULL
);

以及以下约束:

ALTER TABLE FILM ADD CONSTRAINT FK_LANGUAGEID FOREIGN KEY (LANGUAGE_ID) REFERENCES LANGUAGE (LANGUAGE_ID);

ALTER TABLE FILM ADD CONSTRAINT FK_ORLANGUAGEID FOREIGN KEY (ORIGINAL_LANGUAGE_ID) REFERENCES LANGUAGE (LANGUAGE_ID);

问题是:

编写一个SQL触发器,例如名为``BI_FILM_LANG'',以将文本添加到插入数据库的每部新电影的描述中。它基于电影的语言(language_id)和原始语言(original_language_id)。您追加的文本格式应为(替换标记):

Originally in ​<original language>
​ . Re-released in ​<language>
​ . Original language and language should be the name of the language from the             language table. 

例如:如果运行了以下查询:

INSERT INTO FILM (title, description, language_id, original_language_id ) VALUES  ('B Movie', 'Movie about wasps.', 1, 2); 

运行以下选择语句(假设B电影的ID为9999999)时,它会产生以下内容:

SQL> SELECT description FROM FILM WHERE film_id = 9999999;

description 

---------------------------------------------------------
Movie about wasps. Originally in Italian. Re-released in English. 

1 个答案:

答案 0 :(得分:-1)

1。您必须编写一个编译指示区:

procedure TForm1.aButton1Click(Sender: TObject);
var
  DestPNG: TPngImage;
begin
  DestPNG := TPNGImage.Create;
  try
    // Initialize PNG
    DestPNG.CreateBlank(COLOR_RGBALPHA, 8, 60, 60);

    // Obtain PNG from image list
    LoadPNGFromImageList(ImageList1, 0, DestPNG);

    // Output PNG onto Canvas
    DestPNG.Draw(Canvas, Rect(0, 0, 60, 60));
    DestPNG.SaveToFile('C:\MyPNGIcon.png');
  finally
    DestPNG.Free;
  end;
end;  

2.Trigger

create or replace 
PROCEDURE update_data (ol NUMBER,rl NUMBER,film_id NUMBER
                                )
IS
   PRAGMA AUTONOMOUS_TRANSACTION;

  l_name  VARCHAR2(100);
  l_namerl    VARCHAR2(100);

BEGIN

 SELECT name   INTO l_name FROM language 
 WHERE language_id=ol;

  SELECT name INTO l_namerl FROM language 
 WHERE language_id=rl; 


  UPDATE film
  SET description=description||'.Originally in '||l_name||'. Re-released in '||l_namerl 

  WHERE film_id=film_id;

 COMMIT;    


END;

3。

create or replace 
TRIGGER sspr_air_tr
    AFTER INSERT
    ON film
    FOR EACH ROW 

BEGIN

 update_data(:new.language_id ,:new.original_language_id,:new.film_id);
END;