Apache Derby 简明教程

Apache Derby - Triggers

在数据库中,触发器是每当发生事件时执行的语句/代码。一旦您为表上的特定事件创建了触发器,触发器中指定的代码将每次发生事件时执行。您可以在单个表上创建多个触发器。

本章将向您介绍如何使用 Apache Derby 创建和删除触发器。

Creating a trigger

您可以使用 CREATE TRIGGER 语句在 Derby 中创建触发器。

Syntax

以下是 CREATE TRIGGER 查询的语法。

CREATE TRIGGER trigger_name
{ NO CASCADE BEFORE | AFTER }
{INSERT [OR] | UPDATE [OR] | DELETE}[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
Statement

Example

假设我们在 Derby 中创建了一个名为 Emp 的表,如下所示。

CREATE TABLE Emp (
   Id INT NOT NULL,
   Name VARCHAR(255),
   Salary INT NOT NULL,
   Location VARCHAR(255)
);

并在其中插入了 5 行。

INSERT INTO Emp(Id, Name, Salary, Location) VALUES
   (1, 'Amit', 30000, 'Hyderabad'),
   (2, 'Kalyan', 40000, 'Vishakhapatnam'),
   (3,'Renuka', 50000, 'Delhi'),
   (4, 'Archana', 15000, 'Mumbai'),
   (5, 'Trupthi', 45000, 'Kochin');

如果我们有另一个名为 BackUp 的表,我们的目的是存储 Emp 表中的已删除行。

CREATE TABLE BackUp (
   Id INT NOT NULL,
   Name VARCHAR(255),
   Salary INT NOT NULL,
   Location VARCHAR(255)
);

以下查询在 DELETE 查询表中创建了一个名为 Emp 的触发器。它将 Emp 的已删除行存储在 Backup 表中。

ij> CREATE TRIGGER my_trigger
AFTER DELETE ON Emp
REFERENCING OLD AS oldRow
FOR EACH ROW MODE DB2SQL
INSERT INTO BackUp
VALUES (oldRow.Id, oldRow.Name, oldRow.Salary, oldRow.Location);

现在,从 Emp 表中删除一行,如下所示:

ij> Delete From Emp where Name = 'Kalyan';
1 row inserted/updated/deleted
ij> Delete From Emp where Name = 'Amit';
1 row inserted/updated/deleted

如果验证 BackUp 表,你可以在其中观察已删除的行。

ij> select * from BackUp;
ID |NAME   |SALARY |LOCATION
-------------------------------------------------------------------------
2  |Kalyan |40000  |Vishakhapatnam
1  |Amit   |30000  |Hyderabad

2 rows selected

Deleting a trigger

你可以使用 DROP TRIGGER 语句在 Derby 中删除触发器。

Syntax

以下是 DROP TRIGGER 查询的语法:

ij> Drop trigger tigger_name;

Example

以下示例删除了上面创建的触发器 my_trigger:

ij> Drop trigger my_trigger;
0 rows inserted/updated/deleted

JDBC example

以下 JDBC 程序在 Derby 中创建和删除触发器。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Triggers_Example {
   public static void main(String args[]) throws SQLException, ClassNotFoundException {
      //Registering the driver
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
      //Getting the Connection object
      String URL = "jdbc:derby:TestDataBase;create=true";
      Connection conn = DriverManager.getConnection(URL);

      //Creating the Statement object
      Statement stmt = conn.createStatement();

      //Creating the Emp table
      stmt.execute("CREATE TABLE Emp ( "
         + "Id INT NOT NULL, "
         + "Name VARCHAR(255), "
         + "Salary INT NOT NULL, "
         + "Location VARCHAR(255))");

      //Insert values in to the EMp table
      String query = "INSERT INTO Emp(Id, Name, Salary, Location) VALUES \r\n"
         +"(1, 'Amit', 30000, 'Hyderabad'), "
         + "(2, 'Kalyan', 40000, 'Vishakhapatnam'), "
         + "(3,'Renuka', 50000, 'Delhi'), "
         + "(4, 'Archana', 15000, 'Mumbai'), "
         + "(5, 'Trupthi', 45000, 'Kochin')";
         stmt.execute(query);

      //Creating the BackUp table
      stmt.execute("CREATE TABLE BackUp ( "
         + "Id INT NOT NULL, "
         + "Name VARCHAR(255), "
         + "Salary INT NOT NULL, "
         + "Location VARCHAR(255))");

      //Creating a trigger
      String createTrigger = "CREATE TRIGGER my_trigger "
         + "AFTER DELETE ON Emp "
         + "REFERENCING OLD AS oldRow "
         + "FOR EACH ROW MODE DB2SQL "
         + "INSERT INTO BackUp "
         + "VALUES (oldRow.Id, oldRow.Name, oldRow.Salary, oldRow.Location)";
      stmt.execute(createTrigger);
      System.out.println("Trigger created");

      //Deleting records from Emp table
      stmt.executeUpdate("Delete From Emp where Name = 'Kalyan'");
      stmt.executeUpdate("Delete From Emp where Name = 'Amit'");
      //Getting the contents of BackUp table
      ResultSet rs = stmt.executeQuery("SELECT * from BackUp");
      while(rs.next()){
         System.out.println(rs.getInt("Id"));
         System.out.println(rs.getString("Name"));
         System.out.println(rs.getString("Salary"));
         System.out.println(rs.getString("Location"));
         System.out.println(" ");
      }
   }
}

Output

在执行上述程序时,生成以下输出 −

Trigger created
2
Kalyan
40000
Vishakhapatnam
1
Amit
30000
Hyderabad